Selenium (Python) Page object + data-driven test framework

Source: Internet
Author: User
Tags assert gettext unpack xpath

Directory structure for the entire project:

Common method Classes:

 class Seleniummethod (object): 
# Encapsulation Selenium Common method

def __init__ (self, driver):
Self.driver = Driver

def getTitle (self):
# Get page title
return self.driver.title

def Clearandinpu T (self, location, value):
# Locates the element according to the XPath and clears, enters
element = Self.driver.find_element_by_xpath br> element.clear ()
Element.send_keys (value)

def click (Self, location):
# based on XPath bit element and click
Return Self.driver.find_element_by_xpath. Click ()

def getText (self, location):
# Locate the element according to the XPath and get the literal value
return Self.driver.find_element_by_xpath (location). Text



Page object class:


From Commonmethod.webdrivermethod import Seleniummethod

Class Baidupage (Seleniummethod):
# Baidu Page Object

InputBox = ".//*[@id = ' kw ']"
# Baidu Input Box
Searchbotton = ".//*[@id = ' su ']"
# Baidu Search button
Oneresult = ".//*[@id = ' 1 ']/h3/a"
# Search Results First line

def searchchinese (self, keyword):
# Search Keywords
Self.clearandinput (self.inputbox, keyword)
Self.click (Self.searchbotton)


Test Case class:

Import CSV
Import UnitTest
From time import sleep

From DDT import DDT, data, unpack
From selenium import Webdriver

From Pageobject.baiduhome import Baidupage


Def getcsvdata ():
Value_rows = []
DataPath = "Testcase/testdirectory/testdata/csvtestdata.csv"
With open (DataPath, encoding= "UTF-8") as F:
F_csv = Csv.reader (f)
Next (F_csv)
For R in F_csv:
Value_rows.append (R)
Return value_rows

@ddt
Class Mytestcase (UnitTest. TestCase):

def setUp (self):
Self.driver = Webdriver. Firefox (executable_path= "Drive/geckodriver.exe")
Self.driver.maximize_window ()
Self.driver.get ("https://www.baidu.com/")
Assert Self.driver.title, "Baidu a bit, you know"
Sleep (2)

@data (*getcsvdata ())
@unpack
def test_searchchinese (self, searchterm, Answertitle, Answerresult):
"" "Baidu Search Test Case" ""
Homepage = baidupage (self.driver)
Homepage.searchchinese (Searchterm)
Sleep (3)
Assert Homepage.gettitle (), Answertitle
Assert Homepage.gettext (Homepage.oneresult), Answerresult

def tearDown (self):
Self.driver.close ()
Self.driver.quit ()

if __name__ = = ' __main__ ':
Unittest.main ()


Test Execution class:

Import Htmltestrunner

From time import strftime, localtime, time
From UnitTest import Defaulttestloader


if __name__ = = "__main__":
TestProject = "TestCase"
organize = Defaulttestloader.discover (TestProject, pattern= "*test.py")
now = strftime ("%y-%m-%m-%h_%m_%s", LocalTime (Time ()))
filename = "testcase/testdirectory/report/" + Now + ". html"
fp = open (filename, "WB")
Runner = Htmltestrunner.htmltestrunner (
STREAM=FP,
verbosity=2,
Title= "The title of the test Report",
description= "Case of test Case execution")
Runner.run (organize)
Fp.close ()


Test data:

Search keywords, search results title, search results first line
China, China _ Baidu Search, China _ Baidu Encyclopedia
United States, USA _ Baidu Search, USA _ Baidu Encyclopedia
UK, UK _ Baidu Search, UK _ Baidu Encyclopedia


Test report:



Selenium (Python) Page object + data-driven test framework

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.