UI自动化测试—PO设计模式(一)
写博起因:想把自己所学所做的,分享出去。
目录结构
--pages
--base_page.py
base_page.py
“`
coding:utf8
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
class BasePage(object):
def init(self,driver,path=None):
self.driver = driver
# self.driver = webdriver.Chrome()
self.url = ‘https://www.tapd.cn/‘
self.time_out = 10
self.poll_frequency = 0.3
self.open_web_page(path)
self.driver.maximize_window()
def open_web_page(self,path):
if path != None:
url = self.url + path
else:
url = None
if url != None:
self.driver.get(url)
else:
pass
def wait(self,*locator):
WebDriverWait(self.driver,self.time_out,self.poll_frequency).until(EC.visibility_of_element_located(locator))
def wait_text(self,text=None,*locator):
WebDriverWait(self.driver,self.time_out,self.poll_frequency).until(EC.text_to_be_present_in_element((locator),text))
def wait_all(self,text=None,*locator):
if text == None:
self.wait(*locator)
else:
self.wait_text(text,*locator)
def by_css(self,css,text=None):
locator = (By.CSS_SELECTOR,css)
self.wait_all(text,*locator)
return self.driver.find_element(*locator)
if name == ‘main‘:
driver = webdriver.Chrome()
test_base_page = BasePage(driver,’cloud_logins/login’)
test_base_page.by_css(‘#username’).send_keys(‘XXXXXX’)
test_base_page.by_css(‘#password_input’).send_keys(‘XXXXXX’)
test_base_page.by_css(‘#tcloud_login_button’).click()