The advantage of using the PageObject Page object is that
When the position of the page element changes,
Just need to modify the XPath or ID,
Without having to modify the test case itself;
This time the idea is:
1. Common Method Class
2. Page objects
3. Test Case Class
webdrivermethod.py:
From selenium import Webdriver
Class Seleniummethod (object):
# Common methods for package selenium
def __init__ (self, driver):
Self.driver = Driver
def startupfirefox (self):
# Open Firefox browser
Self.driver = Webdriver. Firefox ()
def openURL (self, urladdress):
# Open Address
Self.driver.get (urladdress)
def maxwindow (self):
# Maximize your browser window
Return Self.driver.maximize_window ()
def getTitle (self):
# get page title
Return Self.driver.title
def clearandiinput (self, location, value):
# position elements According to XPath and clear, enter
element = Self.driver.find_element_by_xpath (location)
Element.clear ()
Element.send_keys (value)
def click (Self, location):
# position elements according to XPath and click
return Self.driver.find_element_by_xpath (location). Click ()
def getText (self, location):
# position elements based on XPath and get text values
return Self.driver.find_element_by_xpath (location). Text
def closewindow (self):
# Close the window
Return Self.driver.close ()
def quitedriver (self):
# End Driver
Return Self.driver.quit ()
baiduhome.py:
from Webdrivermethod import seleniummethod
Class Baidupage (Seleniummethod):
# Baidu Page Object
Baiduur L = "https://www.baidu.com/"
# Baidu Home Address
Baidutitle = "Baidu a bit, you will know"
# Baidu homepage title
InputBox = ".//*[@id = ' kw '] "
# Baidu input box
Searchbotton =".//*[@id = ' su '] "
# Baidu Search button
Responsetitle =" China _ Baidu Search "
# The title of the search results page
Oneresult = ".//*[@id = ' 1 ']/h3/a"
# first line
Oneresulttext = "China _ Baidu Encyclopedia"
# Text of the first line
def openbaidu (self):
# Open Baidu
Self.startupfirefox ()
Self. Maxwindow ()
Self.openurl (Self.baiduurl)
def Searchchinese (self):
# search China
SELF.C Learandiinput (Self.inputbox, "China")
Self.click (Self.searchbotton)
def Exitbaidu (self):
# Fallback Out of Baidu
Self.closewindow ()
Self.quitedriver ()
baidutest.py:
Import UnitTest
From time import sleep
From Baiduhome import Baidupage
Class Mytestcase (UnitTest. TestCase):
def Test_searchchinese (self):
# test Case
Homepage = Baidupage (self)
Homepage.openbaidu ()
Assert Homepage.gettitle (), Homepage.baidutitle
# assert Baidu Headline
Sleep (2)
Homepage.searchchinese ()
Sleep (2)
Assert Homepage.gettitle (), Homepage.responsetitle
# Assert Search Results page title
Assert Homepage.gettext (Homepage.oneresult), Homepage.oneresulttext
# assert the text of the first line of search results
Homepage.exitbaidu ()
if __name__ = = ' __main__ ':
Unittest.main ()
Selenium (Python) pageobject Page Object