Test on Internal Networks
BrowserStack enables you to run your CodeceptJS automated tests on your internal development environments, on localhost, and from behind a corporate firewall. This feature is called “Local Testing”. 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.
Prerequisites
BrowserStack Username and Access key, which you can find in your account profile. If you have not created an account yet, you can sign up for a Free Trial or purchase a plan.
Run your first Local test
Complete the following steps to run a CodeceptJS test script using Local Testing:
- Clone the sample codecept-js-selenium-browserstack repository using the following command:
git clone https://github.com/browserstack/codecept-js-browserstack cd codecept-js-browserstack
- Run the following command in your command-line to install the required dependencies in the cloned repository:
npm install
- Verify that the
browserstack-local
dependency is added in thepackage.json
file:"dependencies": { "browserstack-local": "^1.5.1" }
- Set your BrowserStack credentials in the
conf.js
file as follows:const userCredentials = { 'browserstack.user': process.env.BROWSERSTACK_USERNAME || 'YOUR_USERNAME', 'browserstack.key': process.env.BROWSERSTACK_ACCESS_KEY || 'YOUR_ACCESS_KEY' }
- Verify that the
browserstack.local
capability is set totrue
in theconf.js
file as follows:exports.localTestCapabilities = { "local" : "true", }
- Set your BrowserStack Access Key in the
tests/local.js
file as follows:// set your Access Key const bs_local_args = { 'key': process.env.BROWSERSTACK_ACCESS_KEY || 'YOUR_ACCESS_KEY' };
- Run the CodeceptJS test using the following command:
npm run local
- View the test result on your Automate Dashboard.
Understand your Local test script
When you run the npm run local
command, the local.js
file within the tests
directory is executed. When the test is triggered, it:
- Starts Local Testing connection
- Opens
http://bs-local.com:45691/check
- Checks whether the web page contains the
Up and running
text - Marks the test as passed or failed based on the availability of the text
- Stops the Local Testing connection.
const webdriver = require('selenium-webdriver');
const { By } = require('selenium-webdriver');
const assert = require('assert');
const browserstack = require('browserstack-local');
// Input capabilities
const { localTestCapabilities, hubURL } = require('../conf');
//creates an instance of Local
const bs_local = new browserstack.Local();
const bs_local_args = { 'key': process.env.BROWSERSTACK_ACCESS_KEY || "YOUR_ACCESS_KEY" };
// starts the Local instance with the required arguments
bs_local.start(bs_local_args, function() {
console.log("Started BrowserStackLocal");
runTestWithCaps();
});
async function runTestWithCaps () {
let driver = new webdriver.Builder()
.usingServer(hubURL)
.withCapabilities(localTestCapabilities)
.build();
try{
await driver.get("http://bs-local.com:45691/check");
const body = await driver.wait(
webdriver.until.elementLocated(By.css('body'))
, 10000);
const bodyText = await driver.wait(
webdriver.until.elementIsVisible(body , 10000)
).getText();
assert(bodyText == 'Up and running');
//marking the test as local is up
await driver.executeScript(
'browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed","reason": "Local test is successful"}}'
);
} catch(e) {
//marking the local test as Failed
console.log("Error:", e.message)
await driver.executeScript(
'browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed","reason": "Couldn\'t connect to local"}}'
);
} finally {
if(driver){
await driver.quit();
}
}
// stop the Local instance
bs_local.stop(function() {
console.log("Stopped BrowserStackLocal");
});
}
Next steps
After you have successfully run your first test using BrowserStack Local, you can explore the following sections:
- Test local websites that resides behind a proxy
- Manage Local Testing connections
- Set up your CI/CD: Jenkins, Bamboo, TeamCity, Azure, CircleCI, BitBucket, TravisCI, GitHub Actions, GoCD
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!