Cucumber Questions
Cucumber Setup:
Section-A
1. java- set environment in system variable variable at Jdk level JAVA_HOME. Add this path in path variable %java_home%\bin
2. Eclipse with java development
3. Cucumber eclipse plugin from market place of eclipse
4. Natural plugin
5 cucumber project skeleton uisng maven
6. Crete Maven Project--cucumber supports quickstart template version 1.1
7. POM.xml needs all jar info
8. Cucumber-Java and cucumber-junit
9. Afer adding all dependencies, go to project-->build automatically
Section-B
1. Feature File
2. Step Definition file
3. Options(Junit Test Runner) File
4. To create step definition file easily, using TD Gherkin chrome add on
Imp Note: If you are not able to Navigate to step defintion from feature file by clicking ctrl+click, then remove teh natural plugin that you have. Afer this you should see Cucumber icon clearly for feature file. earlier it was light green ball. after fix, dark cucumber icon.
Refer: https://stackoverflow.com/questions/14804945/maven-build-path-specifies-execution-environment-j2se-1-5-even-though-i-chang
To fix this add the below code to your pom file. Remember to do a Maven → Update project… afterwards or mvn clean compile at the command line.
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
After adding above to POM.xml
Working POM(Note that instead of taking latest dependencies, lesser version dependencies with gherkin dependency version 5.1.1 worked here
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Cucumber</groupId>
<artifactId>Automation</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Automation</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>6.8.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>6.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-core -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-core</artifactId>
<version>6.8.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/gherkin -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>gherkin</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.9.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
Note: While running tests from Maven, Build failure occurs. Make sure this is because of incorrect versions of junit or some other library. In the above pom, Junit has 4.11 version which works good.
EXAMPLES
1. Parameterization and Data Table
Step Definition
Monochrome = true gives a neat out put rather than a funky symbols
=========================================================================
@Smoke
Scenario:
When User searched for "cucumber" vegetable
@Reg
Scenario:
When User entered the below information in the feedback form
|Rahul|23years|
|M.Sc|Anna University|
1. Write step definition for the first
@RunWith(Cucumber.class)
public class MyStepDefinitions {
@When("^User searched for \"([^\"]*)\" vegetable$")
public void user_searched_for_something_vegetable(String strArg1) throws Throwable {
throw new PendingException();
}
}
2. Write step Definition for the second
1st Method
@When("^When User entered the below information in the feedback form$")
public void mutipledate(DataTable data) {
System.out.println("Data Table Concept");
List<Map<String,String>> mps = data.asMaps(String.class, String.class);
System.out.println(mps.get(0).get(0));
System.out.println(mps.get(0).get(1));
//System.out.println(mps.get(0).get("Address"));
}
2nd Method
@When("^When User entered the below information in the feedback form$")
public void mutipledate(DataTable credentials) {
System.out.println("Data Table Concept");
System.out.println("Mapping with multiple data Concept");
// Map<String,String> data = (Map<String, String>)
// credentials.asMaps(String.class, String.class);
for (Map<Object, Object> creds : credentials.asMaps(String.class, String.class)) {
System.out.println(creds.get("username"));
System.out.println(creds.get("password"));
}
3. How to run Selective scenarios
mvn test -Dcucumber.options="src/test/java/features/Login.feature:8"
4. how to generate reports in cucumber. Write a test runner file to generate cucumber html report
5. What is the difference between TestNG and Junit runner files
Junit:
@RunWith(Cucumber.class)
@CucumberOptions(
features = {"src/test/java/Features"},
glue = {"StepDefinitions"},
tags= "@MappingTest1",
// dryRun=true,
monochrome = true,
// tags= "@SmokeTest or @RegTest", either smoe or reg
// tags= "@SmokeTest and @RegTest", with both tags
// tags= "not @SmokeTest", Not smoke test
plugin = {"pretty","html:target/cucumber.html","json:target/cucumber.json","junit:target/cucumber.xml"},
stepNotifications = true
)
public class testRunner {
}
TestNG
@CucumberOptions(
features = {"src/test/java/Features"},
glue = {"StepDefinitions"}
)
public class TestNGtestRunner extends AbstractTestNGCucumberTests {
}
6. What is the purpose of stepNotifications in Test runner file
7. How to run cucumber tests/specific test runner using maven
mvn -Dtest=<<Name of the test runner>> test
8. How to override the tagged tests of cucumber using maven
1st way: mvn test -DCucumber.Options="--tags @TagName"
2nd way: mvn test -Dcucumber.filter.tags=
"
@TagName"
10. Running feature file from command line?
mvn test -Dcucumber.options=
"src/test/resources/features/featurefile.feature"
11. Creating cucumber repots from command line?
mvn test -Dcucumber.options="--plugin junit:target/cucumber-reports/cucumberReport.xml
12. Passing multiple parameters from command line?
mvn test -Dcucumber.options="src/test/resources/features/CucumberTagsExample.feature" -Dcucumber.filter.tags="@InValidCredentials"
13. What is the difference between Hooks and Background. Can we use both?
Back Ground runs before every Scenario. Its normally used for setup like envionment,
urls etc. Hooks are similar to annotation in junit. In general practice Back ground
should not be used with hooks. But if used, Background runs after the before hooks
Comments