1.Page object is one of the best design patterns for selenium automated test project development practices, reducing redundant code through encapsulation of interface elements, while in post-maintenance, if element positioning changes, only the code that encapsulates the page element needs to be adjusted. Improve the maintainability of test cases.
PageObject design pattern: is to enclose a page of all "element (contain control) attribute" and "element action" inside the 1 classes (Class).
2. Use our company's login page as an example to explain the POM pattern:
The pom pattern is the separation between the test code and the element positioning, ensuring that when the positioning of the element changes, the test code is not changed, and the test Code and the page elements are separated.
3. Element Positioning code:
Package com.rrx.test;
Import org.openqa.selenium.WebElement;
Import Org.openqa.selenium.support.FindBy;
Public class Testloginpage {
//Login name
@FindBy (xpath= ".//*[@id = ' username ']")
webelement UserName;
//Password
@FindBy (xpath= ".//*[@id = ' password ']")
webelement password;
//Login button
@FindBy (xpath= ".//*[@id = ' form ']/div[4]/button")
webelement login;
Public void Login (String user,string pass) {
System.out.println (user);
Username.sendkeys (user);
Password.sendkeys (pass);
Login.click ();
}
}
4. Test the code:
Package com.rrx.test;
Import java.io.IOException;
Import Org.openqa.selenium.WebDriver;
Import Org.openqa.selenium.support.PageFactory;
Import Org.testng.Assert;
Import Org.testng.Reporter;
Import Org.testng.annotations.Test;
Import Com.rrx.framework.BorwserEngin;
Import Com.rrx.framework.Logger;
Public class Newtest {
@Test
Public void F () throws IOException, interruptedexception {
borwserengin borwser=new borwserengin ();
Reporter.log ("I'm printing a log");
Webdriver Driver = Borwser.getdriver ();
testloginpage page=pagefactory.initelements (Driver, testloginpage.class);
page.login ("hexin001", "Test123456");
Reporter.log ("I'm out of the browser");
Thread.Sleep (+);
Logger.getlogger (). info ("exit");
//Borwser.teardown ();
}
}
JAVA+SELENIUM+TESTNG Building Automation Test Architecture (3) Realizing Pom (page+object+modal)