JAVA+SELENIUM+TESTNG Building Automation Test Architecture (3) Realizing Pom (page+object+modal)

Source: Internet
Author: User
Tags xpath testng

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.