文章目录
- 2.2.1 Webdriver包下面的test_case包下面的page_object下面的Base_Page代码
- 2.2.2 Webdriver包下面的test_case包下面的page_object下面的Login_page代码
- 2.2.3Webdriver包下面的test_case包下面的page_object下面的Add_page代码
1.任务目标
接着我们上一小结没学完的PO设计模式,我们继续学习一下PO设计模式,并熟练运用
2.任务实操
2.1任务目录
2.2任务代码
2.2.1 Webdriver包下面的test_case包下面的page_object下面的Base_Page代码
from time import sleep class Page(): def __init__(self, driver): self.driver = driver self.base_url = "http://test.pandabrother.cn/gpracticef/my/login.html" self.timeout = 10 def _open(self, url): url_ = self.base_url + url print("Test page is %s" % url_) self.driver.maximize_window() self.driver.get(url_) sleep(3) assert self.driver.current_url == url_, "Did not lend on %s" % url_ def open(self): self._open(self.url) def find_element(self, *loc): return self.driver.find_element(*loc) #这里主要封装点击方法,浏览器的ip地址
2.2.2 Webdriver包下面的test_case包下面的page_object下面的Login_page代码
from selenium.webdriver.common.by import By from Webdriver.test_case.page_object.Base_page import * class LoginPage(Page): url = "" username_loc = (By.CLASS_NAME, "form-control") psw_loc = (By.XPATH, '//*[@id="input-password"]') click_loc = (By.XPATH, '/html/body/div/div/div[1]/section/form/div[3]/button') def type_username(self, user): self.find_element(*self.username_loc).send_keys(user) def type_pws(self, pws): self.find_element(*self.psw_loc).send_keys(pws) def type_click(self): self.find_element(*self.click_loc).click() def test_user_login(driver, user, psw): login_page = LoginPage(driver) login_page.open() login_page.type_username(user) login_page.type_pws(psw) login_page.type_click() #在这个里面封装的是登录方法
2.2.3Webdriver包下面的test_case包下面的page_object下面的Add_page代码
from selenium.webdriver.common.by import By from Webdriver.test_case.page_object.Base_page import * class UserAdd(Page): url = '' sycd_loc = (By.XPATH, '//*[@id="sidebar-menu"]/div/ul/li/a') jcgl_loc = (By.XPATH, '//*[@id="sidebar-menu"]/div/ul/li/ul/li[2]/a') xinzeng_loc = (By.XPATH, '/html/body/div[1]/div/div[3]/div/div[3]/div[2]/div/form/button[2]') input_loc = (By.XPATH, '//*[@id="input-name"]') baocun_loc = (By.XPATH, '//*[@id="btn-save"]') cf_loc = (By.XPATH, '//*[@id="out-tip"]') def type_sycd(self): self.find_element(*self.sycd_loc).click() def type_jcgl(self): self.find_element(*self.jcgl_loc).click() def type_xinzheng(self): self.find_element(*self.xinzeng_loc).click() def type_input(self, input_name): self.find_element(*self.input_loc).send_keys(input_name) def type_baocun(self): self.find_element(*self.baocun_loc).click() def type_cf(self): return self.find_element(*self.cf_loc).text def test_user_add(driver, input_name): add_test = UserAdd(driver) add_test.type_sycd() add_test.type_jcgl() add_test.type_xinzheng() add_test.type_input(input_name) add_test.type_baocun() #在这里面封装的是登录进去后添加数据和断言的方法
3.小结
PO设计模式没有说完,未完待续。