当前位置: 首页 > wzjs >正文

asp.net网站开发典型模块与实例精讲源码社区

asp.net网站开发典型模块与实例精讲,源码社区,鄂州网站设计制作,重庆黄页网站目录 一、xpath定位1. 属性定位2.属性与逻辑结合3. 属性与层级结合 二、cookie1. 验证码处理方案2. cookie3. 案例:cookie跳过登录 三、pytest1. 介绍及安装2. 定义用例3. 执行测试用例3.1 命令行运行3.2 配置文件运行3.3 项目配置文件config.py 4. 参数化5. 断言6.…

目录

  • 一、xpath定位
    • 1. 属性定位
    • 2.属性与逻辑结合
    • 3. 属性与层级结合
  • 二、cookie
    • 1. 验证码处理方案
    • 2. cookie
    • 3. 案例:cookie跳过登录
  • 三、pytest
    • 1. 介绍及安装
    • 2. 定义用例
    • 3. 执行测试用例
      • 3.1 命令行运行
      • 3.2 配置文件运行
      • 3.3 项目配置文件config.py
    • 4. 参数化
    • 5. 断言
    • 6. allure报告

一、xpath定位

1. 属性定位

在这里插入图片描述

  • 需求:打开注册页面。完成以下操作:
    1). 利用元素的属性信息精准定位用户名输入框,并输入:admin
  • 项目地址:http://121.43.169.97:8848/pageA.html
# 导包
from selenium.webdriver.common.by import By
from tools.chromeDriver import get_driver, quit_driverdriver = get_driver("http://121.43.169.97:8848/pageA.html")driver.find_element(By.XPATH, "//*[@id='userA']").send_keys("admin")# 退出浏览器
quit_driver(driver)

2.属性与逻辑结合

在这里插入图片描述

  • 需求:打开注册页面。完成以下操作:
    1). 利用属性与逻辑结合在test1输入框输入:admin
  • 项目地址:http://121.43.169.97:8848/pageA.html
# 导包
from selenium.webdriver.common.by import By
from tools.chromeDriver import get_driver, quit_driverdriver = get_driver("http://121.43.169.97:8848/pageA.html")driver.find_element(By.XPATH, "//*[@name='user' and @class='login']").send_keys("admin")# 退出浏览器
quit_driver(driver)

3. 属性与层级结合

在这里插入图片描述

  • 需求:打开注册页面。完成以下操作:
    1). 利用属性与逻辑结合在test1输入框输入:admin
  • 项目地址:http://121.43.169.97:8848/pageA.html
# 导包
from selenium.webdriver.common.by import By
from tools.chromeDriver import get_driver, quit_driverdriver = get_driver("http://121.43.169.97:8848/pageA.html")
# driver.find_element(By.XPATH, "//*[@name='user' and @class='login']").send_keys("admin")
driver.find_element(By.XPATH, "//*[@id='p1']/input").send_keys("admin")# 退出浏览器
quit_driver(driver)

二、cookie

1. 验证码处理方案

  • 去掉验证码、万能验证码
  • 验证码识别技术
  • 记录cookie

在这里插入图片描述

2. cookie

在这里插入图片描述

3. 案例:cookie跳过登录

在这里插入图片描述

  • 需求:使用cookie实现跳过登录
    • 1). 手动登录商城,获取cookie
    • 2). 使用获取到的cookie,达到登录目的,然后就可以执行登录之后的操作

在这里插入图片描述

# 导包
import time
from selenium import webdriver# 打开浏览器
driver = webdriver.Chrome()# 打开页面
driver.get("https://hmshop-test.itheima.net/Home/User/index.html")
time.sleep(2)# 绕过登录
cookie_data = {"name": "PHPSESSID","value": "h0lrprhei3p3v16pr8sb2ehrm3"
}
driver.add_cookie(cookie_data)
time.sleep(2)
driver.refresh()  # 刷新# 退出浏览器
time.sleep(3)
driver.quit()

三、pytest

1. 介绍及安装

  • pytest:python中的一种单元测试框架。
  • pytest作用
    • 能阻止多个用例去执行
    • 方便实现参数化
    • 能够生成测试报告
  • 安装
    • 安装: pip install pytest
      在这里插入图片描述

    • 验证: pip show pytest
      在这里插入图片描述

2. 定义用例

在这里插入图片描述

# 导包
import time
from selenium import webdriver
from selenium.webdriver.common.by import Byclass TestLogin:def setup_class(self):# 打开浏览器self.driver = webdriver.Chrome()self.driver.maximize_window()def setup(self):# 打开页面self.driver.get("https://hmshop-test.itheima.net/Home/user/login.html")def test_login(self):# case02:登录失败(用户名为空)self.driver.find_element(By.ID, "username").send_keys("")self.driver.find_element(By.ID, "password").send_keys("123456")self.driver.find_element(By.ID, "verify_code").send_keys("8888")self.driver.find_element(By.LINK_TEXT, "登   录").click()def test_login2(self):# case03:登录失败(密码为空)self.driver.find_element(By.ID, "username").send_keys("13488888888")self.driver.find_element(By.ID, "password").send_keys("")self.driver.find_element(By.ID, "verify_code").send_keys("8888")self.driver.find_element(By.LINK_TEXT, "登   录").click()def test_login3(self):# case01:登录成功self.driver.find_element(By.ID, "username").send_keys("13488888888")self.driver.find_element(By.ID, "password").send_keys("123456")self.driver.find_element(By.ID, "verify_code").send_keys("8888")self.driver.find_element(By.LINK_TEXT, "登   录").click()def teardown(self):time.sleep(2)def teardown_class(self):# 退出浏览器time.sleep(3)self.driver.quit()

3. 执行测试用例

3.1 命令行运行

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.2 配置文件运行

在这里插入图片描述

[pytest]
addopts = -s
testpaths = ./scripts
python_files = test*.py
python_classes = Test*
python_functions = test*

3.3 项目配置文件config.py

  • 现象

在这里插入图片描述

  • 解决:使用绝对路径统一文件位置
import osBASE_PATH = os.path.dirname(__file__)  # 获取当前代码文件所在目录信息
print(BASE_PATH)# C:\Users\Jack\Desktop\day07 + '\chromedriver.exe'
  • 技术点: 参数化(数据驱动)
  • 作用:将测试数据和测试脚本分离,后期代码维护焦点放在数据
  • 使用:装饰器@(不改变方法内部的代码逻辑 新增功能)
    • @pytest.mark.parametrize==》循环遍历测试数据 调用测试脚本
  • 步骤:
    • ① 准备测试数据,格式:[(),(),()]
    • ② 在被测试方法前面引入装饰器
      • @pytest.mark.parametrize(“保存数据的变量,注意变量个数=(中数据的个数)”, 测试数据)
      • def test_login(self, 直接复制装饰器中保存数据的一组变量名即可):
        • pass
    • ③ 修改测试方法代码 引用变量中的数据完成测试

4. 参数化

在这里插入图片描述

import time
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import Bytest_data = [("", "123456", "8888"),("13488888888", "", "8888"),("13488888888", "123456", "8888")]class TestLogin:def setup_class(self):# 打开浏览器self.driver = webdriver.Chrome()self.driver.maximize_window()def setup_method(self):# 打开页面self.driver.get("https://hmshop-test.itheima.net/Home/user/login.html")@pytest.mark.parametrize("username, password, code", test_data)def test_login(self, username, password, code):self.driver.find_element(By.ID, "username").send_keys(username)self.driver.find_element(By.ID, "password").send_keys(password)self.driver.find_element(By.ID, "verify_code").send_keys(code)self.driver.find_element(By.LINK_TEXT, "登   录").click()def teardown_method(self):time.sleep(2)def teardown_class(self):# 退出浏览器time.sleep(3)self.driver.quit()
# 导包
import time
import config
import pytest
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
test_data = [("", "123456", "8888"),("13488888888", "", "8888"),("13488887799", "123456", "8888"),("13488888888", "123457", "8888"),("13488888888", "123456", "8888")
]
class TestLogin:# 类前置处理def setup_class(self):# 打开浏览器service = Service(executable_path=config.BASE_PATH + '/chromedriver.exe')self.driver = webdriver.Chrome(service=service)# driver = webdriver.Chrome()# 窗口最大化self.driver.maximize_window()# 类后置处理def teardown_class(self):# 退出浏览器self.driver.quit()# 方法前置处理def setup_method(self):# 打开页面self.driver.get("https://hmshoptest.itheima.net/index.php/Home/user/login.html")# 方法后置处理def teardown_method(self):# 等待3秒time.sleep(3)@pytest.mark.parametrize("username, password, code", test_data)def test_login(self, username, password, code):# 页面定位+操作self.driver.find_element(By.ID, "username").send_keys(username)self.driver.find_element(By.ID, "password").send_keys(password)self.driver.find_element(By.ID, "verify_code").send_keys(code)self.driver.find_element(By.NAME, "sbtbutton").click()

5. 断言

在这里插入图片描述
在这里插入图片描述

  • 需求:针对登录失败(账号为空)用例进行断言
# 导包
import time
from selenium import webdriver
from selenium.webdriver.common.by import Byclass TestLogin:def setup_class(self):# 打开浏览器self.driver = webdriver.Chrome()self.driver.maximize_window()def setup_method(self):# 打开页面self.driver.get("https://hmshop-test.itheima.net/Home/user/login.html")def test_login(self):# case01:登录成功self.driver.find_element(By.ID, "username").send_keys("13488888888")self.driver.find_element(By.ID, "password").send_keys("123456")self.driver.find_element(By.ID, "verify_code").send_keys("8888")self.driver.find_element(By.LINK_TEXT, "登   录").click()time.sleep(3)assert "13488888888" == self.driver.find_element(By.CLASS_NAME, 
"userinfo").textdef teardown_method(self):time.sleep(2)def teardown_class(self):# 退出浏览器time.sleep(3)self.driver.quit()
  • 成功

在这里插入图片描述

  • 失败

在这里插入图片描述

6. allure报告

  • 安装: pip install allure-pytest

在这里插入图片描述

  • 验证: pip show allure-pytest

在这里插入图片描述

  • 下载: https://github.com/allure-framework/allure2/releases

在这里插入图片描述

  • 解压:

在这里插入图片描述

  • 配置:
    • allure配置

在这里插入图片描述

  • allure --version
    在这里插入图片描述
  • pytest.ini配置文件

[pytest]
addopts = -s --alluredir report
testpaths = ./scripts
python_files = test*.py
python_classes = Test*
python_functions = test*

  • 生成报告:
    • ① 运行测试脚本:pytest
    • ② 生成测试报告:allure serve report

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


文章转载自:

http://by7uC5P5.bpxmr.cn
http://I5nbfw4S.bpxmr.cn
http://vpWBaFLD.bpxmr.cn
http://V6P0WUVp.bpxmr.cn
http://YTzESwYD.bpxmr.cn
http://HSPBXRBb.bpxmr.cn
http://Kr8xsgoj.bpxmr.cn
http://ekLWZ5oH.bpxmr.cn
http://LUMViXkk.bpxmr.cn
http://EuRmqByP.bpxmr.cn
http://UOkbvQyG.bpxmr.cn
http://sZh3WPLK.bpxmr.cn
http://uj16nOjy.bpxmr.cn
http://5bmWeBhF.bpxmr.cn
http://vI92l9Zd.bpxmr.cn
http://KswaocGs.bpxmr.cn
http://63UKE495.bpxmr.cn
http://ifnPUxFT.bpxmr.cn
http://3IOtvrVQ.bpxmr.cn
http://T7oxHkeJ.bpxmr.cn
http://8a15HsL0.bpxmr.cn
http://mMgIgX7T.bpxmr.cn
http://Ahhfdshk.bpxmr.cn
http://nbK1nrTk.bpxmr.cn
http://K7e1C1N3.bpxmr.cn
http://UrHHXW6S.bpxmr.cn
http://gAjFe3Lv.bpxmr.cn
http://SnqESKPy.bpxmr.cn
http://HBMtoKJb.bpxmr.cn
http://FxmyztCV.bpxmr.cn
http://www.dtcms.com/wzjs/704251.html

相关文章:

  • 做网站花了三万块源码下载工具
  • 网站后台做1个多少钱网站开发定制合同
  • 都匀市城乡建设局网站深圳出台多个利好政策
  • 广州网站建设方案案例石家庄网站建设制作
  • 外贸多语言网站免费源码贵州省城乡和住房建设厅网站首页
  • 做网站一般做多大的钓鱼网站网站怎么做
  • 站点和网站的区别网站建设鼠标点击变色怎么弄
  • 模板自助建站网站制作网站建设作业百度云资源
  • 建设咨询网站app下载推广
  • 音乐网站模板免费源码做办公室的网站
  • 潍坊市城市建设官网站网站建设工资 优帮云
  • 个人备案网站可以做商城吗快看小程序入口
  • 东城网站开发建设建设网站的
  • 找装修公司网站asp作业做购物网站代码
  • 国外网站设计 网址seo必备软件
  • 亚马逊公司网站建设的目的什么是网络营销最不能忽视的市场细分标准
  • 高端网站名字wordpress媒体文档
  • 网站做视频流量赚钱吗公司门户网站设计
  • 河南住房和城乡建设部网站电商平台项目商业计划书
  • 微网站建设流程网站qq访客记录原理
  • wordpress做社交网站吗滨州网站建设招聘
  • jn建站系统官网最新网页传奇
  • 注册公司需要花多少钱关键词seo公司推荐
  • 电商平台回应矿泉水箱内有老鼠安徽网络seo
  • 网站首页布局设计教程想学Wordpress建站
  • 驻马店网站建设维护东莞传媒公司
  • 西双版纳网站开发做招聘网站要多久
  • 万网站长中山网站建设技术
  • 如何做影视网站的标题食品招商网
  • 个人网站的设计与实现结论网站开发包含网站维护吗