Selenium Object Page Design pattern understanding and implementation!

Source: Internet
Author: User
Tags xpath

Page Object mode is a test design pattern in selenium, which is to design each page as a class that contains the elements (buttons, input boxes, headings, etc.) that need to be tested on the page. This allows the page element to be fetched by invoking the page class on the Selenium test page, which subtly avoids the need to test the page code when the page element ID or location changes. When the page element ID changes, you only need to change the properties of the page in the test page class.

The properties of an element in a page can be obtained through id,class or XPath, where the ID can be used to get the page element, or the page element can be positioned using XPath.

You can use the Firebug tool in Firefox to find the page element, first right-click on the element, select Use Firebug to view the element, then the element is selected, right-click on the page code, select Copy XPath to get the XPath of the element.

The specific operating interface is as follows:

Use XPath to get the user name element code as follows:

 Public Static Final String Username_xpath = "//*[@id =\" username\ "]"this. Driver.findelement (By.xpath ( LoginPageClass.Contants.USERNAME_XPATH). SendKeys ("USERNAME");  

In fact, in the Page object design, you can have two design patterns:

1. Implement logic functions in class, such as determining whether the title is displayed correctly, if the page jumps correctly, only need to return the function execution result (True OR False)

2. Return element content in class, such as return title content, the specific logic in the test page code to write, so that the page Object only need to get the element and return value on the line, the work is relatively simple


Below we analyze the landing page Loginpageclass specific implementation function, we use the above first design pattern.

First of all, we want to analyze the landing page to test which elements, page title, User box, Password box, login button, on these four, then we set four constants, a storage of the expected title value, and three is the XPath value of the three elements. The code is as follows:

1      Public Static classcontants {2          Public Static FinalString TITLE = "Tianjin Ninghe County Sanitary Supervision System: Login Page"; 3         //get page elements from XPath4          Public Static FinalString Username_xpath = "//*[@id =\" username\ "]"; 5          Public Static FinalString Password_xpath = "//*[@id =\" password\ "]"; 6          Public Static FinalString Login_button_xpath = "//*[@id =\" loginform\ "]/img"; 7}

Then we rewrite the constructor of this Loginpageclass class, because when we declare the object of this class in the Code of the test page, we should pass the Webdriver and the URL of the page that we want to open, and we can get the elements of the page by Webdriver. The login page can be opened by URL and we want to open the login page when declaring the object. The code is as follows:

1     PrivateWebdriver Driver; 2     //URL of landing page3     PrivateString URL; 4       5     //automatically load pages when declaring objects6      PublicLoginpageclass (webdriver driver, String URL) {7          This. Driver =driver; 8          This. url =URL; 9         //Load PageTen          This. Driver.get ( This. URL);  One}

Then, the pre-preparation work is done, we need to write some basic functions for testing, such as entering a different user name and password, return to login success and so on, here we wrote another two functions, The first getTitle is used to get the title of the page (This.driver.getTitle (), which is so simple), there is another is to determine whether the page load normal isLoaded (in fact, the title is equal to the expected constant judgment), Finally, the argument is the user name and password, which returns the login function of T OR F. The code is as follows:

1     //Get page title2         PublicString GetTitle () {3            return  This. Driver.gettitle (); 4        }  5          6        //detects whether the page is loaded, determines whether the title is equal, returns t/f7         Public BooleanisLoaded () {8System.out.println ( This. GetTitle ()); 9            returnLoginPageClass.Contants.TITLE.equals ( This. GetTitle ()); Ten        }   One           A        //login function, incoming user name and password, and click the Login button, and then according to the title to determine whether the jump is successful -         Public BooleanLogin (string username, string password) { -             This. Driver.findelement (By.xpath (LoginPageClass.Contants.USERNAME_XPATH)). SendKeys (USERNAME);  the             This. Driver.findelement (By.xpath (LoginPageClass.Contants.PASSWORD_XPATH)). SendKeys (PASSWORD);  -             This. Driver.findelement (By.xpath (LoginPageClass.Contants.LOGIN_BUTTON_XPATH)). Click ();  -            //jump to the main page, compare the title to determine if the jump was successful -            returnMainPageClass.Contants.TITLE.equals ( This. GetTitle ());  +}

Well, here's the Loginpageclass class we designed based on page object's first design pattern.

Here we begin to design the test code, let's call it loginpagetest, because the logic is basically implemented in class almost, in the test code only need to write some @Test function on the line (note that a @test represents a test, will reopen Firefox, And their @before and @After conditions are the same)

First write @before and @After, we need to define webdriver and URLs to the Loginpageclass class, and declare a Loginpageclass object, and then open Firefox in before, Assign the object (this will open the URL at this point) and close the browser in after (it is recommended to quit with quit, close exits with a bug). The code is as follows:

1     Private Static FinalString URL = "Http://192.168.10.15:8080/nh"; 2     PrivateWebdriver Driver; 3     PrivateLoginpageclass page; 4       5 @Before6      Public voidSetUp ()throwsException {7System.setproperty ("Webdriver.firefox.bin", "K:/program Files/mozilla Firefox/firefox.exe" ); 8          This. Driver =NewFirefoxdriver (); 9          This. page =NewLoginpageclass ( This. Driver, URL); Ten       //this.driver = new Chromedriver ();//This is the driver for Chrome browser One       //this.driver = new Internetexplorerdriver ();//This is the driver of IE browser A       //this.driver = new Htmlunitdriver ();//This is a no-interface test mode, do not open the browser, through the background input to determine whether the test case through -     }   -        the @After -      Public voidTearDown ()throwsException { -         //Close Browser -          This. Driver.quit ();  +}

Selenium Object Page Design pattern understanding and implementation!

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.