我的 Nightwatch.conf.js 文件有问题吗?

Is There a Problem with my Nightwatch.conf.js file?

我一直在尝试为我的工作设置一个自动化测试框架,而 nodejs 测试似乎是未来可扩展性的最佳选择。另外,我正在尝试将自己扩展到 QA 的新领域。

我已经使用 npm install 命令设置了 Nightwatch 框架,并手动安装了最新的 chrome 驱动程序和 selenium 独立可执行文件。我试图确保我的 nightwatch.conf.js 文件已完全设置为 运行 我使用 google chrome 进行的第一次测试,但我收到此错误:

⚠ Error connecting to localhost on port 4444.
_________________________________________________

TEST FAILURE: 1 error during execution; 0 tests failed, 0 passed (249ms)

 ✖ GoogleTest
   An error occurred while retrieving a new session
Error: An error occurred while retrieving a new session
    at endReadableNT (internal/streams/readable.js:1327:12)
    at processTicksAndRejections (internal/process/task_queues.js:80:21)

  Error: An error occurred while retrieving a new session
       at endReadableNT (internal/streams/readable.js:1327:12)
       at processTicksAndRejections (internal/process/task_queues.js:80:21)

   SKIPPED:
   - Basic Google search

这似乎很笼统。按照正常的 Nightwatch 设置,我创建了一个名为 'Automation' 的根文件夹,我将我的 bin 文件夹用于驱动程序和 selenium 框架。在 'Automation' 文件夹中,我还有我设置的 nightwatch.conf.js 文件(如果我将其代码粘贴到此处,请告诉我它是否更有帮助)。为了 运行 我的测试,我使用了 npx nightwatch 命令,但我继续得到这个错误。

据我所知,这可能是因为我试图 运行 在本地进行测试,但我想不出一种方法来配置 selenium 服务以接受本地端口,除了如何已设置。

我不知道这是否是对问题的格式正确的描述,所以如果这非常含糊,我提前道歉。如果还有什么我可以帮助清理它的,请告诉我。

谢谢!

更新Conf.js文件

// Autogenerated by Nightwatch
// Refer to the online docs for more details: https://nightwatchjs.org/gettingstarted/configuration/
const Services = {}; loadServices();

module.exports = {
  // An array of folders (excluding subfolders) where your tests are located;
  // if this is not specified, the test source must be passed as the second argument to the test runner.
  src_folders: ["tests"],

  // See https://nightwatchjs.org/guide/working-with-page-objects/
  page_objects_path: '',

  // See https://nightwatchjs.org/guide/extending-nightwatch/#writing-custom-commands
  custom_commands_path:  '',

  // See https://nightwatchjs.org/guide/extending-nightwatch/#writing-custom-assertions
  custom_assertions_path: '',

  // See https://nightwatchjs.org/guide/#external-globals
  globals_path : '',

  webdriver: {},

  test_settings: {
    default: {
      disable_error_log: false,
      launch_url: 'https://nightwatchjs.org',

      screenshots: {
        enabled: false,
        path: 'screens',
        on_failure: true
      },

      desiredCapabilities: {
        browserName : 'chrome'
      },

      webdriver: {
          start_process: true,
          server_path: (Services.chromedriver ? Services.chromedriver.path : '.Automation/bin/chromedriver.exe')
      }
    },

    safari: {
      desiredCapabilities : {
        browserName : 'safari',
        alwaysMatch: {
          acceptInsecureCerts: false
        }
      },
      webdriver: {
        port: 4444,
        start_process: true,
        server_path: '/usr/bin/safaridriver'
      }
    },

    firefox: {
      desiredCapabilities : {
        browserName : 'firefox',
        alwaysMatch: {
          // Enable this if you encounter unexpected SSL certificate errors in Firefox
          // acceptInsecureCerts: true,
          'moz:firefoxOptions': {
            args: [
              // '-headless',
              // '-verbose'
            ],
          }
        }
      },
      webdriver: {
        start_process: true,
        port: 4444,
        server_path: (Services.geckodriver ? Services.geckodriver.path : './Automation/bin/geckodriver.exe'),
        cli_args: [
          // very verbose geckodriver logs
          // '-vv'
        ]
      }
    },

    chrome: {
      desiredCapabilities : {
        browserName : 'chrome',
        chromeOptions : {
          // This tells Chromedriver to run using the legacy JSONWire protocol (not required in Chrome 78)
          w3c: true,
          //More info on Chromedriver: https://sites.google.com/a/chromium.org/chromedriver/
            args: ['disable-gpu', 'no-sandbox', 'headless']
        }
      },

      webdriver: {
        start_process: true,
        port: 9515,
        server_path: (Services.chromedriver ? Services.chromedriver.path : './Automation/bin/chromedriver.exe'),
        cli_args: [
          // --verbose
        ]
      }
    },

    //////////////////////////////////////////////////////////////////////////////////
    // Configuration for when using the browserstack.com cloud service               |
    //                                                                               |
    // Please set the username and access key by setting the environment variables:  |
    // - BROWSERSTACK_USER                                                           |
    // - BROWSERSTACK_KEY                                                            |
    // .env files are supported                                                      |
    //////////////////////////////////////////////////////////////////////////////////
    browserstack: {
      selenium: {
        host: 'hub-cloud.browserstack.com',
        port: 443
      },
      // More info on configuring capabilities can be found on:
      // https://www.browserstack.com/automate/capabilities?tag=selenium-4
      desiredCapabilities: {
        'bstack:options' : {
          local: 'false',
          userName: '${BROWSERSTACK_USER}',
          accessKey: '${BROWSERSTACK_KEY}',
        }
      },

      disable_error_log: true,
      webdriver: {
        keep_alive: true,
        start_process: false
      }
    },

    'browserstack.chrome': {
      extends: 'browserstack',
      desiredCapabilities: {
        browserName: 'chrome',
        chromeOptions : {
          // This tells Chromedriver to run using the legacy JSONWire protocol
          // More info on Chromedriver: https://sites.google.com/a/chromium.org/chromedriver/
            w3c: true,
            args: ['disable-gpu', 'no-sandbox', 'headless']
        }
      }
    },

    'browserstack.firefox': {
      extends: 'browserstack',
      desiredCapabilities: {
        browserName: 'firefox'
      }
    },

    'browserstack.ie': {
      extends: 'browserstack',
      desiredCapabilities: {
        browserName: 'IE',
        browserVersion: '11.0',
        'bstack:options' : {
          os: 'Windows',
          osVersion: '10',
          local: 'false',
          seleniumVersion: '3.5.2',
          resolution: '1366x768'
        }
      }
    },

    //////////////////////////////////////////////////////////////////////////////////
    // Configuration for when using the Selenium service, either locally or remote,  |
    //  like Selenium Grid                                                           |
    //////////////////////////////////////////////////////////////////////////////////
    selenium: {
      // Selenium Server is running locally and is managed by Nightwatch
      selenium: {
        start_process: true,
        port: 4444,
            server_path: (Services.seleniumServer ? Services.seleniumServer.path : './Automation/bin/selenium-server-standalone-3.141.59.jar'),
        cli_args: {
          'webdriver.gecko.driver': (Services.geckodriver ? Services.geckodriver.path : './Automation/bin/geckodriver.exe'),
          'webdriver.chrome.driver': (Services.chromedriver ? Services.chromedriver.path : './Automation/bin/chromedriver.exe')
        }
      }
    },

    'selenium.chrome': {
      extends: 'selenium',
      desiredCapabilities: {
        browserName: 'chrome',
        chromeOptions : {
            w3c: true,
            args: ['disable-gpu', 'no-sandbox', 'headless']
        }
      }
    },

    'selenium.firefox': {
      extends: 'selenium',
      desiredCapabilities: {
        browserName: 'firefox',
        'moz:firefoxOptions': {
          args: [
            // '-headless',
            // '-verbose'
          ]
        }
      }
    }
  }
};

function loadServices() {
  try {
    Services.seleniumServer = require('selenium-server');
  } catch (err) {}

  try {
    Services.chromedriver = require('chromedriver');
  } catch (err) {}

  try {
    Services.geckodriver = require('geckodriver');
  } catch (err) {}
}

我的建议是为 Nightwatch.conf.js 创建一个新文件,然后从那里开始添加其余配置。下面是一个对我有用的例子:

module.exports = {
    src_folders: ["tests"],
    skip_testcases_on_fail: false,
    page_objects_path: "pageObjects",
    custom_commands_path: "./commands",
    screenshots: {
        enabled: true,
        path: "./screenshots",
        on_failure: true,
        on_error: true
    },
    test_settings: {
        default: {
            desiredCapabilities: {
                browserName: 'chrome',
                chromeOptions: {
                    prefs: {
                        download: {
                            default_directory: require('path').resolve(__dirname + '/download')
                        }
                    }
                },
            },
            webdriver: {
                start_process: true,
                port: 4444,
                server_path: require('chromedriver').path,
            }
        },

        test_workers: {
            enabled: true,
            workers: 'auto'
        },

        safari: {
            desiredCapabilities: {
                browserName: 'safari',
                alwaysMatch: {
                    acceptInsecureCerts: false
                }
            },
            webdriver: {
                port: 4445,
                start_process: true,
                server_path: '/usr/bin/safaridriver'
            }
        },

        firefox: {
            desiredCapabilities: {
                browserName: 'firefox'
            },
            webdriver: {
                start_process: true,
                port: 4446,
                server_path: require('geckodriver').path
            }
        }
    }
}

Nightwatch JS、chromedriver、geckodriver 是使用 npm 命令安装的。由于我使用的是 mac 我只需要启用 safari webdriver。

npm install nightwatch --save-dev
npm install geckodriver --save-dev
npm install chromedriver --save-dev
safaridriver --enable
  1. 如果您想查看,这是完整的回购协议:https://github.com/alapanme/NightwatchJS
  2. 此外,如果您需要我如何在 machine 上设置 Nightwatch 的详细步骤,您可以参考:https://testersdock.com/nightwatch-js-installation/