我正在与Chrome无头的硒测试进行问题

发布于 2025-02-01 19:05:41 字数 1717 浏览 4 评论 0 原文

我正在使用JavaScript使用“ Selenium-Webdriver”:“ 4.0.0-Alpha.7”。当我尝试运行测试用例时,无头传递。该测试只是与镀铬浏览器一起进行,而不是无头。

我有一个看起来像这样的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'
              }
          }
        

,并且我有一个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 




 

人知道为什么没有在capablitiies中设置无头参数?

I am using "selenium-webdriver": "4.0.0-alpha.7" with javascript. When I try to run my test cases headless passing in the .withCapabilities . The test just runs with the chrome browser and not headless.

I have a config.js file that looks like this

// 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

    // 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魂牵梦绕锁你心扉 2025-02-08 19:05:41

从Selenium 4开始(和IIRC,Selenium 3系列的一些后期版本)供应商特定的参数已被命名为空间,,扩展功能“ rel =“ nofollow noreferrer”>。

现在需要根据 GOOG 名称空间提供参数,例如 goog:chromeoptions 。

的选项

可以 用以下内容替换您的配置:

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

一个更好的想法

selenium-webdriver library具有将Chrome(和Chromium)设置为内置的选项无头模式。这样做的方式将意味着您不必跟踪所需的参数值,而IMO有点干净。

只需更新您的驱动程序:

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

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.

Arguments now need to be provided to the driver under the goog namespace, eg 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. Doing things this way will mean you don't have to keep track of the argument values needed, and is IMO a bit cleaner.

Just update your driverFactory:

      case "headless":
        builder.withCapabilities(this.config.headless)
            .setChromeOptions(new chrome.Options().headless());
        break;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文