Skip to content

Instantly share code, notes, and snippets.

@deterralba
Last active July 17, 2023 15:40
Show Gist options
  • Save deterralba/5ec757d8cbb47152c072eea8183a8007 to your computer and use it in GitHub Desktop.
Save deterralba/5ec757d8cbb47152c072eea8183a8007 to your computer and use it in GitHub Desktop.
Setup nightwatchjs + selenium/standalone-chrome

Goal

Use 2 docker containers to run nightwatchjs test: one with nightwatch, the other with selenium/standalone-chrome.

The setup

In your folder, the following files:

- Dockerfile
- test/
  - nightwatch.json
  - test.js

We may need to update the selenium_host ip to your host machine ip (accessible from your container, ie the bridge network gateway: try docker inspect bridge).

Start the docker-selenium-chrome container with docker run --rm -d -p 4444:4444 --name selenium-chrome selenium/standalone-chrome.

Build and run the nightwatch container

Build the nightwatchjs runner with docker run build -t nightwatch ., run it with docker run nightwatch.

Redirect selenium urls

You probably want to redirect selenium's url to your local sever, you can do that with:

echo "$SERVER_IP my-domain.com" | docker exec -i $SELENIUM_IMAGE_NAME sudo tee -a /etc/hosts

where SERVER_IP is the server ip and SELENIUM_IMAGE_NAME the selenium docker image name... simple.

# Create a lightweight nigthwatchjs docker runner
FROM alpine:latest
RUN apk add --update nodejs-npm && \
npm install -g nightwatch && \
rm -rf /tmp/* /root/.npm
# Copy your tests and your config to the containers
COPY tests /tests
CMD nightwatch -c tests/nightwatch.json
{
"src_folders" : ["./tests/"],
"output_folder" : "reports",
"custom_commands_path" : "",
"custom_assertions_path" : "",
"page_objects_path" : "",
"globals_path" : "",
"selenium" : {
"start_process" : false
},
"test_settings" : {
"default" : {
"selenium_port" : 4444,
"selenium_host" : "172.17.0.1",
"desiredCapabilities": {
"browserName": "chrome",
"chromeOptions" : {
"args" : ["--no-sandbox"]
},
"acceptSslCerts": true
}
}
}
}
// taken from http://nightwatchjs.org/guide
module.exports = {
'Demo test Google' : function (browser) {
browser
.url('http://www.google.com')
.waitForElementVisible('body', 1000)
.setValue('input[type=text]', 'nightwatch')
.pause(1000)
.end();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment