码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2020-06-28 12:53:14      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:button   case   封装   xxx   png   用例   页面   path   stc   

简单理解:

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

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()

执行结果:

技术图片

 

 

 

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

标签:button   case   封装   xxx   png   用例   页面   path   stc   

原文地址:https://www.cnblogs.com/Xiaojiangzi/p/13202018.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!