Page Object Model (Selenium + Python) (iii) __python

Source: Internet
Author: User
Tags xpath

Based on the reference to several articles in the front, I wrote a pageobject framework according to the requirements of the system. The main tree-shaped structure is as follows:



The following are individual blocks of code:

1.base Page Module

This parent page can be used primarily on every page, so that subsequent pages are inherited (some methods are not complete and can be added on their own)

# _*_ Coding=utf-8 _*_ from ABC import abstractmethod to Pageobject.log.logger import Logging import time from Selenium . Webdriver.common.action_chains Import Actionchains class BasePage (object): Base_url = "XXXXX" def __init__ (sel
        F, driver): if self.base_url[-1]!= ': self.base_url + = '/' Self._validate_page (Driver)
        Self.driver = Driver Self.log_obj = Logging () @abstractmethod def _validate_page (self, driver): Return def open (self, url): url = self.base_url + URL self.driver.get (URL) def get_xpath (SE LF, XPath): "Return the DOM element of the XPath or the None object if the ' not found ' def click_el Ement (self, Locator, Is_button=true): "Click the button supplied" if is_button:self.driver.fi Nd_element (*locator). Click () else:f = Self.driver.find_element (*locator) actionchains (sel F.driver). Click (f). PerfoRM () def write (self, msg, level= ' ERROR '): Self.log_obj.write (msg, level) def set_text (self, locator, Val
            UES): Try:text_field = Self.driver.find_element (*locator) text_field.clear () Text_field.send_keys (values) except Exception as Msg:self.write (msg) def wait (self, wait_seconds
    =5): "Performs wait to time provided" Time.sleep (Wait_seconds) class Invalidpageexception (Exception):


 Pass

2.login page

This login page inherits from base page, which mainly writes the properties and controls of the login page (depending on the properties on your system login page, button, and so on)

# _*_ Coding=utf-8 _*_ from selenium.webdriver.support.ui import webdriverwait to base_page import basepage from Pageobj Ect.parameter.locator_element Import login_locators from base_element Import basetextelement class Usernameelement ( basetextelement): def __init__ (self): Self.locator = login_locators["Login.username"] class Passwordelement (basetextelement): def __init__ (self): Self.locator = login_locators["Login.password"] class LoginPage (Base Page: Username = usernameelement () password = passwordelement () def __init__ (self, driver): Basepag e.__init__ (self, driver) Self.open (Login_locators.get ("Login.url")) def submit (self): "Submit the Log In form "Self.click_element (Login_locators.get (" Login.submit ")) @property def Register (self):" Ju MP to register page ' self.click_element (login_locators["Login.register"]) from Register_page import regist Erpage return RegisteRPage (Self.driver)
 

3.base element

This page mainly writes the table, the Drop-down list, the input box and so on control's base class, so that the page's control inherits (above the login page's input box inherits from the following code the Basetextelement class)

# _*_ Coding=utf-8 _*_ from selenium.webdriver.support.ui import webdriverwait from Selenium.webdriver.common.action_ Chains Import Actionchains class Basetextelement (object): "" "" "" "" "Base Page class, is initialized on every page objec

    T class. "" " def __set__ (self, obj, value): "" "Sets the text to the value supplied" "Dr = Obj.driver Webdriver Wait (dr, MB). Until (Lambda driver:driver.find_element (*self.locator)) dr.find_element (*self.locator) . Clear () dr.find_element (*self.locator). Send_keys (value) def __delete__ (self, obj): Pass def __g 
    Et__ (self, obj, owner): "" "Gets the text of the specified object" "Pass class Basedropdownmenu (object): def __set__ (self, obj, value): Dr = Obj.driver # element = Dr.find_element (*self.btn_locator) # Ac
      Tionchains (DR). Click (Element). Perform ()  Dr.find_element (*self.btn_locator). Click () ul_element = Dr.find_element (*self.ul_locator) if ul_element. Tag_name.lower ()!= "select": # dr.find_element_by_link_text (U '%s '% value). Click () dr.find_elemen T_by_xpath (Self.ul_locator[1] + "/li[{0}]". Format (Value). Click () else:try:dr.find_e
                Lement (*self.ul_locator). Click () except Exception:dr.find_element (*self.btn_locator). Click () Dr.find_element (*self.ul_locator). Click () dr.find_element_by_css_selector ("Option[value=\"%s\ ] "% value". Click () class Basetableelement (object): "" "" "Base table class that is initialized on every page object cla

    ss. "" " @property def column (self): column = Len (self.driver.find_elements (*self.column_locator)) return Colum

    n @property def Row (self): row = Len (self.driver.find_elements (*self.row_locator)) return row def Get_elEment (self, *row_column): Try:element = Self.driver.find_element_by_xpath (Self.tbody_locator + "/tr[{ 0}]/td[{1}] ". Format (*row_column)" return element except Exception as Msg:print msg D EF Get_cell_text (Self, *value): # locator = "//*[@id =\" Wrapper\ "]/div/aside[2]/section/div/div/div/div/div/div/div
            /section/div[2]/table/tbody "If Self.row:text = Self.get_element (*value). Text else: Text = "Nothing" Return text def get_x_column_all_cell_text (self, column): "" "" Some column and row-column, such as {value: (1,2)} "" "Text_row = {} If self.row:for I in rang
            E (1, self.row+1): temp = Self.get_cell_text (i, column) text_row[temp] = I else:




 Text_row = {} return Text_row
4. The following will no longer repeat the relevant code block, what do not understand the place can be discussed, if you need to download the overall code, address: http://download.csdn.net/detail/lb245557472/9819007
Reference: Https://pragprog.com/magazines/2010-08/page-objects-in-python

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.