简体   繁体   English

我在使用 chrome 无头运行 selenium 测试时遇到问题

[英]I am having issues running selenium tests headless with chrome

I am using "selenium-webdriver": "4.0.0-alpha.7" with javascript.我正在使用 "selenium-webdriver": "4.0.0-alpha.7" 和 javascript。 When I try to run my test cases headless passing in the .withCapabilities .当我尝试在 .withCapabilities 中无头地运行我的测试用例时。 The test just runs with the chrome browser and not headless.测试只在 chrome 浏览器上运行,而不是无头的。

I have a config.js file that looks like this我有一个看起来像这样的 config.js 文件

// filename: lib/config.js
     
    module.exports = {
       baseUrl: process.env.BASE_URL || 'https://www.mycompanyURL.com',
       host: process.env.HOST || 'headless',
        headless: {
          "browserName": process.env.BROWSER_NAME || 'chrome'
          "chromeOptions": {
            "args": ['--headless'],
               },
          },
         localhost: {
              "browserName": process.env.BROWSER_NAME || 'chrome'
              }
          }
        

And I have a driveFactory.js that looks like this我有一个看起来像这样的 driveFactory.js

    // filename: DriverFactory.js
    const path = require('path')
    const { Builder } = require('selenium-webdriver')
    
    class DriverFactory {
      constructor(config) {
        this.config = config
      }
    
      _configure() {
        let builder = new Builder()
        switch (this.config.host) {
    
          case 'localhost':
                  builder.withCapabilities(this.config.localhost)
            break
    
          case 'headless':
                    builder.withCapabilities(this.config.headless).
            break
        }
        return builder
      }
  }

module.exports = DriverFactory 




 

Does anyone have any idea why the headless arguments are not being set in the capablitiies?有谁知道为什么没有在能力中设置无头论点?

As of Selenium 4 (and, IIRC, some of the later versions of the Selenium 3 series) vendor-specific arguments have been namespaced, in accordance with the WebDriver Spec .从 Selenium 4(以及 IIRC,Selenium 3 系列的一些较新版本)开始,供应商特定的参数已根据 WebDriver Spec进行命名空间。

Arguments now need to be provided to the driver under the goog namespace, eg goog:chromeOptions .现在需要在goog命名空间下向驱动程序提供参数,例如goog:chromeOptions

An option一个选项

You could replace your config with the following:可以使用以下内容替换您的配置:

        headless: {
          "browserName": process.env.BROWSER_NAME || 'chrome'
          "goog:chromeOptions": {
            "args": ['--headless'],
               },
          },

A better idea一个更好的主意

The selenium-webdriver library has built-in options for setting Chrome (and Chromium, for that matter) into headless mode. selenium-webdriver库具有用于将 Chrome(和 Chromium,就此而言)设置为无头模式的内置选项。 Doing things this way will mean you don't have to keep track of the argument values needed, and is IMO a bit cleaner.以这种方式做事意味着您不必跟踪所需的参数值,并且 IMO 更干净一些。

Just update your driverFactory:只需更新您的驱动程序工厂:

      case "headless":
        builder.withCapabilities(this.config.headless)
            .setChromeOptions(new chrome.Options().headless());
        break;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM