Cucumber
Q: Explain data table implementation in Cucumber ?
Ans:
In this example, we will pass the test data using the data table and handle it using Raw() method.
Scenario: Successful Login with Valid Credentials
Given User is on Home Page
When User Navigate to LogIn Page
And User enters Credentials to LogIn
| testuser_1 | Test@153 |
Then Message displayed Login Successfully
public void user_enters_testuser__and_Test(DataTable usercredentials) throws Throwable
{
//Write the code to handle Data Table
List<List<String>> data = usercredentials.raw();
//This is to get the first data of the set (First Row + First Column)
driver.findElement(By.id("log")).sendKeys(data.get(0).get(0));
//This is to get the first data of the set (First Row + Second Column)
driver.findElement(By.id("pwd")).sendKeys(data.get(0).get(1));
driver.findElement(By.id("login")).click();
}
usercredentials.raw()
converts the DataTable
into a List<List<String>>
—a list of lists where each inner list represents a row of data from the table.
@RunWith(Cucumber.class)
@CucumberOptions(
features = "Feature"
,glue={"stepDefinition"}
)
public class TestRunner {
}
Feature File
Feature: Login Action
Scenario: Successful Login with Valid Credentials
Given User is on Home Page
When User Navigate to LogIn Page
And User enters Credentials to LogIn
| testuser_1 | Test@153 |
Then Message displayed Login Successfully
Step Definition
public class Test_Steps {
public static WebDriver driver;
@When("^User enters Credentials to LogIn$")
public void user_enters_testuser__and_Test(DataTable usercredentials) throws Throwable {
List<List<String>> data = usercredentials.raw();
driver.findElement(By.id("log")).sendKeys(data.get(0).get(0));
driver.findElement(By.id("pwd")).sendKeys(data.get(0).get(1));
driver.findElement(By.id("login")).click();
}}
===================== Maps in Data Tables ======================
Maps in Data Tables can be used if different ways. Headers can also be defined for the data tables. A same step can be executed multiple times with different set of test data using Maps.
a .Maps in Data Tables with Header
In the previous chapter of Data Tables in Cucumber, we pass Username & Password without Header, due to which the test was not much readable. What if there will
be many columns. The basic funda of BDD test is to make the Test in Business readable format, so that business users can understand it easily. Setting Header in Test data
is not a difficult task in Cucumber. take a look at a below Scenario.
Feature File Scenario
Scenario: Successful Login with Valid Credentials
Given User is on Home Page
When User Navigate to LogIn Page
And User enters Credentials to LogIn
| Username | Password |
| testuser_1 | Test@153 |
Then Message displayed Login Successfully
The implementation of the above step will be like this:
@When("^User enters Credentials to LogIn$")
public void user_enters_testuser_and_Test(DataTable usercredentials) throws Throwable {
//Write the code to handle Data Table
List<Map<String,String>> data = usercredentials.asMaps(String.class,String.class);
driver.findElement(By.id("log")).sendKeys(data.get(0).get("Username"));
driver.findElement(By.id("pwd")).sendKeys(data.get(0).get("Password"));
driver.findElement(By.id("login")).click();
}
b .Maps in Data Tables with Multiple Test Data
In this test we will pass Username and Password two times to the test step. So our test should enter Username & Password once, click on LogIn button and repeat the same steps again.
Scenario: Successful Login with Valid Credentials
Given User is on Home Page
When User Navigate to LogIn Page
And User enters Credentials to LogIn
| Username | Password |
| testuser_1 | Test@153 |
| testuser_2 | Test@154 |
Then Message displayed Login Successfully
The implementation of the above step will be like this:
@When("^User enters Credentials to LogIn$")
public void user_enters_testuser_and_Test(DataTable usercredentials) throws Throwable {
//Write the code to handle Data Table
for (Map<String, String> data : usercredentials.asMaps(String.class, String.class)) {
driver.findElement(By.id("log")).sendKeys(data.get("Username"));
driver.findElement(By.id("pwd")).sendKeys(data.get("Password"));
driver.findElement(By.id("login")).click();
}
}
c .Map Data Tables to Class Objects
Luckily there are easier ways to access your data than DataTable. For instance you can create a Class-Object and have Cucumber map the data in a table to a list of these.
Scenario: Successful Login with Valid Credentials
Given User is on Home Page
When User Navigate to LogIn Page
And User enters Credentials to LogIn
| Username | Password |
| testuser_1 | Test@153 |
| testuser_2 | Test@154 |
Then Message displayed Login Successfully
The implementation of the above step will be like this:
@When("^User enters Credentials to LogIn$")
public void user_enters_testuser_and_Test(List<Credentials> usercredentials) throws Throwable {
//Write the code to handle Data Table
for (Credentials credentials : usercredentials) {
driver.findElement(By.id("log")).sendKeys(credentials.getUsername());
driver.findElement(By.id("pwd")).sendKeys(credentials.getPassword());
driver.findElement(By.id("login")).click();
}
}
Class Credentials
public class Credentials {
private String username;
private String password;
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
Q: How to rerun only failed test cases using TestNG in automation testing framework? TestNG listeners for Automation Framework
Ans:
1. Create a class which implements IRetryAnalyzer and provide the retry count @maxRetryCount
public class FailRetry implements IRetryAnalyzer
{
private int retryCount = 0;
private static final int maxRetryCount = 2;
@Override
public boolean retry(ITestResult result)
{
if (retryCount < maxRetryCount)
{
retryCount++;
return true;
}
return false;
}
}
2. Create a class which implements IAnnotationTransformer and pass the class name that we have created in step-1 @setRetryAnalyzer
public class RetryListener implements IAnnotationTransformer
{
@Override
public void transform(ITestAnnotation testannotation, Class testClass,Constructor testConstructor, Method testMethod)
{
IRetryAnalyzer retry = testannotation.getRetryAnalyzer();
if (retry == null)
{
testannotation.setRetryAnalyzer(FailRetry.class);//pass the class name created in Step-1
}
}
}
3. Add listener in testng.xml file after the suite tag as below and define the class name created in Step-2 with the package path
<suite name="Automation Test Suite">
<listeners>
<listener class-name="com.task.automation.util.RetryListener"/>
</listeners>
In Cucumber, we can re-run failed test cases using different approaches. Here are a few possible ways:
- Using Rerun plugin: The Rerun plugin is a built-in plugin that allows us to re-run failed test cases automatically. We need to specify the plugin in the Cucumber options and provide a file path to store the failed test cases. Here is an example:
kotlin@CucumberOptions(
plugin = {"rerun:target/rerun.txt"},
features = "src/test/resources/features",
glue = "com.example.steps"
)
After running the tests, the Rerun plugin creates a text file in the specified path that contains the failed test cases. We can then re-run the failed tests using the command:
bashmvn test -Dcucumber.options="@target/rerun.txt"
- Using Cucumber hooks: We can also use Cucumber hooks to retry failed test cases. We can create an After hook that checks if the scenario failed and reruns it based on a specified condition. Here is an example:
less@After
public void afterScenario(Scenario scenario) {
if (scenario.isFailed()) {
// Retry logic here
}
}
We can then implement the retry logic inside the if condition to rerun the failed scenario.
- Using TestNG retry analyzer: If we are using TestNG with Cucumber, we can use the TestNG retry analyzer to rerun failed test cases automatically. We need to create a retry analyzer class that implements the IRetryAnalyzer interface and provide the maximum number of retries. Here is an example:
javasee the first answer
We can then annotate the test method or scenario with the TestNG @RetryAnalyzer annotation to apply the retry analyzer. TestNG will automatically rerun the failed tests based on the provided logic.
Q:How to implement listener in selenium
ITestListener has following methods
- OnStart-> OnStart method is called when any Test starts.
- onTestSuccess-> onTestSuccess method is called on the success of any Test.
- onTestFailure-> onTestFailure method is called on the failure of any Test.
- onTestSkipped-> onTestSkipped method is called on skipped of any Test.
- onTestFailedButWithinSuccessPercentage-> method is called each time Test fails but is within success percentage.
- onFinish-> onFinish method is called after all Tests are executed.
The first way is to use Listeners annotation (@Listeners) as shown below:
Q: What are different Hooks in cucumber ?
Q: How to capture screenshot only for failed test in cucumber?
Q: How can I run same test case multiple times?
Q: How to integrate extent report in Cucumber with testNG ?
ExtentTest
: Represents a single test or a set of test steps in the report.ExtentHtmlReporter
and ExtentSparkReporter
: Reporters used to create and configure the report format and output location.
Comments
Post a Comment