起步
安装SeleniumBase(以下简称sbase)
- 安装PythonheGit
- 推荐使用Python 虚拟环境
使用 PyPI或者GitHub上安装seleniumbase:
使用PyPI安装seleniumbase:
xxxxxxxxxx
pip install seleniumbase
- –upgrade / –u 升级 SeleniumBase
- –force-reinstall 升级非直接的安装包
- Python 3.x版本以上,使用pip3安装
使用GitHub clone 安装seleniumbase:
xxxxxxxxxx
git clone https://github.com/seleniumbase/SeleniumBase.git
cd SeleniumBase/
pip install -e .
使用github clone中升级当前的安装:
xxxxxxxxxx
git pull
pip install -e
输入seleniumbase 或者 sbase 来验证 seleniumbase是否被成功安装
成功界面如下:
xxxxxxxxxx
___ _ _ ___
/ __| ___| |___ _ _ (_)_ _ _ __ | _ ) __ _ ______
\__ \/ -_) / -_) ' \| | \| | ' \ | _ \/ _` (_-< -_)
|___/\___|_\___|_||_|_|\_,_|_|_|_\|___/\__,_/__|___|
----------------------------------------------------
╭──────────────────────────────────────────────────╮
│ * USAGE: "seleniumbase [COMMAND] [PARAMETERS]" │
│ * OR: "sbase [COMMAND] [PARAMETERS]" │
│ │
│ COMMANDS: PARAMETERS / DESCRIPTIONS: │
│ get / install [DRIVER_NAME] [OPTIONS] │
│ methods (List common Python methods) │
│ options (List common pytest options) │
│ behave-options (List common behave options) │
│ gui / commander [OPTIONAL PATH or TEST FILE] │
│ behave-gui (SBase Commander for Behave) │
│ caseplans [OPTIONAL PATH or TEST FILE] │
│ mkdir [DIRECTORY] [OPTIONS] │
│ mkfile [FILE.py] [OPTIONS] │
│ mkrec / codegen [FILE.py] [OPTIONS] │
│ recorder (Open Recorder Desktop App.) │
│ record (If args: mkrec. Else: App.) │
│ mkpres [FILE.py] [LANG] │
│ mkchart [FILE.py] [LANG] │
│ print [FILE] [OPTIONS] │
│ translate [SB_FILE.py] [LANG] [ACTION] │
│ convert [WEBDRIVER_UNITTEST_FILE.py] │
│ extract-objects [SB_FILE.py] │
│ inject-objects [SB_FILE.py] [OPTIONS] │
│ objectify [SB_FILE.py] [OPTIONS] │
│ revert-objects [SB_FILE.py] [OPTIONS] │
│ encrypt / obfuscate │
│ decrypt / unobfuscate │
│ proxy (Start a basic proxy server) │
│ download server (Get Selenium Grid JAR file) │
│ grid-hub [start|stop] [OPTIONS] │
│ grid-node [start|stop] --hub=[HOST/IP] │
│ │
│ * EXAMPLE => "sbase get chromedriver stable" │
│ * For command info => "sbase help [COMMAND]" │
│ * For info on all commands => "sbase --help" │
╰──────────────────────────────────────────────────╯
下载web驱动(webdrivers):
sbase会自动的下载web驱动,例如chromedriver 谷歌驱动
谷歌驱动示例:
xxxxxxxxxx
*** chromedriver to download = 131.0.6778.108 (Latest Stable)
Downloading chromedriver-mac-arm64.zip from:
https://storage.googleapis.com/chrome-for-testing-public/131.0.6778.108/mac-arm64/chromedriver-mac-arm64.zip ...
Download Complete!
Extracting ['chromedriver'] from chromedriver-mac-arm64.zip ...
Unzip Complete!
The file [chromedriver] was saved to:
~/github/SeleniumBase/seleniumbase/drivers/
chromedriver
Making [chromedriver 131.0.6778.108] executable ...
[chromedriver 131.0.6778.108] is now ready for use!
基本案例/使用:
如果你已经克隆了sbase,你可以从 examples/ 文件夹里面运行测试
示例:my_first_test.py:
xxxxxxxxxx
cd examples/
pytest my_first_test.py
完整代码:
xxxxxxxxxx
from seleniumbase import BaseCase
BaseCase.main(__name__, __file__)
class MyTestClass(BaseCase):
def test_swag_labs(self):
self.open("https://www.saucedemo.com")
self.type("#user-name", "standard_user")
self.type("#password", "secret_sauce\n")
self.assert_element("div.inventory_list")
self.assert_exact_text("Products", "span.title")
self.click('button[name*="backpack"]')
self.click("#shopping_cart_container a")
self.assert_exact_text("Your Cart", "span.title")
self.assert_text("Backpack", "div.cart_item")
self.click("button#checkout")
self.type("#first-name", "SeleniumBase")
self.type("#last-name", "Automation")
self.type("#postal-code", "77123")
self.click("input#continue")
self.assert_text("Checkout: Overview")
self.assert_text("Backpack", "div.cart_item")
self.assert_text("29.99", "div.inventory_item_price")
self.click("button#finish")
self.assert_exact_text("Thank you for your order!", "h2")
self.assert_element('img[alt="Pony Express"]')
self.js_click("a#logout_sidebar_link")
self.assert_element("div#login_button_container")
- 默认地,使用CSS 选择器来寻找网页元素
- 对于CSS选择器新手来说,像CSS Diner这样的游戏可以帮助你学习
- 如果需要深入阅读,这里有一个关于CSS属性选择器的高级指南
这里有一些常见的Sbase函数
xxxxxxxxxx
self.open(url) # 使浏览器窗口导向URL链接
self.type(selector, text) # 使用text更新对于文本域
self.click(selector) # 点击选择器定位的元素
self.click_link(link_text) # 点击包含文本的链接
self.go_back() # 返回至前一个URL地址
self.select_option_by_text(dropdown_selector, option) # 根据文本选择下拉选项
self.hover_and_click(hover_selector, click_selector) # 悬停点击
self.drag_and_drop(drag_selector, drop_selector) # 拖拽
self.get_text(selector) # 从元素中获取文本内容
self.get_current_url() # 获取当前页面的URL路径
self.get_page_source() # 获取当前页面的HTML代码
self.get_attribute(selector, attribute) # 获取元素属性
self.get_title() # 获取当前页面的title标题
self.switch_to_frame(frame) # 切换至iframe容器
self.switch_to_default_content() # 退出iframe容器
self.open_new_window() # 在同一浏览器中打开新的窗口
self.switch_to_window(window) # 切换浏览器窗口
self.switch_to_default_window() # 切换至起始的浏览器窗口
self.get_new_driver(OPTIONS) # 使用选项开启一个新的驱动
self.switch_to_driver(driver) # 切换浏览器驱动
self.switch_to_default_driver() # 切换到初始的浏览器驱动
self.wait_for_element(selector) # 等待元素可见
self.is_element_visible(selector) # 返回元素可见性
self.is_text_visible(text, selector) # 返回文本可见性
self.sleep(seconds) # 在给定的时间内,什么也不做
self.save_screenshot(name) # 以 .png的格式保存截图
self.assert_element(selector) # 验证元素可见性
self.assert_text(text, selector) # 验证文本text是否在元素中
self.assert_exact_text(text, selector) # 验证文本是否一致
self.assert_title(title) # 验证当前网页的标题名称是否一致
self.assert_downloaded_file(file) # 验证文件是否下载
self.assert_no_404_errors() # 验证是否不存在无效链接 不存在 True 存在 False
self.assert_no_js_errors() # 验证是否不存在JS错误 不存在 True 存在 False
- 完整sbase函数,详见:函数总结
有趣的事实/了解更多
sbase会自动地处理常见的网页驱动行为,如在测试前开启网页浏览器、在失败时保存截图、
并且在测试结束后关闭浏览器
sbase允许你通过命令行选项自定义测试
sbase对于命令使用简单的语法,例如:
xxxxxxxxxx
self.type("input", "dogs\n") # (The "\n" presses ENTER)
大多数的sbase脚本可以使用pytes、pynose 或者纯 python运行。
但并非所有的测试运行器可以运行所有测试格式。
举个例子,使用sb pytest固件的测试只能由pytest运行。详见语法格式
这里是一个 由behave运行的 Gherkin 测试格式 :
pytest coffee_cart_tests.py --rs
pytest test_sb_fixture.py --demo
pytest test_suite.py --rs --html=report.html --dashboard
pynose basic_test.py --mobile
pynose test_suite.py --headless --report --show-report
python raw_sb.pya
python raw_test_scripts.py
behave realworld.feature
behave calculator.feature -D rs -D dashboard
pytest包含了自动地测试发现。如果你没有指定一个特定的文件或者文件夹来运行,pytest会自动地搜寻所有测试子目录来运行,基于以下的标准:
- 以
test_
开头或者以_test.py
结尾的Python文件 - 以
test_
开头的Python方法
使用sbase pytest.ini 文件,你可以调整默认的发现设置。Python类名可以是任何内容,因为 seleniumbase.BaseCase 继承了
unittest.TestCase
触发自动发现功能。- 以
在真正运行之前,你可以进行一个 预冲突检查 来查看哪一个测试会被 pytest发现:
xxxxxxxxxx
pytest --co -q
当对一个文件调用
pytest
或者pynose
来运行时,你可以更明确地使用:xxxxxxxxxx
pytest [FILE_NAME.py]::[CLASS_NAME]::[METHOD_NAME]
pynose [FILE_NAME.py]:[CLASS_NAME].[METHOD_NAME]
pytest [文件名.py]::[类名]::[方法名]
pynose [文件名.py]::[类名]::[方法名]
不再有古怪的测试!在网页可交互之前(却决于超时限制),sbase会自动地等待网页元素加载成功。这意味着你不再需要在你的脚本中添加随机的 time.sleep()语句。
sbase支持所有主流的浏览器以及操作系统
浏览器:Chrome、Edge、Firefox 和Safari
系统:Linux/Ubuntn、macOS 以及 windows
sbase 工作在所有流行的 CI/CD 平台
- GitHub Actions
- Jenkins
- Azure
- Google Cloud
- AWS
- Your Computer
- sbase 包括了一个自动化/手动 混合的解决方案 ,称为 MasterQA ,当手动测试者处理验证的时候,可以通过自动化来加速手动测试
- sbase支持离线运行测试(假设之前在线的时候,网页驱动已经下载完毕)
- sbase完整的特性,点击这里