In my last post I wrote about creating Selenium Grid with use of Docker. I’ve received some questions about customising node’s containers – by default, Docker containers for Selenium Grid nodes run only one instance of browser per node. It’s important to understand that running Grid on Docker is slightly different approach than running it alone – instead of building huge Grid with multiple nodes and dozens of browsers, you run few smaller, independent machines. If one machine is down – you throw it away and build another one. Great use case of Selenium Grid with Docker is to build Grid’s machines as a self service for teams in your company, or to build it automatically before automated tests trigger, and destroy it after.
Nevertheless, it is possible to tweak default node’s conteiners, so they would contain more browser instances. In this post we’ll create container with custom Selenium Grid’s node configuration – our container would provide more than one instance of browser. Remember, that if you have an account on Docker Hub, you can host one container for free, so after going through this tutorial you can commit your own container and host it, or just create your own, custom Grid on Docker, tailored to your needs.
For purpose of this tutorial, we’ll assume that you’ve already set your Selenium Grid with Docker. If not, see the full step-by-step tutorial in my previous post. Starting point for this post is having hub and at least one node linked and running…
… so right now we’re having Selenium Grid with one node, running one instance of Firefox:
Our goal is to increase number of instances to – let’s say – three Firefox’es. How to do that, since Docker containers are isolated units by default? First thing we have to do is ssh to container. It’s not a simple ssh though, it’s rather connecting to node’s tty:
$ docker exec -it firefox bash
… and we’re in! Now you can execute command on container process:
Our node is configured by config.json file. How can we locate it? First, let’s go sudo…
$ sudo su
… and find it!
$ find . -name "config.json"
Our config is in ./opt/selenium/ directory. We can’t open config.json in vi, vim, or any other, because Docker containers are running on minimalistic linux vm’s, without any additional tools. We have apt-get though, so we can add some!
$ apt-get update && apt-get install vim
After downloading vim, let’s edit our config.json. File looks as follows:
We have two maxInstances keys. First one is Selenium RC, which is deprecated, and the second one is WebDriver. Our goal is to change the number of Firefox instances to 3, so let’s change the value of the second maxInstances key from 1 to 3 (“maxInstances”: 3). If you’re having problems navigating through the file by arrows, try to use h,j,k,l keys. When value is changed, save and exit from the file, and then type exit to change user and one more exit to go abandon container’s terminal.
Our new container’s configuration is ready to go. Last thing we have to do is to restart out container:
$ docker restart firefox
If you open your Grid in browser now, you’ll see 3 Firefox instances in Webdriver protocol:
If you want to continue reading and expand your knowledge in area of Docker and Selenium Grid, I recommend you these books:
- Docker in Action – if you want to truly understand Docker, this is the book for you. Great reference and source of knowledge.
- The DevOps 2.0 Toolkit: Automating the Continuous Deployment Pipeline with Containerized Microservices – this one is more practical, not only about containers but building whole deployment pipelines.
- Selenium WebDriver Practical Guide – there’re not many materials about Selenium Grid, but this books is a great read if you want to master not only Grid, but Selenium in general
Although recommended approach to work with Selenium Grid with containers is slightly different, Docker gives you many possibilities. It’s always good to search your own, best solution. If you have any thoughts or questions – leave a comment!