Skip to content

命令行运行脚本

通过Selenium命令行运行器(Command-line Runner)​,Selenium IDE创建的所有测试可以运行在所有浏览器上。

命令行运行器还支持并行运行,也支持在Selenium Grid上运行。

官方文档: https://www.selenium.dev/selenium-ide/docs/en/introduction/command-line-runner

安装

安装nodejs运行环境,这个不多说,自行安装

node: v16.16.0

npm install -g selenium-side-runner

查看版本

selenium-side-runner --version

4.0.13

查看支持的参数

shell
selenium-side-runner
Usage: selenium-side-runner [options] your-project-glob-here-*.side [variadic-project-globs-*.side]

Options:
  -V, --version                                   output the version number
  --base-url [url]                                Override the base URL that was set in the IDE
  -c, --capabilities [list]                       Webdriver capabilities
  -j, --jest-options [list]                       Options to configure Jest, wrap in extra quotes to allow shell to process (default: "\"\"")
  -s, --server [url]                              Webdriver remote server
  -r, --retries [number]                          Retry tests N times on failures, thin wrapper on jest.retryTimes (default: 0)
  -f, --filter [string]                           Run suites matching name, takes a regex without slashes, eg (^(hello|goodbye).*$)
  -w, --max-workers [number]                      Maximum amount of workers that will run your tests, defaults to number of cores
  -t, --timeout [number]                          The maximimum amount of time, in milliseconds, to spend attempting to locate an element. (default: 15000) (default: 15000)
  -T, --jest-timeout [number]                     The maximimum amount of time, in milliseconds, to wait for a test to finish. (default: 60000) (default: 60000)
  -x, --proxy-type [type]                         Type of proxy to use (one of: direct, manual, pac, socks, system)
  -y, --proxy-options [list]                      Proxy options to pass, for use with manual, pac and socks proxies
  -n, --config-file [filepath]                    Use specified YAML file for configuration. (default: .side.yml)
  -o, --output-directory [directory]              Write test results as json to file in specified directory. Name will be based on timestamp.
  -z, --screenshot-failure-directory [directory]  Write screenshots of failed tests to file in specified directory. Name will be based on test + timestamp.
  -f, --force                                     Forcibly run the project, regardless of project's version
  -d, --debug                                     Print debug logs
  -D, --debug-startup                             Print debug startup logs
  -X, --debug-connection-mode                     Debug driver connection mode
  -h, --help                                      display help for command

运行

selenium-side-runner --test test.side

指定浏览器

shell
-c "browserName=chrome"
-c "browserName='internet explorer'"
-c "browserName=edge"
-c "browserName=firefox" 
 -c "browserName=safari"

执行结果

info: Running test demo1
info: Finished test demo1 Success
 PASS  ../.nvm/versions/node/v16.16.0/lib/node_modules/selenium-side-runner/dist/main.test.js (34.62 s)
  Running project diyai.cn
    Running suite Default Suite
      ✓ Running test demo1 (34071 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        34.833 s, estimated 35 s
Ran all test suites within paths "/Users/wangxiaomin/.nvm/versions/node/v16.16.0/lib/node_modules/selenium-side-runner/dist/main.test.js".

常见问题

1、缺少浏览器驱动

js
info: Running test demo1
 FAIL  ../.nvm/versions/node/v16.16.0/lib/node_modules/selenium-side-runner/dist/main.test.js (75.979 s)
  Running project diyai.cn
    Running suite Default Suite
      ✕ Running test demo1 (75326 ms)

  ● Running project diyai.cn › Running suite Default Suite › Running test demo1

    Unable to obtain browser driver.
            For more information on how to install drivers see
            https://www.selenium.dev/documentation/webdriver/troubleshooting/errors/driver_location/. Error: Error executing command for /Users/wangxiaomin/.nvm/versions/node/v16.16.0/lib/node_modules/selenium-side-runner/node_modules/selenium-webdriver/bin/macos/selenium-manager with --browser,firefox,--language-binding,javascript,--output,json: error sending request for url (https://github.com/mozilla/geckodriver/releases/download/v0.36.0/geckodriver-v0.36.0-macos-aarch64.tar.gz)

      at getBinaryPaths (node_modules/selenium-webdriver/common/driverFinder.js:38:11)
      at Function.createSession (node_modules/selenium-webdriver/firefox.js:558:45)
      at createDriver (node_modules/selenium-webdriver/index.js:153:33)
      at Builder.build (node_modules/selenium-webdriver/index.js:682:16)
      at WebDriverExecutor.getDriverSync (node_modules/@seleniumhq/side-runtime/src/webdriver.ts:186:44)
      at driver (node_modules/@seleniumhq/side-runtime/src/webdriver.ts:205:14)
      at WebDriverExecutor.getDriver (node_modules/@seleniumhq/side-runtime/src/webdriver.ts:195:28)
      at WebDriverExecutor.init (node_modules/@seleniumhq/side-runtime/src/webdriver.ts:259:32)
      at Playback.init (node_modules/@seleniumhq/side-runtime/src/playback.ts:144:25)
      at Playback.play (node_modules/@seleniumhq/side-runtime/src/playback.ts:167:46)
      at src/run.ts:181:11

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        76.028 s

mac平台解决方案:

brew install geckodriver

各浏览器安装驱动方法: https://www.selenium.dev/selenium-ide/docs/en/introduction/command-line-runner#installing-a-browser-driver

需要自行解决浏览器和驱动的兼容问题

比如

firefox: 136.0.4 (aarch64)
geckodriver: 0.36.0

修改基础url

通过“--base-url 地址”参数,可以指定当前的运行环境

shell
selenium-side-runner --base-url http://localhost:8080/ --test test.side