Test on Internal Networks
Local Testing is a BrowserStack feature that allows you to run automated tests on internal development environments (localhost) or behind a corporate firewall.
Local Testing establishes a secure connection between your machine and the BrowserStack cloud. Once you set up Local Testing, all URLs work out of the box, including HTTPS URLs and those behind a proxy or firewall.
This guide will help you:
- Run your local test
- Understand Nightwatch project structure
- Understand the Nightwatch configuration file
- Understand the command to run local test
Prerequisites
- BrowserStack Username and Access key, which you can find in your account settings. If you have not created an account yet, you can sign up for a Free Trial or purchase a plan.
- Node.js installed on your machine.
- Git installed on your machine.
Run your local test
- Clone the nightwatch-browserstack sample project, available on GitHub, using the following command:
git clone https://github.com/browserstack/nightwatch-browserstack.git
Note: You can use the repository to understand how test cases can be structured to integrate BrowserStack with your existing test suites.
- Open the cloned project in the IDE of your choice and install the required dependencies by running the following command in your command line:
npm install
- Set your BrowserStack credentials in the project by running the following command at the root directory of your project:
export BROWSERSTACK_USER=YOUR_USERNAME && export BROWSERSTACK_KEY=YOUR_ACCESSKEY
Note: It is recommended that the BrowserStack credentials are added to the environment variables to use them every time you run this project.
- Run a local test using the following command:
npm run local
- View your tests on BrowserStack by visiting your Automate Dashboard.
npm
command generates the nightwatch.conf.js
file in the sample Nightwatch.js project folder, preloaded with some capabilities, such as browser name and browser version. If you are editing your existing test scripts and already have a configuration file, ensure that the browserstack.local
capability is added to the configuration file and set to true
.
For example:
desiredCapabilities: {
'browserstack.local': true
}
Understand Nightwatch project structure
Nightwatch.js project consists of the following files and folders structure:
-
tests/local
directory: It contains the test script file to test a locally-hosted website. -
scripts
directory: It contains the runner file required to set up and manage a local connection using methods, such asbs_local.start(), bs_local.stop(),and bs_local.isRunning()
, which are provided by BrowserStack. -
package.json
file: It contains the installed dependencies of the project. It specifies the command to run the tests. -
nightwatch.conf.js
: It contains the configuration of environments and capabilities to use while running the test.
Understand the Nightwatch configuration file
In the sample repository, after you run the npm run local
command, a configuration file named nightwatch.conf.js
is created at the root directory of the project. It is preloaded with default configuration for connecting tests to BrowserStack, and sample capabilities, such as browser name and its version, for running tests on multiple browsers.
As seen in the code snippet given below, the browserName
has been set to firefox
, within desiredCapabilities
, to run a test on Firefox. As there is no desktop machine specified, the local test runs on Windows 10 by default.
// default setting of the test
test_settings: {
default: {
disable_error_log: false,
launch_url: 'https://nightwatchjs.org',
screenshots: {
enabled: false,
path: 'screens',
on_failure: true
},
desiredCapabilities: {
browserName : 'firefox'
},
webdriver: {
start_process: true,
server_path: (Services.geckodriver ? Services.geckodriver.path : '')
}
},
// other capabilities
// Configuration for when using the browserstack.com cloud service |
browserstack: {
selenium: {
// setting the host name
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' : {
// sets BrowserStack's credentials via environment variables
userName: '${BROWSERSTACK_USER}',
accessKey: '${BROWSERSTACK_KEY}',
}
},
//...
// capabilities to run local test on BrowserStack
'browserstack.local': {
extends: 'browserstack',
desiredCapabilities: {
'browserstack.local': true
}
},
Understand the command to run local test
The test file and capabilities to consider for running local test is specified in the package.json
file, as shown below:
"scripts": {
...
"local": "node scripts/local.runner.js --test ./tests/local/local_test.js --env browserstack.local ",
...
},
This statement instructs the npm run local
command to run the local_test.js
file by using capabilities specified within browserstack.local
in the nightwatch.conf.js
file.
Next steps
After you have successfully run your local test on BrowserStack, you might want to do the following:
We're sorry to hear that. Please share your feedback so we can do better
Contact our Support team for immediate help while we work on improving our docs.
We're continuously improving our docs. We'd love to know what you liked
We're sorry to hear that. Please share your feedback so we can do better
Contact our Support team for immediate help while we work on improving our docs.
We're continuously improving our docs. We'd love to know what you liked
Thank you for your valuable feedback!