• pytest 框架自动化Selenium 之yield 使用


    环境

    python 3.7 由于3.0-3.5以下部分pytest可能有部分兼容问题安装建议2.7-2.9,3.5-最新

    pip install pytest专属 pytest框架包

    pip install  allure-pytest 专属allure的报告包后面会用到可以不安装这里

    pip install pytest-html  pytest自己专属报告包

    pip install pytest-rerunfailures 失败重跑包也是pytest专属

    并发的也可以安下,利用多cpu运行调高用例执行速度

    python -m pip install xdist

    配置

    file>>setting>>tools>>Diff>>python integrated tools,选取pytest框架

    2.重新封装webdriver定位方法

    # coding=utf-8
    """
        @author:  ChenQuan 2019/1/20;
        @:param: rewrite old  find ways and return ele or elements ;
        @:param: Usage:  ele/elements = GetEle().ele_by_id(locator)
        @:param: driver is given by setup/fixture by yield
    
    """
    class GetEle(object):
    def __init__(self,driver):

    self.driver=driver

    def find_element(self,*loc):
    return self.driver.find_element(*loc)


    def find_elements(self,*loc):
    return self.driver.find_elements(*loc)

    def openPage(self,url):
    self.driver.get(url)

    def getTitle(self):
    return self.driver.current_url
    3.编写pytest脚本

    pytest命名规则:

    包名test_开头或者以_test结尾

    用例名称test_格式开头

    类名称Test开头

    4.脚本解释:

    yield 作用是类似return ,在用例执行完成后会返回继续执行yield后面代码,yield将在python4.0以后删除详情见官网

    # coding=utf-8
    import pytest
    from selenium import webdriver
    from pyfoo.Utils.pageobj import GetEle
    from pyfoo.Utils.pageobj import *
    @pytest.fixture(scope="class")
    def driver():
        url="http://www.baidu.com"
        driver= webdriver.Chrome()
        driver.get(url)
        yield driver
        driver.close()
        print("teardown...")
    
    @pytest.mark.usefixtures("driver")
    class Test_fix_module(object):
    
        def test_open_bd(self,driver):
            GetEle().find_element(By.IDr,'kw').send_keys("pytest")
            GetEle().find_element(By.ID,'su').click()
            print("搜索资源成功")
        def test_open_tb(self,driver):
            GetEle().find_element(By.XPATH,'//*[@id="s_tab"]/div/a[1]').click()
            print("进入资讯系列")
    
    if __name__ == "__main__":
        pytest.main(['-v','-s','-q'])

  • 相关阅读:
    自动化运维与Saltstack
    keepalived+nginx 高可用集群
    Nginx集群(负载均衡)
    Nginx优化
    Nginx管理(一)
    业务环境、测试、上线逻辑
    服务器部署逻辑
    python面试题——爬虫相关
    springmvc文件上传
    springmvc入门
  • 原文地址:https://www.cnblogs.com/SunshineKimi/p/10585999.html
Copyright © 2020-2023  润新知