Table Of Contents
Connecting the node (another external machine to the hub which is present in the main machine)
There are four components of Selenium -Selenium IDE, RC, WebDriver, and Grid. We are all familiar with WebDriver. We know tests can run parallelly using TestNG. Then what is the need of this Selenium Grid. In this blog we will see how Selenium Grid works and what is the use of it.
Customers wants more features with fast delivery and at the same time everyone wants lesser bugs. Speed, Quality and Money are important factors to be considered when we develop and test any applications. In todays Agile environment were delivery happens very quickly speed and quality cannot be compromised. One of the reasons behind the huge popularity of Selenium is its capability to automate cross browser testing. And Selenium Grid has by far been the most useful component of the Selenium project. Though Cypress is introduced in the market it does not support cross browser testing.
What is Selenium Grid
Selenium official site "https://www.selenium.dev/documentation/grid/" gives the definition for Grid as Selenium Grid allows the execution of WebDriver scripts on remote machines by routing commands sent by the client to remote browser instances.
Grid aims to:
Provide an easy way to run tests in parallel on multiple machines
Allow testing on different browser versions
Enable cross platform testing
It is a smart proxy server that makes it easy to run tests in parallel on multiple machines. It is a component of Selenium used for executing test cases remotely.
Imagine for an entire state for all the houses the water supply comes from the main reservoir. A pipeline is connected to each individual house for the water supply. We can think of a power grid too which supplies electricity to all the houses. Here the reservoir and power grid acts as a central hub and all the individual houses acts as nodes. When one system is used to run tests, they are not as efficient considering the high complexity and size of the test cases. To speed up things we need various systems to be connected which will help us to run the test cases quickly and simultaneously.
In the above diagram we can see few terms like hub, node. Once we get an idea how this Selenium Grid works internally, we will get a clear picture of this grid.
Architecture Of Selenium Grid 4
After understanding this Selenium Architecture 4 we will understand what are the major differences between 3 and 4 and why Selenium Grid 4 provides its users with fast paced computing environments. Following are the components of Selenium Grid 4 architecture.
New Session Que
Lets understand them step by step. I have explained in a very simple form so a beginner can also understand the basics.
Client is nothing but your local system. Client sends the request that is the test cases which are to be executed to the Router.
Router sends the request directly to the node(chrome browser, Firefox browser etc. are called as nodes) but if it is a first-time request received from the client then it contacts the Distributor as Router does not know how to distribute the requests to the node. As the name indicates router job is to route the requests.
This component is responsible and decides on which selenium node the session should run. It is a new feature in Selenium 4.
It stores the session information for the distributed tests. A Session Map is a KV store (Key-Value). It stores session id and the capabilities which is shown in the above diagram.
5.New Session Que
Distributor can solve only one session at a time. There is another component between Router and distributor which will store all new sessions waiting in the que. This component is called as New session que. Distributor will retrieve one by one from this session que and check which physical machine it has to send. If only one request is sent from the router there is no scope.
Node is the machine which is attached to the hub. There can be multiple (preferably) nodes in Selenium Grid. Selenium Grid uses a hub-node concept where you only run the test on a single machine called a hub, but the execution will be done by different machines called nodes.
All the communication happens through this event bus channel which is not shown in the above diagram as it is virtual.
Two Methods Selenium Grid Can Be Set Up
1. There are different ways we can set up the Selenium Grid.
2. If we check their official documentation it is said several components compose a Selenium Grid. In the above architecture we have seen the different components.
3.Depending on our needs we can start each of the components on its own or a few at the same time by using a Grid role. There are 2 nodes where we can start the components mentioned above.
4.One is Distributed node and another one is Hub and Node method .
5. Most of the companies rely on hub and node method.
6. We can start all the components together instead of starting them separately which saves the time . By this way I mean to say that need not write separate syntax for each components.
Setting Up Selenium Grid In Our Local System
In this blog we will be seeing how to set up the Grid using hub and node which is mostly used than the distributed mode.
Download the Selenium Server . This can be downloaded from https://www.selenium.dev/downloads/ site.
2. Then download all the browser drivers and place in the same path where Selenium server is located. I Created a separate folder named Grid in the desktop and stored all the downloaded drivers and Selenium server in the same folder.
3. Start the hub -which eventually starts the Router, Distributor, Session Map, New Session Que and Event Bus :
In the command prompt for windows type the following syntax C:\Users\13095\Desktop\Grid>java -jar selenium-server-4.8.1.jar hub. Here the green color represents the path where I have stored the downloaded items mentioned above.
After hitting enter Selenium hub will be started in your local system as shown in the below screen shot.
4. Type Localhost:4444 and check. This port no can be found in the logs from your command prompt. The page will get displayed with the message shared in the below screen shot. So now the Grid is installed in the local system but nodes are not registered with it yet.
5. In this scenario hub and node are in the same machine . But in real time different machines will be acting as a node to execute bulk number of test cases.
6. Start the node in the same machine where hub is running :
Type the following syntax in the command prompt and hit enter
C:\Users\13095\Desktop\Grid>java -jar selenium-server-4.8.1.jar node --detect-drivers true
Syntax : java-jar<SeleniumJarname>node --detect. drivers true
In the below screen shot we can the see the nodes that is the browser drivers are added. The yellow arrow mark points to the syntax.
7. In the local host :4444 we can see the nodes added now. Have shared the screen shot for a better clarity. The max concurrency is 8 in my system which means 8 individual browsers can be opened parallelly.
Connecting the node (another external machine to the hub which is present in the main machine)
Here we will be seeing how to connect the node from the external machine to the machine where the hub is present.
Need to install the Selenium server stand alone jar file and drivers as mentioned above. Drivers after selenium 4 need not be physically downloaded. Only Selenium Grid (the jar alone can be downloaded.
A couple of things to be known before writing the syntax to register the node of external machine to the central hub.
There are 2 sockets basically. One is publishing event (XPUB)and another one is subscribing event(XSUB)
In the Scenario 1 which we saw before hub and node were present in the same machine. If we analyze the cmd prompt node started at 5555 port no and it will make a call to hub port no 4444.
Here in this scenario 2 if you want to ping the hub you nee to register your node(external machine ) to your hub machine.
If you see in your cmd prompt or in the above screen shot publish event is listening at port 4442. So we will be making a call from your external machine(node) by using the following syntax
(path)java -jar selenium-server-4.8.1.jar node --detect-drivers true --publish-events tcp://192.168.0.16:4442--subscribe-events tcp://192.168.0.16:4443
In the above syntax (Have explained the syntax for a better clarity) //This entire syntax you need to type in the machine 2 (node)that you are trying to register with the hub. You can first connect your systems with teams viewer. Need to download Teams viewer.
1. Need to give the path where you have downloaded your selenium server jar.
2. After tcp I have given the IP address which you will get from the command prompt where you have your central hub installed. In this blog I have installed the hub in machine 1.
3. You can see publish-events and subscribe-events and their respective port numbers which again you will get from the command prompt where your hub is located.
4. If you have more than one computer in your house can set the hub in one machine and try connecting another machine to the first one by using the above mentioned steps and start executing your test cases.
5. You can cross verify by entering localhost:4444 and search where a page will get displayed showing what all are the nodes connected to the hub.
Key Differences between Selenium Grid 3 and 4.
Today, almost every business has realized the importance of automation testing. And to fulfil this growing demand for automation testing numerous test automation tools and frameworks are made available in the market today. Out of all the available automation testing frameworks and tools, one tool that is widely used by testers all across the globe is Selenium. Recently a new update of Selenium was rolled out in the market that gained a lot of attraction from the audience. Selenium 4 has several additional benefits like seamless integration, parallel testing, cross-browser testing, better documentation, etc. that make Selenium testing much easier.
In this blog we learned what is Selenium Grid, Its architecture and different components and how to install the Selenium Grid in our system and another external system and how to connect it to the main hub. It might seem to be difficult to understand as a beginner but as said "Practice makes a man perfect".
For Cross browser testing and parallel execution of test cases can refer the following below links written by my colleague Vasanthi.