1、po模型介绍
- 在自动化中,Selenium自动化测试中有一个名字经常被提及PageObject(思想与面向对象的特征相
同),通常PO模型可以大大提高测试用例的维护效率 - 优点:重用,业务和对象分离,代码结构清晰,方便代码维护
实现BasePage
BasePage代码实现
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains # 鼠标操作
class BasePage():
'''BasePage封装所有界面都公用的方法。例如driver,find_element等'''
'''实例化BasePage类时,事先执行的__init__方法,该方法需要传递参数'''
def __init__(self, driver, url):
self.driver = driver
self.base_url = url
# 进入网址
def get(self):
self.driver.get(self.base_url)
# 元素定位,替代八大定位
def get_element(self, *locator):
return self.driver.find_element(*locator)
# 点击
def left_click(self, *locator):
self.driver.find_element(*locator).click()
# ActionChains(self.driver).click(self.get_element(*locator)).perform()
# 输入
def send_text(self, text, *locator):
self.driver.find_element(*locator).send_keys(text)
# 清除
def clear_text(self, *locator):
self.driver.find_element(*locator).clear()
def switch_iframe(self, *locator):
self.driver.switch_to.frame(self.driver.find_element(*locator))
def switch_window(self, n):
self.driver.switch_to.window(self.driver.window_handles[n])
# 用于断言
def get_title(self):
return self.driver.title
实现dianjishijian
dianjishijian代码实现
po001.py
from Basepage.one import BasePage
from selenium.webdriver.common.by import By
class Search(BasePage):
def __init__(self, driver, url):
BasePage.__init__(self, driver, url)
# 进入百度
def get_bai(self):
self.get()
# 获取输入框id
def shuru(self, test):
self.send_text(test, By.ID, "kw")
# 点击
def dianji(self):
self.left_click(By.ID, "su")
实现TestCase
TestCase代码
test_001.py
import unittest
from selenium import webdriver
from dianjishijian.po001 import Search
class TeseClass(unittest.TestCase):
def setUp(self) -> None:
print("开始")
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(10)
def test001(self):
url = "http://www.baidu.com"
b = Search(self.driver,url)
b.get_bai()
b.shuru("123")
b.dianji()
def tearDown(self) -> None:
print("结束")
if __name__ == '__main__':
unittest.main()
多个po(界面操作)
po002.py
from Basepage.one import BasePage
from selenium.webdriver.common.by import By
class Search2(BasePage):
def __init__(self, driver, url):
BasePage.__init__(self, driver, url)
# 点击
def hao_123(self):
self.left_click(By.XPATH, ".//*[@id='1']/h3/a[1]")
def switch_windowss(self,n):
self.switch_window(n)
po003.py
from Basepage.one import BasePage
from selenium.webdriver.common.by import By
class Search3(BasePage):
def __init__(self, driver, url):
BasePage.__init__(self, driver, url)
# 点击
def baidu(self):
self.left_click(By.XPATH, ".//*[@id='userCommonSites']/ul/li[6]/div/a")
test_001.py
import unittest from selenium import webdriver from dianjishijian.po001 import Search from dianjishijian.po002 import Search2 from dianjishijian.po003 import Search3
class TeseClass(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
print("开始")
cls.driver = webdriver.Firefox()
cls.driver.implicitly_wait(10)
def test001(self):
url = "http://www.baidu.com"
b = Search(self.driver, url)
b.get_bai()
b.shuru("123")
b.dianji()
def test002(self):
url = ""
c = Search2(self.driver, url)
c.hao_123()
c.switch_windowss(1)
def test003(self):
url = ""
d = Search3(self.driver, url)
d.baidu()
@classmethod
def tearDownClass(cls) -> None:
print("结束")
if __name__ == '__main__':
unittest.main()
运行结果图