PO模式:分层思想
base_page.py
from selenium import webdriver
from selenium.webdriver.remote.webdriver import WebDriver
class BasePage:
_base_url = ''
def __init__(self, driver: WebDriver = None): # 初始化 driver: webdriver python中的标记,标识符 ->bool :反馈类型为布尔值
self._driver = ''
if driver is None:
self._driver = webdriver.Chrome() # 声名driver
else:
self._driver = driver
if self._base_url != '':
self._driver.get(self._base_url)
def find(self, by, locator):
return self._driver.find_element(by, locator)
login.py
from selenium.webdriver.common.by import By
from page.base_page import BasePage
from page.register import Register
class Login(BasePage):
def goto_register(self):
self.find(By.CSS_SELECTOR, '.login_registerBar_link').click()
return Register(self._driver)
def scan(self):
pass
main.py
from selenium.webdriver.common.by import By
from page.base_page import BasePage
from page.login import Login
from page.register import Register
class Main(BasePage):
_base_url = 'https://work.weixin.qq.com/'
def goto_register(self): # 立即注册
self.find(By.CSS_SELECTOR, '.index_head_info_pCDownloadBtn').click() # . 代表class
return Register(self._driver)
def goto_login(self):
self.find(By.CSS_SELECTOR, '.index_top_operation_loginBtn').click()
return Login(self._driver)
register.py
from selenium.webdriver.common.by import By
from page.base_page import BasePage
class Register(BasePage):
def register(self):
self.find(By.ID, 'corp_name').send_keys('hello')
self.find(By.ID, 'manager_name').send_keys('hello02')
return True
test_register.py
from page.main import Main
class TestRegister:
def setup(self):
self.main = Main()
def test_register(self):
# assert self.main.goto_register().register()
assert self.main.goto_login().goto_register().register()