Selenium (Python) pageobject Page Object

Source: Internet
Author: User
Tags assert gettext xpath

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

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.