封装的目的在于当页面改动时,方便修改.
我们在做一个测试项目的时候,首先要将用到的这些按钮,属性封装好,方便后面调用或者修改
测试页面mailTest
import unittest from selenium import webdriver from MailLoginPage import MailLoginPage class SeTest(unittest.TestCase): def setUp(self): self.se = webdriver.Chrome() self.se.maximize_window() self.se.get('https://www.jianshu.com/sign_in') self.se.implicitly_wait(2) self.login_page = MailLoginPage(self.se) def testMail(self): username = self.login_page.username username.send_keys('18513729036') password = self.login_page.password password.send_keys('wangxue1254') self.se.implicitly_wait(2) self.login_page.login_button_click() self.se.implicitly_wait(10) print(self.se.title) def tearDown(self): pass if __name__ == '__main__': unittest.main()
MailLoginPage.py
class MailLoginPage: def __init__(self,driver): self.driver = driver def tab_login_click(self): return self.driver.find_element_by_xpath('/html/body/div[1]/div[2]/h4/div/a[1]').click() def tab_reg_click(self): return self.driver.find_element_by_id('js-sign-up-btn').click() @property #将方法换成属性调用 def username(self): return self.driver.find_element_by_id('session_email_or_mobile_number') @property def password(self): return self.driver.find_element_by_id('session_password') def login_button_click(self): return self.driver.find_element_by_id('sign-in-form-submit-btn').click() def remeber_me_click(self): return self.driver.find_element_by_id('session_remember_me').click() def login_button_click(self): return self.driver.find_element_by_id('sign-in-form-submit-btn').click()