Learn the POM framework of the great god next door, and modify and execute it based on your own use cases. The main problem encountered in the operation is how to write and run each module in a unified manner, each folder requires the _ init _ module for the modules to be imported.
Pom is mainly divided into three parts: 1. Write basic selenium operations as their own functions; 2. Write the use case operations using their own operation functions, 3. Add the unittest module and report module. Of course, an execution main function is required later.
# Encoding = UTF-8
From selenium import WebDriver
Import time
Driver = WebDriver. Firefox ()
Driver. implicitly_wait (30)
Class page (object ):
Def _ init _ (self, driver, base_url = u "http://www.baidu.com /"):
Self. Driver = driver
Self. base_url = base_url
Self. Timeout = 30
Def find_element (self, * LOC ):
Return self. Driver. find_element (* LOC)
Def input_clear (self, Loc ):
Self. find_element (* LOC). Clear ()
Def input_text (self, Loc, text ):
Self. find_element (* LOC). send_keys (text)
Def click (self, Loc ):
Self. find_element (* LOC). Click ()
Def get_title (Self ):
Return self. Driver. Title
From selenium. WebDriver. Common. By import
From P. basepage import page
Reload (sys)
SYS. setdefaultencoding ("UTF-8 ")
Class searchpage (page ):
Search_input = (by. ID, u'kw ')
Search_button = (by. ID, u'su ')
Def _ init _ (self, driver, base_url = u "http://www.baidu.com /"):
Page. _ init _ (self, driver, base_url)
Def gotopage (Self ):
Print U "Open homepage:", self. base_url
Self. Driver. Get (self. base_url)
Def input_search_clear (Self ):
Print U "Clear default value"
Self. input_clear (self. search_input)
Def input_search_text (self, text = u "automated test report "):
Print U "Enter search keywords: automated test report"
Self. input_text (self. search_input, text)
Def click_search_btn (Self ):
Print U "Click OK"
Self. Click (self. search_button)
The essence of these two classes is to separate element locating from element operations.
# Encoding = UTF-8
Import unittest
Import sys, time
From selenium import WebDriver
From P. searchpage import searchpage
Reload (sys)
SYS. setdefaultencoding ("UTF-8 ")
# Baidu search test
Class testsearchpage (unittest. testcase ):
Def setup (Self ):
Self. Driver = WebDriver. Firefox ()
Def testsearch (Self ):
Driver = self. Driver
# Baidu website
Url = u "http://www.baidu.com /"
# Search text
TEXT = u "automated test report"
# Expected verification title
Assert_title = u "automated test report _ Baidu search"
Print assert_title
Search_page = searchpage (driver, URL)
# Start a browser and access the Baidu Homepage
Search_page.gotopage ()
# Clear default values
Search_page.input_search_clear ()
# Enter search term
Search_page.input_search_text (text)
# Click Baidu to search
Search_page.click_search_btn ()
Time. Sleep (2)
# Verify the title
Self. assertequal (search_page.get_title (), assert_title)
Def teardown (Self ):
Self. Driver. Quit ()
The asserted result is correct.
The above is the POM framework, add batch execution and report
# Encoding = UTF-8
Import unittest
Import sys
Import htmltestrunner
From O. testsearchpage import testsearchpage
If _ name _ = '_ main __':
Testunit = unittest. testsuite ()
Testunit. addtest (testsearchpage ('testsearch '))
# Define the report output path
Htmlpath = u "page_demo_report.html"
Fp = file (htmlpath, "WB ")
Runner = htmltestrunner. htmltestrunner (Stream = FP,
Title = u "Baidu test ",
Description = u "test case result ")
Runner. Run (testunit)
FP. Close ()
Generate Report
Selenium, unittest -- pom framework and report