This is a simple tutorial that shows how to use docker to run Protractor tests. We would setup a Selenium Grid and run the tests.
Docker needs to be installed on your machine. One can download it from Docker's website and follow the documentation accordingly. It is assumed that , one knows the basics of Docker for this tutorial.
To ensure Docker is installed sucessfully , type :
docker -vand one would see a similar output , depending on the version of docker installed :
Docker version 1.12.0-rc4, build e4a0dbc, experimentaldocker pull selenium/hub:latest
docker pull selenium/node-chrome:latestOne could pull 'node-firefox' if they want to work with firefox node. For more information about the different images one can work with , please look at Docker Selenium Images List
docker run -d -p 4444:4444 --name selenium-hub selenium/hub:latestOne can change the tag from 'latest' to '2.53.0' or any other version as they see fit.
docker run -d --link selenium-hub:hub selenium/node-chrome:latestThe above would create a chrome node and link it to the Selenium hub/grid created earlier.
Update the seleniumAddress to the url of the hub in your protractor config file.
seleniumAddress: 'http://localhost:4444/wd/hub'and run your tests.
./node_modules/.bin/protractor <config-file>-
Maximising the browser window
-
Depending on the webpage, the element needs to be scrolled to , in order for it to be visible, by maximising the window , the number of times , one would have to use the util method for it, decreases.
-
Also useful for debugging tests.
-
-
Specifying implicit wait
- By specifying the implicit wait , protractor waits before throwing an error for performing an action. Especially useful when the page load time fluctuates due to network.
browser.manage().window().maximize();
browser.manage().timeouts().implicitlyWait(5000);At this point you would be able to see the output of the tests and will not be able to visually confirm that the tests are running.
Install RealVNC viewer.
One needs to add a node-chrome-debug ( which has a vnc server set up ) to the selenium grid instead of node-chrome to be able to view the tests running . This can not be acheived with a node-chrome because the vnc server is not setup , in the node-chrome docker image.
One can also use the standalone-chrome-debug for the same.
docker run -d -p <port>:5900 --link selenium-hub:hub selenium/node-chrome-debug:latestFind the port that VNC server exposes for the container :
docker port <container-name or container-id> 5900and view it using the vnc viewer using the output of the above command.
Till this point , the selenium hub and nodes were created by typing commands in the CLI. This approach would work on one's machine , but is not a scalable solution. That's where Docker Compose comes into picture. We would do the above the steps , by specifying a .yml file and see the results.
Create a docker-compose.yml
version: '3.5'
services:
chrome:
image: selenium/node-chrome-debug:3.11.0-californium
volumes:
- /dev/shm:/dev/shm
environment:
HUB_HOST: hub
ports:
- "5900:5900"
networks:
- protractortest
hub:
image: selenium/hub:3.11.0-californium
ports:
- "4444:4444"
networks:
- protractortest
angular:
image: node:6-alpine
ports:
- 8000:8000
volumes:
- ./app:/var/www
working_dir: /var/www
command: ["npm", "start"]
networks:
- protractortest
networks:
protractortest:
name: protractortestThen in the terminal, enter the command
docker-compose up -ddocker run -it --rm --network=protractortest -v /dev/shm:/dev/shm -v $(pwd):/protractor jozzhart/node-6-protractor ./docker-selenium-grid-conf.jsor use the shortcut
npm testThis should get you started with writing tests using Docker and Protractor . To learn more, see the documentation for Docker and Protractor.