Page Object Encapsulation

Source: Internet
Author: User
Tags xpath

Source: http://blog.csdn.net/zhenyu5211314/article/details/13632083

The page object mode is a test design mode in selenium. It mainly designs each page as a class, including the elements (buttons, input boxes, titles, etc.) to be tested on the page ), in this way, you can call the page class on the selenium test page to obtain the page elements. This cleverly avoids the need to test the Page code when the page element ID or position changes. When the page element ID changes, you only need to change the properties of the page in the test page class.

You can use ID, class, or XPath to obtain the attributes of elements on the page. If the ID is unique, you can use the ID to obtain the page elements. Otherwise, you can use XPath to locate the page elements.

You can use firebug tool in Firefox to search for page elements. First, right-click the elements and choose firebug to view the elements. Then, the elements are selected and right-click the Page code, select copy XPath to obtain the XPath of the element.

The specific operation interface is shown in:

The code for getting the username element using XPath is as follows:

 

[Java]View plaincopyprint?
  1. Public static final string username_xpath = "// * [@ ID = \" USERNAME \ "]";
public static final String USERNAME_XPATH = "//*[@id=\"username\"]";
[Java]View plaincopyprint?
  1. This. Driver. findelement (by. XPath (loginpageclass. contants. username_xpath). sendkeys ("username ");
this.driver.findElement(By.xpath(LoginPageClass.Contants.USERNAME_XPATH)).sendKeys("username");

The above line of code defines the XPath in the user name input box. The following line of code is to get the element through XPath, and fill in the value in the input box: username so easy!

 

You can also use the XPath checker tool to obtain the element's XPath, which is also proprietary to Firefox. Right-click the page element and select View XPath. The following interface is displayed:

Isn't there an XPATH above? Just copy it directly.

Now that we can get the element's XPath, write the loginpageclass code on the login page.

In fact, there are two design modes in the design of Page Object:

1. implement logical functions in the class, such as determining whether the title is correctly displayed and whether the page Jump is correct. You only need to return the function execution result (true or false ).

2. return the element content in the class, for example, return the title content. The specific logic is written in the test page code. In this way, the page object only needs to obtain the element and return value, and the work is relatively simple.

 

The following describes the specific implementation functions of the loginpageclass logon page. We use the first design mode.

First, we need to analyze the elements to be tested on the login page, including the page title, User box, password box, and login button. Then we need to set four constants, one stores the expected title value, and the other three are the XPath values of the three elements. The Code is as follows:

 

[Java]View plaincopyprint?
  1. Public static class contants {
  2. Public static final String title = "Tianjin Ninghe County Health Supervision System: logon page ";
  3. // Obtain page elements through xpath
  4. Public static final string username_xpath = "// * [@ ID = \" USERNAME \ "]";
  5. Public static final string password_xpath = "// * [@ ID = \" password \ "]";
  6. Public static final string login_button_xpath = "// * [@ ID = \" loginform \ "]/IMG ";
  7. }
Public static class contants {public static final String title = "Ninghe County Health supervision system, Tianjin: logon page "; // use XPath to obtain the public static final string username_xpath = "// * [@ ID = \" USERNAME \ "]"; public static final string password_xpath = "// * [@ ID = \" password \ "]"; public static final string login_button_xpath = "// * [@ ID = \" loginform \ "]/IMG ";}

Then, we will rewrite the loginpageclass class constructor, because when declaring the class object in the test page code, we should pass the WebDriver and the URL to open the page, you can use WebDriver to get the page elements. You can open the login page through the URL. We want to open the login page when declaring the object. The Code is as follows:

 

 

[Java]View plaincopyprint?
  1. Private WebDriver driver;
  2. // The URL of the login page
  3. Private string URL;
  4. // Automatically load the page when declaring an object
  5. Public loginpageclass (WebDriver driver, string URL ){
  6. This. Driver = driver;
  7. This. url = URL;
  8. // Load the page
  9. This. Driver. Get (this. url );
  10. }
Private WebDriver driver; // the URL of the login page private string URL; // when declaring an object, the page public loginpageclass (WebDriver driver, string URL) is automatically loaded {This. driver = driver; this. url = URL; // load the page this. driver. get (this. URL );}

Then, the preparations are complete. We need to write some basic functions for testing, such as entering different user names and passwords, and returning whether the logon is successful, here we have written two other functions. The first gettitle is used to obtain the title (this. driver. gettitle () is that simple). Another method is to judge whether the page is loaded normally with isloaded (in fact, the title is equivalent to the expected constant). Finally, this is what we just mentioned, the parameter is the username and password, and the login function T or F is returned. The Code is as follows:

 

 

[Java]View plaincopyprint?
  1. // Obtain the page title
  2. Public String gettitle (){
  3. Return this. Driver. gettitle ();
  4. }
  5. // Check whether the page is loaded and whether the title is equal. T/F is returned.
  6. Public Boolean isloaded (){
  7. System. Out. println (this. gettitle ());
  8. Return loginpageclass. contants. Title. Equals (this. gettitle ());
  9. }
  10. // Log on to the function, input the user name and password, click the login button, and determine whether the jump is successful Based on the title
  11. Public Boolean login (string username, string password ){
  12. This. Driver. findelement (by. XPath (loginpageclass. contants. username_xpath). sendkeys (username );
  13. This. Driver. findelement (by. XPath (loginpageclass. contants. password_xpath). sendkeys (password );
  14. This. Driver. findelement (by. XPath (loginpageclass. contants. login_button_xpath). Click ();
  15. // Jump to the home page and compare the title to determine whether the jump is successful
  16. Return mainpageclass. contants. Title. Equals (this. gettitle ());
  17. }
// Obtain the page title Public String gettitle () {return this. driver. gettitle () ;}// check whether the page is loaded. If the title is equal, return t/F public Boolean isloaded () {system. out. println (this. gettitle (); Return loginpageclass. contants. title. equals (this. gettitle ();} // log on to the function, input the user name and password, click the login button, and determine whether the jump is successful Based on the title Public Boolean login (string username, string password) {This. driver. findelement (. XPath (loginpageclass. contants. username_xpath )). sendkeys (username); this. driver. findelement (. XPath (loginpageclass. contants. password_xpath )). sendkeys (password); this. driver. findelement (. XPath (loginpageclass. contants. login_button_xpath )). click (); // jump to the home page and compare the title to determine whether the jump is successful. Return mainpageclass. contants. title. equals (this. gettitle ());}

Well, the above is the loginpageclass class we designed based on the first design mode of page object.

 

 

Next, let's start designing the test code, which is called loginpagetest. We can also differentiate it, because the logic is almost implemented in the class, in the test code, you only need to write some @ test functions (Note: One @ test indicates a test and Firefox will be re-opened, and their @ before and @ after conditions are the same)

First, write @ before and @ after. We need to define the WebDriver and URL passed to the loginpageclass class, declare a loginpageclass object, and then open Firefox in before, assign a value to the object (the URL will be opened at this time) and close the browser in after (quit is recommended to exit and close to exit with a bug ). The Code is as follows:

 

[Java]View plaincopyprint?
  1. Private Static final string url = "http: // 192.168.10.15: 8080/NH ";
  2. Private WebDriver driver;
  3. Private loginpageclass page;
  4. @ Before
  5. Public void setup () throws exception {
  6. System. setproperty ("WebDriver. Firefox. bin", "K:/program files/Mozilla Firefox/firefox.exe ");
  7. This. Driver = new firefoxdriver ();
  8. This. Page = new loginpageclass (this. Driver, URL );
  9. // This. Driver = new chromedriver (); // This is the driver of chrome.
  10. // This. Driver = new internetexplorerdriver (); // This is the driver of IE browser.
  11. // This. Driver = new htmlunitdriver (); // This is a non-Interface Test Mode. You do not need to open a browser and enter it in the background to determine whether the test case passes
  12. }
  13. @ After
  14. Public void teardown () throws exception {
  15. // Close the browser
  16. This. Driver. Quit ();
  17. }
Private Static final string url = "http: // 192.168.10.15: 8080/NH"; private WebDriver driver; private loginpageclass page; @ before public void setup () throws exception {system. setproperty ("WebDriver. firefox. bin "," K:/program files/Mozilla Firefox/firefox.exe "); this. driver = new firefoxdriver (); this. page = new loginpageclass (this. driver, URL); // This. driver = new chromedriver (); // This is the driver of chrome. // This. driver = new internetexplorerdriver (); // This is the driver of IE browser // This. driver = new htmlunitdriver (); // This is a non-Interface Test Mode. You do not need to open a browser and use background input to determine whether the test case passes.} @ after public void teardown () throws exception {// close the browser this. driver. quit ();}

The following is the last step. Write a few test cases, test whether the webpage is loaded normally, test whether the page title is as expected, enter a different user name and password, and test whether the jump is correct.

 

 

[Java]View plaincopyprint?
  1. @ Test
  2. Public void testpageloading (){
  3. Assertequals (true, this. Page. isloaded ());
  4. }
  5. @ Test
  6. Public void testtitle (){
  7. Assertequals (loginpageclass. contants. Title, this. Page. gettitle ());
  8. }
  9. @ Test
  10. Public void testloginone (){
  11. Assertequals (true, this. Page. login ("admin"," 12345678 "));
  12. }
  13. @ Test
  14. Public void testlogintwo (){
  15. Assertequals (true, this. Page. login ("admin"," 12345678 "));
  16. }
  17. @ Test
  18. Public void testloginthree (){
  19. Assertequals (true, this. Page. login ("admin"," 12345678 "));
  20. }
  21. @ Test
  22. Public void testloginfour (){
  23. Assertequals (true, this. Page. login ("admin"," 12345678 "));
  24. }
  25. @ Test
  26. Public void testloginfive (){
  27. Assertequals (false, this. Page. login ("Zhenyu", "12345678 "));
  28. }
  29. @ Test
  30. Public void testloginsix (){
  31. Assertequals (false, this. Page. login ("Zhenyu", "10086 "));
  32. }
  33. @ Test
  34. Public void testloginseven (){
  35. Assertequals (false, this. Page. login ("admin", "1234 5678 "));
  36. }
   @Test    public void testPageLoading() {        assertEquals(true, this.page.isLoaded());    }        @Test    public void testTitle() {        assertEquals(LoginPageClass.Contants.TITLE, this.page.getTitle());    }        @Test    public void testLoginOne() {        assertEquals(true, this.page.login("admin",  "12345678"));    }        @Test    public void testLoginTwo() {        assertEquals(true, this.page.login("Admin",  "12345678"));    }        @Test    public void testLoginThree() {        assertEquals(true, this.page.login("Admin",  "  12345678"));    }        @Test    public void testLoginFour() {        assertEquals(true, this.page.login("Admin",  "12345678  "));    }        @Test    public void testLoginFive() {        assertEquals(false, this.page.login("zhenyu",  "12345678"));    }        @Test    public void testLoginSix() {        assertEquals(false, this.page.login("zhenyu",  "10086"));    }        @Test    public void testLoginSeven() {        assertEquals(false, this.page.login("admin",  "1234  5678"));    }


Note: mainpageclass is the home page after I have defined the logon. It also contains its title and page elements.

 

 

The above is an example of the test login page implemented with page object. Finally, right-click loginpagetest and run as junittest. Check whether all your use cases have passed?

I passed all of them, haha.

Page Object Encapsulation

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.