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
Then the Cucumber Framework Project Actual Combat (I), enter the actual combat part
Automated test cases from Selenium tools into CUCUMBER natural language scripts for the BDD framework:
- Cucumber environment preparation, whether eclipse installation or POM configuration, requires Cucumber related components:
- Convert Selenium scripts into Cucumber style use cases;
The first step is to create a project and write a use case ending with feature
The second step is to create the actuator and execute the feature to help us generate step code to avoid artificial creation errors and improve efficiency
In step 3, the test code obtained in step 2 is written to the STEP file.
The fourth step, back to the second step, to know the meaning of @ CucumberOptions annotations, can go to the [tutorial] (https://www.toolsqa.com/cucumber/cucumber-options/)
Therefore, runner can be multiple because the condition cannot cover all of them.
- Cucumber also works in the PO testing mode of UI automation tools like Selenium
As selenium itself applies to Po patterns that are not affected by CUCUMBER, Cucumber is only responsible for invocation.
- Learn how to design a page object manager at Cucumber:
Create a Manager to manage all pageObjects because in the Cucumber framework you might need page objects for each step. The Page object (new) will be created multiple times, resulting in a waste of resources; Because there is only one driver in the same use case.
The page object is as follows:
The page object manager is as follows
To better understand the page object manager, let's change the above ternary operation to conditional judgment:
Test code, new a manager in beforeClass, create another page object, and new one again in @test
/ * * * *TODO:Test the role of the page object manager@author Joe-Tester
* @timeMay 6, 2021 *@file TestPageManager.java
*/
public class TestPageManager {
private WebDriver driver;
private LoginPage loginPage;
private SearchPage searchPage;
private PageObjectManager pageObjectManager;
@BeforeClass
void setUpClass(a) {
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
pageObjectManager = new PageObjectManager(driver);
loginPage = pageObjectManager.getLoginPage();
}
@Test
void testLoginPage(a) {
loginPage = pageObjectManager.getLoginPage();
loginPage.testPage();
}
@Test
void testSearchPage(a) {
searchPage = pageObjectManager.getSearchPage();
searchPage.openBrowser();
searchPage.inputTxtAndClickBtn("cucumber");
}
@AfterClass
void tearDownClass(a) { driver.quit(); }}Copy the code
The results are as follows:
- For flexible configuration, it is necessary to read configuration file classes in the framework
Try not to touch the code when changing parameters, such as what we need to write to the configuration file?
In Selenium, for example, during environment migration, our driver path may need to be changed. 2. Implicit wait time. 3.Copy the code
- File reader as Singleton, what is Singleton
The purpose of the Singleton is to control the creation of objects, limiting the number of objects to one. Since there is only one Singleton instance, any instance field of a Singleton will appear only once in each class, just like static fields.
How to create a Singleton
1. Private constructors that limit the instantiation of this class from other classes. A private static variable of the same class is a unique instance of that class. A public static method that returns an instance of a class, which is the global access point for the outside world to get a singleton instance of a class.Copy the code
- The same idea is that when we create a driver, we need to have a driver, but we can’t go to the code every time to change whether to use Chrome or Firefox or which environment address we are testing, so we need to design a WebDriverManager manager.
Write the browser type and test environment as the configuration. Determine which browser and test environment are local/remote
The test code is as follows:
Before that, configure the file configuration.properties
environment=local
browser=chrome
driverPath=drivers\\chromedriver.exe
implicitlyWait=10
url=https://www.baidu.com
windowMaximize=true
Copy the code
- Context testing, understanding is to connect the preceding and the following, using the same object.
Implementation: In the original Steps class, the webDriverManager is used, which requires two new steps, one for the drivermanager, and one for the page object manager. If you use PicoContainer, you only need to write a constructor in step. You only need the same driver for each page class.
Before modification
Encapsulate a test context
After transforming
- Implement the feature case using hooks that perform actions before and after the test, such as starting the browser, closing the browser, etc.
Tips: Create this class, and make sure it's under Glue
Before does not need to do this since the WebDriverManager manager is already in place before starting the driver
conclusion
To be continued, the next chapter is about the data-driven mode of BDD behavior driven framework!!