selenium+Page Objects(第三话)
Posted 我是冰霜
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了selenium+Page Objects(第三话)相关的知识,希望对你有一定的参考价值。
写好BasePage基类和页面元素定位后,就可以针对每个页面写业务逻辑了
1.编写每个页面page类,拿其中一个页面为例
fourth_page.py(名字我随便取的,实际中希望能取一些有意义的名字)
# coding: utf-8 # author: hmk from pages.BasePage import BasePage from locators.locators import Locators class FourthPage(BasePage):
"""定义一个page类,继承自BasePage类""" upload_button1 = Locators.fourth_page_locators["upload_button1"] # 材料列表第一个材料的上传按钮 upload_t = Locators.fourth_page_locators["upload_t"] upload_true = Locators.fourth_page_locators["upload_true"] # 去掉上传隐藏属性后的真实上传按钮 confirm = Locators.fourth_page_locators["confirm"] # 上传弹窗的确定按钮 nextstep = Locators.fourth_page_locators["nextstep"] # 下一步按钮 backstep = Locators.fourth_page_locators["backstep"] # 上一步按钮 def upload_button(self): """点击【上传】按钮""" self.click(self.upload_button1) def upload_local(self): """在上传弹窗中继续点击【本地上传】按钮""" self.click(self.upload_t) def hand_js(self): """调用js方法,执行js脚本""" js = ‘document.querySelector("#i_select_files>input").style="";‘ self.script(js) def true_upload(self): """去掉上传按钮隐藏属性后,传入文件""" self.send_keys(self.upload_true, "D:\\QQfile\qw.txt") def confirm_button(self): """点击【确定】关闭弹窗""" self.click(self.confirm) def next_step(self): """点击【下一步】,进入下一页面""" self.click(self.nextstep)
通过调用BasePage中的方法,来进一步编写这个页面用到的一些操作,最后在测试用例中根据业务场景来调用这些封装好的方法
2.编写测试用例
# coding: utf-8 # author: hmk import unittest import time from pages.first_page import FirstPage from pages.second_page import SecondPage from pages.third_page import ThirdPage from pages.fourth_page import FourthPage from selenium import webdriver class TestUni(unittest.TestCase): @classmethod def setUpClass(cls): cls.driver = webdriver.Chrome() cls.url = "approveSeq=6578" # self.run = BasePage(self.driver) # self.run.open(self.url) cls.first = FirstPage(cls.driver) cls.first.open(cls.url) cls.second = SecondPage(cls.driver) cls.third = ThirdPage(cls.driver) cls.fourth = FourthPage(cls.driver) @classmethod def tearDownClass(cls): time.sleep(2) cls.driver = cls.driver cls.driver.quit() def test_01(self): time.sleep(6) self.driver.implicitly_wait(30) # self.first.click_agree_button() print(self.first.get_current_url()) self.first.click_agree_button() def test_02(self): time.sleep(3) self.second.handle_js() # js = "var q = document.documentElement.scrollTop=1000" # self.driver.execute_script(js) self.driver.implicitly_wait(30) self.second.begin_deal() time.sleep(6) def test_03(self): self.third.handle_js() # self.third.input_custName() self.third.input_custAddr() self.third.input_custLegalMan() self.third.input_custCerId() self.third.input_custContactPerson() self.third.input_custCardId() self.third.click_nextstep()
if __name__ == "__main__":
unittest.main()
在每个用例汇中调用page类中定义的操作方法,构建业务场景。
综合前面几篇内容,把如何根据page objects设计模式进行ui自动化测试的思路以及基本实现方法说了一遍,当然其中还有一些不足,后续功能可以自己根据需要添加(比如添加日志、添加每一步的断言、生成测试报告)
2018-05-11 15:56:42
以上是关于selenium+Page Objects(第三话)的主要内容,如果未能解决你的问题,请参考以下文章