python+selenium2自动化---关键字驱动+unittest结合实现自动化


简单理解:

关键字驱动就是将页面元素的定位、操作等相关代码封装成方法(关键字),编写用例的时候直接调用对应方法(关键字),而不用关心页面元素的相关操作

unittest框架组织和执行测试用例

示例代码如下:

页面元素操作的关键字:

from selenium import webdriver class BasePage(): def __init__(self,name,url): self.driver = self.open_brower(name,url) def open_brower(self,name,url): if name == 'chrome': driver = webdriver.Chrome() if name=='ie': driver = webdriver.Ie() driver.get(url) driver.maximize_window() return driver #在输入框输入内容
    def input_text(self,locater_type,locater,text): if locater_type == 'id': self.driver.find_element_by_id(locater).send_keys(text) if locater_type == 'xpath': self.driver.find_element_by_xpath(locater).send_keys(text) if locater_type == 'name': self.driver.find_element_by_name(locater).send_keys(text) #点击按钮
    def click_ele(self, locater_type, locater): if locater_type == 'id': self.driver.find_element_by_id(locater).click() if locater_type == 'xpath': self.driver.find_element_by_xpath(locater).click() if locater_type == 'name': self.driver.find_element_by_name(locater).click()

测试用例代码

import unittest import time from ddt import ddt,data,unpack from MainPage import BasePage @ddt class TestLogin1(unittest.TestCase): def setUp(self): print('开始测试') def tearDown(self): print('结束测试') @data(('id','userAccount','id','password','xpath','//*[@id="root"]/div/div/div[2]/form/div[4]/div/div/div/button','xxxxxxx','123456'), ('id','userAccount','id','password','xpath','//*[@id="root"]/div/div/div[2]/form/div[4]/div/div/div/button','aaaaaaaa','123456')) @unpack def test_manager_login(self,locater_type,locator,locator_type1,locator1,locator_type2,locator2,text,text1): driver = BasePage('chrome','http://xxxxxxxxx.com/login') driver.input_text(locater_type,locator,text) driver.input_text(locator_type1, locator1, text1) driver.click_ele(locator_type2,locator2) time.sleep(2) driver.driver.quit() if __name__ == '__main__': unittest.main()

执行结果:

 

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM