Selenium +python page obiect design mode

Source: Internet
Author: User

PageObject is one of the best design patterns for the Selenium Automation test project development practice, which mainly embodies the encapsulation of interface interaction details, which allows the test case to focus more on the business than on the interface details, thus improving the readability of the test case.

1. Understanding PageObject

The advantages of the PageObject design pattern are as follows:

* Reduce duplication of code

* Improve the readability of test cases

* Improve maintainability of test cases, especially for projects with frequent UI changes.

When you write a test for a Web page, you need to manipulate the elements on that Web page. However, if you manipulate HTML elements directly in the test code, the code is vulnerable because the UI can be very volatile. We can encapsulate the page object into an HTML page and then manipulate it by providing an application-specific API. Instead of being positioned in HTML.

One of the basic rules of thumb for page objects is that the Page object can be done by the software client, whatever the person can do. Therefore, he should provide an easy-to-program interface and hide the underlying parts of the window. So access to a text box should be achieved by means of an access method (accessor) to obtain and return the string, the check box should use a Boolean value, the button should be represented as a behavior-oriented method name. The Page object should encapsulate the behavior of all query and manipulation data on the GUI control as a method

A good rule of thumb is that the interface of a Page object should not change even if the specific control is changed

Although the term is a "page" object, it does not mean that you need to set up one such object for each page. For example, elements that are important to the page can be independent of a Page object. The purpose of the rule of thumb is to make the page more meaningful to the user of the application by modeling it.

 1 from selenium import Webdriver 2 from selenium.webdriver.common.by import by 3 from time import sleep 4 5 6 class Pag E (object): 7 "" "8 base class for the inheritance of the Page object Class 9" "Login_url = ' http://mail.163.com/' All Def __init__ (self, S         Elenium_driver, Base_url=login_url): Self.base_url = base_url14 Self.driver = selenium_driver15     Self.timeout = 3016 def on_page (self): return Self.driver.current_url = = (Self.base_url + self.url) 19 20 def _open (self, url): url = self.base_url + url22 self.driver.get (URL) @ Assert self.on_page (), ' did not be land on%s '% url24 def Open: Self._open (self.url)-def find_element (self, *lo      c): Return Self.driver.find_element (*loc), Class LoginPage (page): 33 "" "34 126 mailbox Landing Page Model 35" "36 url = '/' 37 # Locator Username_loc = (by.id, "idinput") Password_loc = (by.id, "Pwdinput") submit_l OC = (by.id, "loginbtn")Action43 def type_username (self, username): Self.find_element (*self.username_loc). Send_keys (User      Name) Type_password def (self, password): Self.find_element (*self.password_loc). Send_keys (password) 48 49  def submit (self): Self.find_element (*self.submit_loc). Click (), Test_user_login (driver, username,     Password): 54 "" "55 test to get the username/password can be logged in" "" Login_page = LoginPage (Driver) Login_page.open () 59 Login_page.type_username (username) login_page.type_password (password), Login_page.submit () + def main ( ): try:66 Driver = webdriver. Chrome () Username = ' [email protected] ' password = ' ******** ' Test_user_login (Driver, u         Sername, password) (3) The text = Driver.find_element_by_xpath ("//span[@id = ' spnuid ']"). Text72 assert (Text = = ' [email protected] '), "User name mismatch, Login failed!"     "Finally:74 # Close browser window 75    Driver.close () if __name__ = = ' __main__ ': Main () 

The code is interpreted as follows:

1. Create Page class

1 class page (object): 2     "" "3     base class for inheritance of Page object classes 4     " "5     login_url = ' http://mail.163.com/' 6  7     def __init__ (self, Selenium_driver, Base_url=login_url): 8         self.base_url = Base_url 9         self.driver = selenium_ Driver10         self.timeout = 3011     def on_page (self):         return Self.driver.current_url = = (Self.base_url + Self.url)     def _open (self, URL):         + url = self.base_url + url17         self.driver.get (URL)-         assert Self.on_page (), ' didn't ' land on%s '% url19-     def open (self):         Self._open (Self.url) +     def find_eleme NT (self, *loc):         return self.driver.find_element (*loc)
First, create a basic Class page, define the driver (driver) in the initialization Method __init__ (), the basic URL (base_url), and the time-out (timeout), etc., define the open () method to open the URL Web site, but it does not do this itself, Instead, it is handed
_open () method to implement. The assertion portion of the URL address is assigned to the On_page () method, and the Find_element () method is used to locate the element.

2. Create a LoginPage class

These methods, as defined in the page class, are the basic methods of page operations. The following is based on the features of the landing page to create the LoginPage class and inherit the page class, which is the most important object layer in the Page object design pattern.

1 class LoginPage (page): 2     "" "3     126 Mailbox Landing page Model 4     " "5     url = '/' 6     # locator 7     Username_loc = (by.id," I Dinput ") 8     Password_loc = (by.id," Pwdinput ") 9     Submit_loc = (by.id," loginbtn ")     # Action12     def Typ E_username (self, username):         self.find_element (*self.username_loc). Send_keys (username)-     def Type_ Password (self, password):         self.find_element (*self.password_loc). Send_keys (password) +     def submit ( Self):         self.find_element (*self.submit_loc). Click ()
The LoginPage class mainly encapsulates the elements on the landing page, making it a more specific method of operation. For example, the user name, password, and login buttons are all encapsulated as methods.

3. Create the Test_user_login () function

1 def test_user_login (driver, username, password): 2     "" "3      Test get username/password can login 4      " "5     login_page = LoginPage (Driver) 6     Login_page.open () 7     login_page.type_username (username) 8     Login_page.type_password (password) 9     login_page.submit ()

The Test_user_login () function consists of a single action, which consists of opening a browser, entering a username/password, and clicking Login. When using this function, Driver,username, password and other information should be used as the parameters of the function, so that the function is highly reusable.

4. Create the main () function

1 def Main (): 2     try:3         driver = webdriver. Chrome () 4         username = ' [email protected] ' 5         password = ' ******** ' 6         test_user_login (driver, username, Password) 7         sleep (3) 8         text = Driver.find_element_by_xpath ("//span[@id = ' spnuid ']"). Text 9         assert (Text = = ' [email protected] ', ' User name mismatch, Login failed! "Ten     finally:11         # Close browser window         driver.close () if __name__ = = ' __main__ ': +     Main ()

The main () function is closer to the user's action behavior, to the user, to make a mailbox login, need to care about is through which browser open the mailbox URL, login user name and password is what, as for the input box, button how to locate, then do not need to care.

The benefit of this layering is that different layers are concerned with different issues. The Page object layer concerns only the problem of element positioning, and the test case only cares about the test data.

One area of disagreement is whether the Page object should contain assertions itself, or simply provide data to the test script to set assertions. Advocates who include assertions in the Page object argue that this helps to avoid duplicate assertions in test scripts, makes it easier to provide better error information, and provides an API that is closer to a no-ask style. Advocates who do not include assertions in the Page object believe that the inclusion assertion mixes the responsibility of accessing the page data and implementing the assertion logic, and causes the Page object to be too bloated.

There is no assertion in the Page object, although we can eliminate duplication by providing an assertion library for common assertions, providing better diagnostics, but from a user's point of view of automation, judging whether the landing is successful is what the user needs to do, should not be left to the Page object layer to complete.

Another benefit of using Page object mode is to help reduce redundancy. If you need to enter a different username/password in 10 use cases, the main () method will become very concise.

Selenium +python page obiect design mode

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.