This article has participated in the third “High Yield more text” track of the Denver Creators Training Camp, check out the details:Digg project | Creator Boot Camp phase 3 is underway, “write” to make a personal impact.
The introduction
Following the previous two BDD articles, this chapter describes the data-driven model
- It’s finally time for data-driven testing
The simplest interpretation of data-driven testing is that data is loaded outside of functional tests and used to extend automated test cases. One of the best examples is the customer order form. To use data-driven tests in this case, you can record an automated test and enter values in the individual fields. After recording an automatic test, it contains only the values you entered during recording. The data you specify most likely won’t cause errors in your application, but other data might. To ensure that the application works as expected, you can change the test to accept variables, which are then entered into data fields. Therefore, data-driven testing lets you run this test every time you want to add an order record, passing a new set of data each time.
This topic, testing needs to spend a lot of energy!!
- First, in feature, the parameter is passed in double quotes:
So when defining the Steps class:
@Given("^I am in the HR system with \"([^\"]*)\" account$")
public void i_am_in_the_HR_system_with_account(String arg1)
throws Throwable {
// Write code here that turns the phrase above into concrete actions
System.out.println(arg1);
}
@Given("^there is another user with \"([^\"]*)\" password$")
public void there_is_another_user_with_password(String arg1)
throws Throwable {
// Write code here that turns the phrase above into concrete actions
System.out.println(arg1);
}
@When("^I update password of the user to \"([^\"]*)\"$")
public void i_update_password_of_the_user_to(String arg1) throws Throwable {
// Write code here that turns the phrase above into concrete actions
System.out.println(arg1);
}
@Then("^I got the message \"([^\"]*)\"$")
public void i_got_the_message(String arg1) throws Throwable {
// Write code here that turns the phrase above into concrete actions
System.out.println(arg1);
}
@Then("^the user password should be \"([^\"]*)\"$")
public void the_user_password_should_be(String arg1) throws Throwable {
// Write code here that turns the phrase above into concrete actions
System.out.println(arg1);
}
Copy the code
- Second, parameterization of keywords. Examples, using <> parentheses instead of variables
Execution Result:
Other data driven approach, please refer to: www.toolsqa.com/cucumber/da…
- The general report format is as follows:
First poM adds the reporting configuration:
<dependency>
<groupId>com.vimalselvam</groupId>
<artifactId>cucumber-extentsreport</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports</artifactId>
<version>3.0.6</version>
</dependency>
Copy the code
To go back to the file reader, we create a config directory under the project root to hold the extent-report.xml configuration file
<extentreports>
<configuration>
<! -- report theme --> <! -- standard, dark -->
<theme>standard</theme>
<! -- document encoding --> <! -- defaults to UTF-8 -->
<encoding>UTF-8</encoding>
<! -- protocol for script and stylesheets --> <! -- defaults to https -->
<protocol>https</protocol>
<! -- title of the document -->
<documentTitle>ToolsQA - Cucumber Framework</documentTitle>
<! -- report name - displayed at top-nav -->
<reportName>ToolsQA - Cucumber Report</reportName>
<! -- global date format override --> <! -- defaults to yyyy-MM-dd -->
<dateFormat>yyyy-MM-dd</dateFormat>
<! -- global time format override --> <! -- defaults to HH:mm:ss -->
<timeFormat>HH:mm:ss</timeFormat>
<! -- custom javascript -->
<scripts><! [CDATA[ $(document).ready(function() { }); ]]></scripts>
<! -- custom styles -->
<styles><! [CDATA[ ]]></styles>
</configuration>
</extentreports>
Copy the code
Write a method to get the path to the configuration file in the configuration file reading class
public String getReportConfigPath(a){
String reportConfigPath = properties.getProperty("reportConfigPath");
if(reportConfigPath! =null) return reportConfigPath;
else throw new RuntimeException("Report Config Path not specified in the Configuration.properties file for the Key:reportConfigPath");
}
Copy the code
Finally, load the XML configuration file in the Runner launcher
@AfterClass
public static void writeExtentReport(a) {
Reporter.loadXMLConfig(new File(FileReaderManager.getInstance().getConfigReader().getReportConfigPath()));
}
Copy the code
Running Cucumber use cases with Junit test framework:
@RunWith(Cucumber.class)
@CucumberOptions( features = "src/test/resources/functionalTests", glue= {"stepDefinitions"}, plugin = { "com.cucumber.listener.ExtentCucumberFormatter:target/cucumber-reports/report.html"}, monochrome = true )
public class TestRunner {
@AfterClass
public static void writeExtentReport(a) {
Reporter.loadXMLConfig(newFile(FileReaderManager.getInstance().getConfigReader().getReportConfigPath())); }}Copy the code
The screenshot of the report is as follows: there is something wrong with the original style, please contact me if you need to know!!
Accustomed to the TestNG test framework, meet junit to try TestNG to test, so the starter need to be modified to look like the following Cucumber want to support TestNG framework, starter must inherit AbstractTestNGCucumberTests class
Use the after annotation method of the hooks, which are used in step. First implement screenshot method:
So add the screenshot method code in runner launcher as follows:
The report generated screenshots are as follows:
The default HTML report for the Cucumber framework is as follows:
- The command line:
If you want to generate another report, or if you want to change the report path. MVN test-dcucumber. Options = "plugin junit: target/cucumber-reports/report-xml". If you want cucumber to run only a single function file or multiple function files, you can pass the same parameters from the command line. MVN test-dcucumber. Options = "SRC/test/resources/functionalTests/end2end_tests. feature" can also pass multiple options at once. Options = "SomeThing" -dcucumber. Options = "SomeThing" -dcucumber. Options = "SomeThing" MVN test-dcucumber. Options = "SRC/test/resources/functionalTests/End2End_Tests. Feature" -dcucumber.options = "-tags @smoke" said at the end: 104.26.10.118 US IP url: <https://www.toolsqa.com/> Learning is necessary.Copy the code
Tips: Cool as a cucumber! If there is a step with the same name in the defined feature, it will find the corresponding method even if it is not defined in the Steps class. What if there is a step class with the same name instead? Must report an error!!
conclusion
Three chapters about The Cucumber behavior driven development mode of Java and have a better understanding of Po design mode and Factorypage. Selenium is fun to use for UI automation testing, but it’s even more fun to put your learning into practice. Welcome to test leaders to exchange ideas.