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

2021-05-02 16:29

阅读:425

标签: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

上一篇:Python爬虫 - scrapy

下一篇:线程池浅析


评论


亲,登录后才可以留言!