学习笔记
环境:python39+pytest+unittest+selenium+webdriver
创建工程文件OOP,文件夹UI
base.py如下:
1 from selenium import webdriver 2 from selenium.webdriver.common.by import By 3 from selenium.webdriver.support.expected_conditions import NoSuchElementException 4 from selenium.webdriver.support.wait import WebDriverWait 5 from appium.webdriver.common.mobileby import MobileBy 6 7 8 class Factory(object): 9 def createObj(self, obj): 10 if obj == 'web': 11 return WebUI() 12 if obj == 'app': 13 return AppUI() 14 15 class WebDriver(object): 16 def __init__(self, driver): 17 self.driver = driver 18 19 def findElement(self, *loc): 20 try: 21 return WebDriverWait( 22 driver=self.driver, 23 timeout=10).until(lambda x: x.find_element(*loc)) 24 except NoSuchElementException as e: 25 return e.args[0] 26 27 def findElements(self, *loc): 28 try: 29 return WebDriverWait( 30 driver=self.driver, 31 timeout=10).until(lambda x: x.find_elements(*loc)) 32 except NoSuchElementException as e: 33 return e.args[0] 34 35 36 class WebUI(WebDriver): 37 def __str__(self): 38 return 'web' 39 40 class AppUI(WebDriver): 41 def __str__(self): 42 return 'app'View Code
page.py如下:
1 from selenium.webdriver.common.by import By 2 from OOP.ui.base import WebUI 3 4 class Baidu(WebUI): 5 so_loc=(By.ID,'kw') 6 7 def typeSo(self,content): 8 self.findElement(*self.so_loc).send_keys(content) 9 10 def getSo(self): 11 return self.findElement(*self.so_loc).get_attribute('value')View Code
test_baidu.py如下:
1 import unittest 2 from selenium import webdriver 3 import time as t 4 from OOP.ui.page import Baidu 5 6 class BaiduTest(unittest.TestCase,Baidu): 7 def setUp(self) -> None: 8 self.driver=webdriver.Chrome() 9 self.driver.maximize_window() 10 self.driver.get('http://www.baidu.com') 11 self.driver.implicitly_wait(30) 12 13 def tearDown(self) -> None: 14 self.driver.quit() 15 16 def test_baidu_so(self): 17 self.typeSo(content='搜索关键字') 18 t.sleep(5) 19 self.assertEqual(self.getSo(),'搜索关键字') 20 21 if __name__ == '__main__': 22 unittest.main(verbosity=2)
2022-10-6笔记
标签:__,return,self,driver,工厂,UI,自动化,import,def From: https://www.cnblogs.com/yuntimer/p/16758760.html