Pytest 和 Allure的简单使用

项目搭建环境
 

  • python版本3.8
  • 浏览器Chrome80
  • Selenium 版本3.1.4

Pycharm创建Project

  • 打开Pycharm,点击【File】>>【New Project】打开项目环境编辑;

    Pytest 和 Allure的简单使用_第1张图片
     
  • 项目命名,选择项目使用的python环境, 点击【Create】

    Pytest 和 Allure的简单使用_第2张图片
     
  • 生成一个新的项目;

    Pytest 和 Allure的简单使用_第3张图片
     
  • 右键点击项目文件夹>> 【new】>> 【python Package】>> 输入一下文件夹名称
    分别创建:
                   #1 >>   commonFuntion         存放公用方法
                   #2 >>   testCase                      测试用例
                   #3 >>   testReport                   测试报告
     


    Pytest 和 Allure的简单使用_第4张图片



    创建完成后的文件夹
    Pytest 和 Allure的简单使用_第5张图片

编写python执行

  • 创建login方法: login_ports_page.py

    Pytest 和 Allure的简单使用_第6张图片
     
    from selenium import webdriver   # 引入Selenium的wendriver
    import  time
    
    webdriver = webdriver.Chrome()          #打开一个chrome浏览器
    webdriver.get("https://www.ports-intl.com/zh/account/login.html")       #在浏览器内打开一个URL
    
    time.sleep(10)
    webdriver.quit()  #退出浏览器

     

元素信息定位,完成页面登陆操作

  • 可以根据需求选择八大定位元素使用,包含复数定位(16种方法)  方法介绍链接

    Pytest 和 Allure的简单使用_第7张图片
     
    from selenium import webdriver   # 引入Selenium的wendriver
    import  time
    
    webdriver = webdriver.Chrome()          #打开一个chrome浏览器
    webdriver.get("https://www.ports-intl.com/zh/account/login.html")       #在浏览器内打开一个URL
    
    #元素定位
    webdriver.find_element_by_xpath('//*[@name="username"]').send_keys('17811895038')   #用户名元素定位并且输入信息
    webdriver.find_element_by_xpath('//*[@name="password"]').send_keys('12345678')      #密码元素定位并且输入信息
    webdriver.find_element_by_xpath('//*[@id="submit"]').click()                        #登录按钮元素定位,并发出点击操作
    
    
    
    
    time.sleep(10)
    webdriver.find_element_by_xpath('//*[@class="header_account"]/span[2]/a').click()    #退出账户
    time.sleep(5)
    webdriver.quit()  #退出浏览器

     

创建用例代码

  • 前置条件 测试场景 测试结果
    用户登录 1.用户跳转到【收货地址管理】 1.成功跳转【收货地址管理】页面
    2.点击【新增地址】按钮 2.页面加载【编辑收货地址】信息
    3.正确输入地址相关信息 3.确认信息已输入成功
    4.点击【保存】按钮 4.页面成功新增的收货地址并显示
  • 编写用例
    from selenium import webdriver   # 引入Selenium的wendriver
    import  time
    
    webdriver = webdriver.Chrome()          #打开一个chrome浏览器
    webdriver.get("https://www.ports-intl.com/zh/account/login.html")       #在浏览器内打开一个URL
    
    #元素定位
    webdriver.find_element_by_xpath('//*[@name="username"]').send_keys('17811895038')   #用户名元素定位并且输入信息
    webdriver.find_element_by_xpath('//*[@name="password"]').send_keys('12345678')      #密码元素定位并且输入信息
    webdriver.find_element_by_xpath('//*[@id="submit"]').click()                        #登录按钮元素定位,并发出点击操作
    
    
    
    time.sleep(5)
    #1.用户跳转到【收货地址管理】
    webdriver.find_element_by_xpath('//*[@href="/zh/account/address"]').click()
    
    # 2.点击【新增地址】按钮
    time.sleep(5)
    webdriver.find_element_by_xpath('//*[@class="btnstyle_s_b"]').click()
    time.sleep(5)
    webdriver.find_element_by_xpath('//*[@name="si_last_name"]').send_keys('姓氏')
    webdriver.find_element_by_xpath('//*[@name="si_firstname"]').send_keys('名称')
    webdriver.find_element_by_xpath('//*[@name="si_tel_mobile"]').send_keys('17811895038')
    webdriver.find_element_by_xpath('//*[@name="si_province"]').send_keys('17811895038')
    time.sleep(5)
    webdriver.find_element_by_xpath('//*[@name="si_province"]/option[5]').click()
    time.sleep(5)
    webdriver.find_element_by_xpath('//*[@name="si_city"]/option[5]').click()
    time.sleep(5)
    webdriver.find_element_by_xpath('//*[@name="si_district"]/option[5]').click()
    webdriver.find_element_by_xpath('//*[@name="si_address"]').send_keys('张望里斯')
    
    time.sleep(5)
    #4.点击【保存】按钮
    webdriver.find_element_by_xpath('//*[@class="btnstyle_s_b "]').click()
    
    
    
    time.sleep(10)
    webdriver.find_element_by_xpath('//*[@class="header_account"]/span[2]/a').click()    #退出账户
    time.sleep(5)
    webdriver.quit()  #退出浏览器

 

引入pytest 框架     

import pytest 
setup_class(self):  所有测试用例开始前执行一次 (只执行一次)  

 

teardown_class(self): 测试用例全部执行完成后执行 只执行一次)

  #   setup_class(self):  所有测试用例开始前执行一次 (只执行一次)
    def setup_class(self):
        self.driver.get("https://www.ports-intl.com/zh/account/login.html")  # 在浏览器内打开一个URL
        self.driver.refresh()
        time.sleep(5)
        self.driver.find_element_by_xpath('//*[@name="username"]').send_keys('17811895038')  # 用户名元素定位并且输入信息
        self.driver.find_element_by_xpath('//*[@name="password"]').send_keys('12345678')  # 密码元素定位并且输入信息
        self.driver.find_element_by_xpath('//*[@id="submit"]').click()  # 登录按钮元素定位,并发出点击操作

    #  teardown_class(self): 测试用例全部执行完成后执行 只执行一次)
    def teardown_class(self):
        time.sleep(5)
        self.driver.find_element_by_xpath('//*[@class="header_account"]/span[2]/a').click()
        self.driver.quit()

 

  • 断言
    Pytest 和 Allure的简单使用_第8张图片
     
     with allure.step("第一步:用户跳转到【收货地址管理】"):
                time.sleep(5)
                self.driver.find_element_by_xpath('//*[@href="/zh/account/address"]').click()
                time.sleep(5)
                addressTextFlag = self.driver.find_element_by_xpath('//*[@class="btnstyle_s_b"]').text
                assert '新增地址' == addressTextFlag  #   针对用例每一步操作做断言

     

引入Allure框架     

import allure

@allure.feature('用户添加地址测试用例')    测试报告内显示测试用例名称;
样式展示:
Pytest 和 Allure的简单使用_第9张图片

代码示例: 

    @allure.feature('用户添加地址测试用例')  #在用例方法上添加即可
    def test_addAddressd(self):

        with allure.step("第一步:用户跳转到【收货地址管理】"):

with allure.step()  为用例划分测试步骤,在测试报告内显示;
样式展示:
Pytest 和 Allure的简单使用_第10张图片
 代码示例: 


        with allure.step("第一步:用户跳转到【收货地址管理】"):
            time.sleep(5)
            self.driver.find_element_by_xpath('//*[@href="/zh/account/address"]').click()
            time.sleep(5)
            addressTextFlag = self.driver.find_element_by_xpath('//*[@class="btnstyle_s_b"]').text
            assert '新增地址' == addressTextFlag

        with allure.step("第二步:点击-新增地址-按钮"):
            time.sleep(5)
            self.driver.find_element_by_xpath('//*[@class="btnstyle_s_b"]').click()
            addressTextFlag = self.driver.find_element_by_xpath('//*[@class="mt20 addressinfo"]/P/b').text
            assert '编辑收货地址' == addressTextFlag

with allure.step()  为用例添加描述,在测试报告内显示;
样式展示:
Pytest 和 Allure的简单使用_第11张图片
代码示例:

  @allure.feature('用户添加地址测试用例')
    @allure.description('功能判定>> 用户添加完成在收获地址列表进行搜索比对是否包含新增地址')
    def test_addAddressd(self):

        with allure.step("第一步:用户跳转到【收货地址管理】"):
            time.sleep(5)
            self.driver.find_element_by_xpath('//*[@href="/zh/account/address"]').click()
            time.sleep(5)
            addressTextFlag = self.driver.find_element_by_xpath('//*[@class="btnstyle_s_b"]').text
            assert '新增地址' == addressTextFlag


 

@allure.link()在测试报告内添加测试用例管理工具的链接,在测试报告内显示;
样式展示:
代码示例:
    @allure.feature('用户添加地址测试用例')
    @allure.description('功能判定>> 用户添加完成在收获地址列表进行搜索比对是否包含新增地址')
    @allure.link('https:www.baidu.com')
    def test_addAddressd(self):

        with allure.step("第一步:用户跳转到【收货地址管理】"):
            time.sleep(5)

 

执行:

 

总代码:
 

import pytest  # 引入pytest框架                     pip install pytest
from selenium import webdriver  # 引入Selenium的wendriver            pip install selenium
import allure     # 引入allure框架                    pip install allure-pytest
import time  


class Test_addAddress_class():
    options = webdriver.ChromeOptions()
    options.add_argument('incognito')
    options.add_argument('--start-maximized')
    driver = webdriver.Chrome(chrome_options=options)

    #   setup_class(self):  所有测试用例开始前执行一次 (只执行一次)
    def setup_class(self):
        self.driver.get("https://www.ports-intl.com/zh/account/login.html")  # 在浏览器内打开一个URL
        self.driver.refresh()
        time.sleep(5)
        self.driver.find_element_by_xpath('//*[@name="username"]').send_keys('17811895038')  # 用户名元素定位并且输入信息
        self.driver.find_element_by_xpath('//*[@name="password"]').send_keys('12345678')  # 密码元素定位并且输入信息
        self.driver.find_element_by_xpath('//*[@id="submit"]').click()  # 登录按钮元素定位,并发出点击操作

    #  teardown_class(self): 测试用例全部执行完成后执行 只执行一次)
    def teardown_class(self):
        time.sleep(5)
        self.driver.find_element_by_xpath('//*[@class="header_account"]/span[2]/a').click()
        self.driver.quit()

    @allure.feature('用户添加地址测试用例')
    @allure.description('功能判定>> 用户添加完成在收获地址列表进行搜索比对是否包含新增地址')
    @allure.link('https:www.baidu.com')
    def test_addAddressd(self):

        with allure.step("第一步:用户跳转到【收货地址管理】"):
            time.sleep(5)
            self.driver.find_element_by_xpath('//*[@href="/zh/account/address"]').click()
            time.sleep(5)
            addressTextFlag = self.driver.find_element_by_xpath('//*[@class="btnstyle_s_b"]').text
            assert '新增地址' == addressTextFlag

        with allure.step("第二步:点击-新增地址-按钮"):
            time.sleep(5)
            self.driver.find_element_by_xpath('//*[@class="btnstyle_s_b"]').click()
            addressTextFlag = self.driver.find_element_by_xpath('//*[@class="mt20 addressinfo"]/P/b').text
            assert '编辑收货地址' == addressTextFlag

        with allure.step("第三步:正确输入地址相关信息"):
            time.sleep(5)
            self.driver.find_element_by_xpath('//*[@name="si_last_name"]').send_keys('姓氏q')
            self.driver.find_element_by_xpath('//*[@name="si_firstname"]').send_keys('名称w')
            self.driver.find_element_by_xpath('//*[@name="si_tel_mobile"]').send_keys('17811845038')
            self.driver.find_element_by_xpath('//*[@name="si_province"]').send_keys('17811495038')
            time.sleep(5)
            self.driver.find_element_by_xpath('//*[@name="si_province"]/option[5]').click()
            time.sleep(5)
            self.driver.find_element_by_xpath('//*[@name="si_city"]/option[5]').click()
            time.sleep(5)
            self.driver.find_element_by_xpath('//*[@name="si_district"]/option[5]').click()
            self.driver.find_element_by_xpath('//*[@name="si_address"]').send_keys('张望里斯4')
        with allure.step("第四步:点击【保存】按钮"):
            time.sleep(5)
            self.driver.find_element_by_xpath('//*[@class="btnstyle_s_b "]').click()
            time.sleep(5)
        with allure.step("第五步:最终的地址新增判断"):
            addressList = self.driver.find_elements_by_xpath('//*[@class="addresslist"]/*[@class="row"]')
            addressNum = len(addressList)

            i = 0
            while (i < addressNum):
                addressText = addressList[i].text
                i += 1
                if "张望里斯4" == addressText:
                    assert "张望里斯4" in addressText
                    break
                else:
                    assert "新增地址未查询到" == addressText




if __name__ == "__main__":

    pytest.main(["-s", "--alluredir=report" "Test_addAddress.py"])

 

a

a

 

你可能感兴趣的:(自动化Selenium)