Cucumber can be used to test almost any computer system. So far we have seen how to run a test using Eclipse IDE. There is one more way through which we can run the Cucumber test that is through command line interface. So what is the advantage of doing it?
Running any test framework from the Terminal has its own advantages, such as overriding the run configurations mentioned in the code.
In order to execute Cucumber test with command prompt, use the following steps after system configuration.
Step 1− Create a Maven Test Project named commandLine.
Go to File → New → Others → Maven → Maven Project → Next.
Provide group Id (group Id will identify your project uniquely across all projects).
Provide artifact Id (artifact Id is the name of the jar without version. You can choose any name which is in lowercase).
Click on Finish.
Open pom.xml −
Go to the package explorer on the left hand side of Eclipse.
Expand the project CucumberTest.
Locate pom.xml file.
Right-click and select the option, Open with “Text Editor”.
Add dependency for Selenium − This will indicate Maven, which Selenium jar files are to be downloaded from the central repository to the local repository.
Open pom.xml is in edit mode, create dependencies tag (<dependencies></dependencies>), inside the project tag.
Inside the dependencies tag, create dependency tag. (<dependency></dependency>).
Provide the following information within the dependency tag.
<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>2.47.1</version> </dependency>
Add dependency for Cucumber-Java − This will indicate Maven, which Cucumber files are to be downloaded from the central repository to the local repository.
Create one more dependency tag.
Provide the following information within the dependency tag.
<dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-java</artifactId> <version>1.0.2</version> <scope>test</scope> </dependency>
Add dependency for Cucumber-JUnit − This will indicate Maven, which Cucumber JUnit files are to be downloaded from the central repository to the local repository.
Create one more dependency tag.
Provide the following information within the dependency tag.
<dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-junit</artifactId> <version>1.0.2</version> <scope>test</scope> </dependency>
Add dependency for JUnit − This will indicate Maven, which JUnit files are to be downloaded from the central repository to the local repository.
Create one more dependency tag.
Provide following information within the dependency tag.
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency>
Verify binaries.
Once pom.xml is edited successfully, save it.
Go to Project → Clean − It will take few minutes.
Step 2 − Create a package named “outline” under src/test/java
Step 3 − Create a feature file named “commandLine.feature”.
Select and right-click on the package outline.
Click on ‘New’ file.
Give the file a name such as “commandLine.feature”
Write below text within the file and save it.
Feature − Scenario Outline
Scenario Outline − Login functionality for a social networking site.
Given the user navigates to Facebook
When I enter Username as "<username>" and Password as "<password>"
Then login should be unsuccessful
| username | password | | username1 | password1 | | username2 | password2 |
Note − Here, example annotation describes the range of input to be provided upon scenario execution. Test scenario will be executed for each of the input provided. So, in the given example, test scenario will be executed three times.
Step 4 − Create a step definition file.
Select and right-click on the package outline.
Click on ‘New’ file.
Name the file as commandLine.java
Write the following text within the file and save it.
package Outline; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import cucumber.annotation.en.Given; import cucumber.annotation.en.Then; i import cucumber.annotation.en.When; public class stepdefinition { WebDriver driver = null; @Given("^user navigates to facebook$") public void goToFacebook() { driver = new FirefoxDriver(); driver.navigate().to("https://www.facebook.com/"); } @When("^I enter Username as \"([^\"]*)\" and Password as \"([^\"]*)\"$") public void I_enter_Username_as_and_Password_as(String arg1, String arg2) { driver.findElement(By.id("email")).sendKeys(arg1); driver.findElement(By.id("pass")).sendKeys(arg2); driver.findElement(By.id("u_0_v")).click(); } @Then("^login should be unsuccessful$") public void validateRelogin() { if(driver.getCurrentUrl().equalsIgnoreCase( "https://www.facebook.com/login.php?login_attempt=1&lwv=110")){ System.out.println("Test Pass"); } else { System.out.println("Test Failed"); } driver.close(); } }
Note − In code, we have to define a function having two input arguments: one username and other will be for password. So, for each set of input provided in the example tag, set of GIVEN, WHEN and THEN will be executed.
Step 5 − Create a runner class file.
Select and right-click on the package outline.
Click on ‘New’ file.
Give the file a name such as, runTest.java
Write the following text within the file and save it.
package Outline; import org.junit.runner.RunWith; import cucumber.junit.Cucumber; @RunWith(Cucumber.class) @Cucumber.Options(format = {"pretty", "html:target/cucumber"}) public class runTest { }
Open a command prompt.
Go to the directory where this package “commandLine” resides. e:\Workspace\LoginTest\src>cd test\java
Run the command mvn test: You will see that all the scenario, described in the feature file have been executed (if there isn’t any error). Finally, at the bottom you will find the following information.
This describes the total test run, along with failure if any.
The previous command runs everything as mentioned in the JUnit Runner class. However, if we want to override the configurations mentioned in the Runner, following are the few examples to do it.
Now run command mvn test - Dcucumber.options="--help" on command prompt. Running this will print all available options.
To run the specific tags only, run the command mvn test -Dcucumber.options="--tags @SmokeTest" on command prompt. It will run only tags, which are marked with @SmokeTest.
In order to change the format of the result, run the command E:\Workspace\LoginTest>mvn test -Dcucumber.options="--plugin junit:target/cucumber-junit-report.xml" on command prompt It changes the report format to JUnit report generator.