Tutorial 25 - Selenium 4 Grid Distributed mode
Let us now see how to setup the third mode viz Distributed mode.
Go to the official download page and download the latest stable version of Selenium server
https://www.selenium.dev/downloads/
Download the latest stable chrome browser executable
Click documentation, you will be redirected to below page
https://chromedriver.chromium.org/
Click stable release link that you see above
Download the zip file and extract it.
Place the chromedriver jar in the same location where you have kept the selenium grid jar
Similarly, download the firefox executable
Open command prompt and cd to above location.
Let us now start the event-bus by executing below command:
java –jar <jarfilename.jar> event-bus
Copy the event-bus url and append /status at the end and launch it, notice that event bus is running
http://192.168.33.1:5557/status
Next, let us create session map (responsible for mapping the session ids to the node the session is running on).
Open a new command window java –jar <jarfilename.jar> sessions
http://192.168.33.1:5556/status
Next, let us create session queue (adds the new session request to a local queue and the distributor picks up the request from the queue).
Open a new command window and execute java –jar <jarfilename.jar> sessionqueue
http://192.168.33.1:5559/status
Next, we will create distributor (when selenium client code sends a session creation request, then distributor is responsible for assigning a node. So we need to point distributor to sessions and session queue address).
Open a new command window and execute java –jar <jarfilename.jar> distributor --sessions <session address> --sessionqueue <sessionqueue address> --bind-bus false
http://192.168.33.1:5553/status
Next, create a router (router receives all client requests, the request to create a new session will be redirected to the distributor, thus router needs to point to sessions/sessionqueue/distributor).
Open a new terminal java –jar <jarfilename.jar> router --sessions <session address> --sessionqueue <sessionqueue address> --distributor <distributor address>
Launch the router url http://192.168.33.1:4444
Notice currently no nodes are registered
Next, we will register node to grid hub.
Open another terminal and execute below command.
The --detect-drivers true will automatically detect the firefox/chrome etc browsers in the same directory where selenium server jar is kept
java -jar selenium-server-4.1.1.jar node --detect-drivers true
Notice above that the node has been added.
Refresh the hub, notice below that we can now see the browsers
Let us now execute our script
Note: If the script does not execute than stop and restart all the individual components
Below on firefox browser
So this is how we can execute our test in a distributed grid server.
Code snippets
package sel4scripts;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
public class GridDistributedMode {
public static void main(String[] args) throws MalformedURLException, InterruptedException {
Capabilities cap = new FirefoxOptions();
//Capabilities cap = new ChromeOptions();
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444"),cap);
driver.get("https://www.google.com/");
driver.findElement(By.name("q")).sendKeys("Distributed grid…");
Thread.sleep(3000);
}
}
Thank you for reading!