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  fromSeleniumImportWebdriver2  fromSelenium.webdriver.common.byImport by3  fromTimeImportSleep4 5 6 classPage (object):7     """8 base class, for inheritance of Page object classes9     """TenLogin_url ='http://mail.163.com/' One  A     def __init__(Self, selenium_driver, base_url=login_url): -Self.base_url =Base_url -Self.driver =Selenium_driver theSelf.timeout = 30 -  -     defon_page (self): -         returnSelf.driver.current_url = = (Self.base_url +Self.url) +  -     def_open (self, url): +url = self.base_url +URL A self.driver.get (URL) at         assertSelf.on_page (),'Did not land on %s'%URL -  -     defOpen (self): - Self._open (Self.url) -  -     defFind_element (Self, *Loc): in         returnSelf.driver.find_element (*Loc) -  to  + classLoginPage (Page): -     """ the 126 Mailbox Landing page Model *     """ $URL ='/'Panax Notoginseng     #Positioner -Username_loc = (By.id,"Idinput") thePassword_loc = (By.id,"Pwdinput") +Submit_loc = (By.id,"loginbtn") A  the     #Action +     deftype_username (self, username): -Self.find_element (*self.username_loc). Send_keys (username) $  $     defType_password (self, password): -Self.find_element (*self.password_loc). Send_keys (password) -  the     defSubmit (self): -Self.find_element (*self.submit_loc). Click ()Wuyi  the  - deftest_user_login (driver, username, password): Wu     """ - test whether the obtained username/password can be logged in About      """ $Login_page =LoginPage (Driver) - Login_page.open () - login_page.type_username (username) - login_page.type_password (password) A Login_page.submit () +  the  - defMain (): $     Try: theDriver =Webdriver. Chrome () theUsername ='[email protected]' thePassword ='fyr19910915' the test_user_login (driver, username, password) -Sleep (3) inText = Driver.find_element_by_xpath ("//span[@id = ' spnuid ')"). Text the         assert(Text = ='[email protected]'),"user name does not match, Login failed! " the     finally: About         #Close the browser window the driver.close () the  the  + if __name__=='__main__': -Main ()

Selenium +python page obiect design mode

Related Article

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.