pytest 框架自動化Selenium 之yield 使用

環境css

python 3.7 因爲3.0-3.5如下部分pytest可能有部分兼容問題安裝建議2.7-2.9,3.5-最新html

pip install pytest專屬 pytest框架包python

pip install  allure-pytest 專屬allure的報告包後面會用到能夠不安裝這裏web

pip install pytest-html  pytest本身專屬報告包併發

pip install pytest-rerunfailures 失敗重跑包也是pytest專屬app

併發的也能夠安下,利用多cpu運行調高用例執行速度框架

python -m pip install xdisturl

配置spa

file>>setting>>tools>>Diff>>python integrated tools,選取pytest框架3d

 

 

 

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'])

相關文章
相關標籤/搜索