当前位置: 首页 > 科技观察

SeleniumIDE命令行运行器

时间:2023-03-12 05:30:22 科技观察

1.前言SeleniumIDE通常从Chrome或Firefox的Web应用程序商店安装。安装完成后,在浏览器菜单栏点击它的图标启动它,执行脚本Recordandwrite,然后回放即可执行脚本。本文介绍了如何安装SeleniumIDE命令行运行器、获取必要的浏览器驱动程序(如果在本地运行测试)以及使用所需选项从命令提示符启动运行器。二、安装1、首先需要安装Node.js(8或10版本)。2、然后打开命令行,安装selenium-side-runner(SeleniumIDE命令行运行程序)。npminstall-gselenium-side-runner3.安装浏览器驱动程序Selenium通过称为浏览器驱动程序的小型二进制应用程序与每个浏览器通信。每个浏览器都有自己的驱动程序,可以手动下载并添加到系统路径,或者使用包管理器安装最新版本的浏览器驱动程序(推荐)。(1)对于Chrome,ChromeDriver是必需的。下载地址:https://chromedriver.chromium.org/。从命令行安装驱动程序:npminstall-gchromedriver(2)对于Edge,需要EdgeDriver。下载链接:https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/。从命令行安装驱动程序:npminstall-gededriver(3)对于Firefox,需要geckodriver驱动程序。下载地址:https://github.com/mozilla/geckodriver。从命令行安装驱动程序:npminstall-ggeckodriver(4)对于IE,需要IEDriver驱动程序。下载地址:https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver。从命令行安装驱动程序:npminstall-giedriver(5)对于Safari,需要SafariDriver驱动程序。下载链接:https://developer.apple.com/documentation/webkit/testing_with_webdriver_in_safari。3.运行selenium-side-runner命令,然后调用保存的工程文件路径。例如使用SeleniumIDE录制脚本并运行成功,将脚本保存到D盘根目录,如D:\test_demo.side。打开命令行工具,执行以下命令运行脚本。selenium-side-runnerD:\test_demo.side执行成功。如果您有多个.side文件,您可以使用通配符(例如D:\*.side)。当运行多个.side文件时,它会在多个浏览器窗口中并行启动测试,分布在n个进程中(其中n是计算机上可用的CPU内核数)。注意:如果出现如下错误信息,请手动下载驱动并配置环境变量。在当前PATH中找不到ChromeDriver。请从http://chromedriver.storage.googleapis.com/index.html下载最新版本的ChromeDriver并确保它可以在您的PATH中找到。(1)首先查看浏览器版本,下载对应版本的驱动。比如浏览器版本106.0.5249.119。然后驱动程序下载106.0.5249.61。(2)把驱动和本地的chrome.exe放在同一个目录下。(3)在环境变量的PATH中添加chromedriver.exe的同级目录路径,然后保存。四、运行时配置1.在本地指定的不同浏览器上运行。selenium-side-runner-c"browserName=chrome"selenium-side-runner-c"browserName='internetexplorer'"selenium-side-runner-c"browserName=edge"selenium-side-runner-c"browserName=firefox“selenium-side-runner-c”browserName=safari“2。在硒网格上运行。selenium-side-runner--serverhttp://localhost:4444/wd/hub-c"browserName='chrome'versinotallow='106.0'platform='Windows10'"3.指定SeleniumGrid上的并行进程数在运行时,您可能希望使用-wn命令标志(其中n是所需的进程数)来控制正在运行的并行会话的数量。selenium-side-runner-w10--serverhttp://localhost:4444/wd/hub4、Chrome特定功能如果你的Chrome安装在你电脑的非标准位置,你可以指定路径让ChromeDriver知道要查找什么位置。selenium-side-runner-c"goog:chromeOptions.binary='/path/to/non-standard/Chrome/install'"无头运行测试。selenium-side-runner-c"goog:chromeOptions.args=[disable-infobars,headless]"5.应用于框架1.更改基本URL。通过指定不同的基本URL,可以轻松地将测试指向不同的环境(例如开发、测试、生产)。selenium-side-runner--base-urlhttps://localhost2.过滤器测试可以选择使用--filtertarget命令标志(其中target是正则表达式值)运行测试的目标子集。仅运行包含给定搜索条件的测试名称。selenium-side-runner--filtersmoke3.将测试结果输出到文件如果需要将测试结果导出到文件(例如,作为CI进程的一部分运行时),可以使用--output-directory和-输出格式。--output-directory定义了测试结果文件的位置,可以是绝对路径也可以是相对路径。--output-format定义测试结果文件使用的格式,可以是jest(如JSON)或junit(如XML),默认格式为jest(不指定类型)。selenium-side-runner--output-directory=resultselenium-side-runner--output-directory=results--output-format=jestselenium-side-runner--output-directory=results--output-format=junit4,指定默认配置可以将运行时参数存储在配置文件中,而不是记住所有需要的命令行参数。有两种配置方法。方法一:在测试运行的目录下创建.side.yml文件,selenium-side-runner会自动识别。以下是文件内容的示例。capabilities:browserName:"firefox"baseUrl:"https://www.seleniumhq.org"server:"http://localhost:4444/wd/hub"如果你想在运行时忽略这个文件并使用命令行参数将--no-sideyml与其他命令一起使用。方法2:代替.side.yml文件,您可以在YAML文件中指定运行时参数并选择名称和位置,然后在运行测试时指定其位置。selenium-side-runner--config-file"/path/to/your/config.yaml"六、高级选项1.参数指定你自己独特的运行时参数,可以通过--params标志使用。(1)基本用法指定参数的名称和值,最基本的方法是指定一个字符串值。selenium-side-runner--params"a='example-value'"(2)嵌套参数parameters可以用点表示嵌套。selenium-side-runner--params"a.b='anotherexample-value'"(3)数组值可以指定字母数字数组。selenium-side-runner--params"a.b.c=[1,2,3]"(4)多个参数--params只能调用一次,但可以指定多个参数,用空格隔开。selenium-side-runner--params"a='example-value'a.b='anotherexample-value'a.b.c=[1,2,3]"2.使用代理服务器可以在运行程序中使用如下选项来代理传递给浏览器的函数。(1)DirectProxy该选项配置WebDriver绕过所有浏览器代理。从命令行:selenium-side-runner--proxy-type=direct在.side.yaml文件中:proxyType:direct(2)手动代理手动配置浏览器代理。从命令行:selenium-side-runner--proxy-type=manual--proxy-options="http=localhost:434bypass=[http://localhost:434,http://localhost:8080]"在.在side.yaml文件中:proxyType:manualproxyOptions:http:http://localhost:434https:http://localhost:434ftp:http://localhost:434bypass:-http://localhost:8080-http://host:434-http://somethingelse:32(3)PAC代理配置WebDriver使用给定URL的PAC文件来设置浏览器代理。从命令行:selenium-side-runner--proxy-type=pac--proxy-options="http://localhost/pac"在.side.yaml文件中:proxyType:pacproxyOptions:http://localhost/pac(4)SOCKS代理为SOCKS代理创建代理配置。从命令行:selenium-side-runner--proxy-type=socks--proxy-options="socksProxy=localhost:434socksVersinotallow=5"在.side.yaml文件中:proxyType:socksproxyOptions:socksProxy:localhost:434socksVersion:5(5)SystemProxy配置WebDriver使用当前系统的代理。从命令行:selenium-side-runner--proxy-type=system在.side.yaml文件中:proxyType:system