Pytest-selenium简单使用示例

栏目: 编程工具 · 发布时间: 7年前

内容简介:Pytest-selenium是Pytest的一个插件, 安装方法:pip3 install pytest pytest-selenium用例编写时只需要在测试函数中传入selenium参数作为driver使用即可, 如下:

Pytest-selenium是Pytest的一个插件, 安装方法:

pip3 install pytest pytest-selenium

用例编写时只需要在测试函数中传入selenium参数作为driver使用即可, 如下:

# test_baidu.py

def test_baidu(selenium):

selenium.get("https://www.linuxidc.com")  # selenium即driver

然后在运行时指定通过--driver指定浏览器即可:

pytest test_baidu.py --driver Chrome

需要提前配置好selenium环境及chromedriver

下面是一个Django后台添加课程的简单使用示例:

# test_add_course.py

import pytest

from selenium.webdriver.support.select import Select

from selenium.webdriver.support.expected_conditions import presence_of_element_located as element_exist

@pytest.fixture  # 指定为fixture方法(测试准备/清理方法)

def login(selenium):  # selenium为使用pytest-selenium固定参数, 相当于driver, 运行时通过--driver指定浏览器

selenium.get("http://linuxidc.com:8000/admin/")

selenium.find_element_by_id("id_username").send_keys("hanzhichao")

selenium.find_element_by_id("id_password").send_keys("hanzhichao123")

selenium.find_element_by_class_name("submit-row").click()

@pytest.fixture

def del_course(selenium):  # 用于测试完删除新建的文章

yield  # yield以下为teardown清理代码, 用例执行完运行

selenium.find_element_by_link_text("Python接口测试教程").click()

selenium.find_element_by_class_name("deletelink").click()

selenium.find_element_by_css_selector("input[type='submit']").click()

def test_add_course(selenium, login, del_course):  # 通过参数使用fixtures方法login及del_course

selenium.find_element_by_link_text("Courses").click()  # 点击'Courses'链接

selenium.find_element_by_class_name("addlink").click()  # 点击'新增 COURSE'按钮

Select(selenium.find_element_by_id("id_owner")).select_by_visible_text("hanzhichao")  # 选择作者

Select(selenium.find_element_by_id("id_subject")).select_by_visible_text("接口测试")  # 选择主题

selenium.find_element_by_id("id_title").send_keys("Python接口测试教程")  # 输入文章标题

selenium.find_element_by_id("id_overview").send_keys("作者:临渊")  # 输入描述

selenium.find_element_by_class_name("default").click()  # 点击保存

assert element_exist(("link text", "Python接口测试教程"))  # 断言返回的页面存在标题名的链接

运行方法:

pytest test_add_course.py --driver Chrome

Linux公社的RSS地址https://www.linuxidc.com/rssFeed.aspx

本文永久更新链接地址: https://www.linuxidc.com/Linux/2019-06/158957.htm


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

疯长

疯长

[美]肖恩· 阿美拉蒂 / 中信出版集团 / 2018-10 / 45

实现财务回报以及扩大影响力是企业家长期关注和讨论的问题。 为什么有些公司实现了10倍的投资回报,而其他的则勉力支撑?产品类似的公司,为什么有的家喻户晓,有的默默无闻直至退出市场…… 为了了解真相,作者阿美拉蒂在这本书中精选10组对照公司,比如,同为社交平通的Facebook(脸谱网)和Friendster(交友网),同为快餐领域先驱的麦当劳和白色城堡,再比如都在开发电动汽车市场的特斯拉......一起来看看 《疯长》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试