扩展和改进Selenium Python库的库。
PyWebRunner的Python项目详细描述
PyWebRunner
用于selenium的超级python包装器
文档
完整文档可在以下位置找到: https://intuitivewebsolutions.github.io/PyWebRunner
使用
您可以使用webrunner来清理网站、自动化web任务,或者 任何你能想象的。它易于初始化和使用。是的 也与 BrowserStack使用 该页上的命令执行器和远程功能示例。
(请注意,您需要订阅、用户名和api密钥 使其工作。)
安装
pip install PyWebRunner
基本示例
# Import WebRunner if you aren't going to assert anything.# WebTester is a sub-class of WebRunnerfromPyWebRunnerimportWebRunner# Running headless FireFox is the default.wr=WebRunner()# Defaults to xvfb=True, driver=FireFox# If xvfb is not installed, it will be bypassed automatically.# Start the browser instance.wr.start()# Navigate to a page.wr.go('https://www.google.com/')# Fill in a text field.wr.set_value('#lst-ib','PyWebRunner')wr.send_key('#lst-ib','ENTER')# Click the link based on a (gross) CSS selector.wr.click('#rso > div:nth-child(1) > div:nth-child(1) > div > h3 > a')# Wait for the page to load.wr.wait_for_presence('div.document')# Are we there yet?wr.is_text_on_page('A helpful wrapper for Selenium')# True# Take a screenshot!wr.screenshot('/tmp/screenshot1.png')# Stop the browser instance.wr.stop()
yaml脚本
pywebrunner支持运行yaml脚本,并包括webrunner 命令。
假设我们为上面的示例制作了一个yaml脚本,并将其命名为 script.yml
-go:https://www.google.com/-set_value:-"#lst-ib"-PyWebRunner-send_key:-"#lst-ib"-"ENTER"-click:"#rso>div:nth-child(1)>div:nth-child(1)>div>h3>a"-wait_for_presence:div.document-assert_text_on_page:A helpful wrapper for Selenium-screenshot:/tmp/screenshot1.png
我们可以这样运行:
webrunner script.yml
…它的行为与上面基于python的示例相同。
高级yaml功能
yaml支持使用假工厂库(如果已安装) 以及evals和python函数调用。尽管山药不是 作为python脚本的完全替代,这确实使 一些相当灵活的脚本运行。
你可能会问自己,解析yaml的目的是什么 如果您可以编写python并通过 违约?
答案是我想要一种纯数据驱动的前端编写方法 测试这些好处可以概括为:
- 它使得编写一个单独的加载程序脚本成为可能。 将yaml文件放在文件夹中,并一次运行一个(或在 平行)。
- 测试本身可以从一个远程的 网络服务器。
- 测试可以由非程序员编写,只需很少的培训和 努力。
- 可以很容易地创建gui工具来编写测试/自动化任务 不需要任何编程知识。
请考虑以下注册帐户的示例:
# Go to the page.-go:https://somesite/page.html# Click the register link.-click:"#register"# Wait for the registration form.-wait_for_presence:"#email"# Set the email field to a freshly-generated fake email address:-set_value:-"#email"-(( fake-email ))# Create a fake password string.-set_value:-"#password"-(( fake-password ))# Reference the fake password we already generated.-set_value:-"#password-verify"-(( vars|password ))# Click the register button.-click:"#register"# Wait for the redirect and the confirmation div.-wait_for_presence:"#confirmation-div"# Assert that the registration was successful.-assert_text_in_page:Your registration was successful!
fakedata
本例使用伪工厂/伪库生成 伪造的电子邮件地址和密码。任何使用 (())语法自动分配给的变量列表 以后再参考。
安装伪造
在2016年9月15日之前安装:pip install fake-factory
2016年9月15日后安装:pip install faker
假货工厂/假货只需要安装山药来支持 (( fake-* ))标记。
*可以是faker类上的任何方法。这个名单是 广泛且正在积极发展中。有关详细信息,请转到 假冒工厂网站:
https://faker.readthedocs.io/en/latest/
((特殊)
双括号(()中包含的项将在 脚本执行的特殊方式。
示例
-include:basic_setup.yml-import:random.randint-set_value:-"#someinput"-(( randint|1,2 ))
前面的示例将导入random.randint并使用它生成 值1或2,并将其插入someinput元素。
-import:random.choice-set_value:-"#someinput"-(( choice|['frog','cat','bird'] ))
如您所见,choice函数不接受位置参数 就像randint函数一样。它需要一个选项列表。
当我们运行一个函数不止一次并且我们需要 参考第二个或第三个输出?
-import:random.choice-set_value:-"#someinput-a"-(( choice|['frog','cat','bird'] ))# bird-set_value:-"#someinput-b"-(( choice|['frog','cat','bird'] ))# cat-set_value:-"#someinput-c"-(( choice|['frog','cat','bird'] ))# cat (again)-assert_value_of_element:-"#someinput-a"-(( vars|choice ))# The "choice" array. Defaults to index 0.-assert_value_of_element:-"#someinput-b"-(( vars|choice|1 ))# The "choice" array index 1.-assert_value_of_element:-"#someinput-c"-(( vars|choice|2 ))# The "choice" array index 2.
好的,页面本身的值如何?有没有办法得到 从yaml webrunner脚本中引用它们?
-value_of:"#my-input-element"# Set the value.-set_value:-"#someinput-a"-(( vars|value_of|0 ))# Use the value at index 0.-text_of:"#my-div-element"# Set the text to a variable.-set_value:-"#someinput-b"-(( vars|text_of|0 ))# Use the text value at index 0.
BrowserStack示例:
此库还对BrowserStack提供一流的支持。使用它是 和上面的例子没什么不同。
fromPyWebRunnerimportWebRunner# Change any of these values to valid ones.desired={'browser':'Edge','browser_version':'13.0','os':'Windows','os_version':'10','resolution':'1440x900'}# Make sure you plug in your own USERNAME and API_KEY values here.wr=WebRunner(desired_capabilities=desired,command_executor='http://USERNAME:API_KEY@hub.browserstack.com:80/wd/hub',driver='Remote')wr.start()wr.go('http://google.com')# ... Etc.
测试
西ebtester
WebTester继承了WebRunner,因此它拥有 webrunner有,但它添加了一些额外的方法,这些方法对于 测试。
测试断言
- assert_alert_不存在
- 断言警报存在
- 已选中“断言”
- assert_element_包含文本
- assert_element_有_类
- assert_element_not_has_class
- 找到断言
- 未勾选“断言”
- 找不到断言
- 断言不可见
- 在元素中断言文本
- 在元素中断言文本
- 在页面中断言文本
- 断言页面中的文本不是
- 断言URL
- 断言元素的值
- 断言可见
找不到文件“changes”。