1

Is there a way to execute a batch file that lives on a remote Node machine using Selenium Grid to change the hosts file?

VM1 contains the code and acts as the Hub VM2 runs a Chrome Node VM3 runs a Firefox Node VM4 runs an IE Node

A set of batch files exist on each VM, however, running as is, only VM1 is getting the batch file executed. How do I tell the Selenium to execute the batch file on the correct Node?

FWIW, I am now executing the jobs via Jenkins.

When I originally wrote the code, I was only running the tests locally via Eclipse and not using the Grid. That being said, I wrote an AutoIT script to handle the task. The code I'm using that works when running locally is:

String env = StoredVariables.getenvironment().get();

if (env.equals("Beta Offline"))
{
    env = "BetaOffline";
}
if (env.equals("Live Offline"))
{
    env = "LiveOffline";
}

System.out.println("Set hosts file to " + env + " via AutoIT");

Thread.sleep(3000);

Runtime.getRuntime().exec(StoredVariables.getautoIT().get()+"HostsSwitcher.exe " + StoredVariables.getautoIT().get() + " " + env);
Dustin N.
  • 785
  • 1
  • 14
  • 32

2 Answers2

1

Here's how you do it.

  1. First you build a custom servlet which houses the code logic that you shared and which can be invoked via either a GET or POST method.
  2. You now need to inject the servlet into the node at startup.Refer here for learning how to do that.
  3. You then start the node with the custom servlet injected into it.
  4. You now can start running your tests and right at the point wherein the batch needs to be executed on the node, you query the grid to find out the node IP and node port to which your test was routed to. [ You can refer to this blog post of mine to learn how to query the grid to get the IP and port of the node to which your test was routed to.]
  5. Using the IP and PORT retrieved from (4) you trigger either a GET or POST to actually trigger the batch command from your test code.

That should basically solve your use case.

Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66
0

The answer above from Krishnan Mahadevan was a good guide that got me started. I used his steps #1-3 for writing a servlet to run on the end node.

For steps #4 and 5, I used a different approach. Instead of having the client try to fish out the IP of the Selenium node and communicate with it directly, it was more straightforward to have the clients communicate with the hub and then have the hub proxy the request to the correct node.

Luckily, someone else has already done the work of writing the proxy on the hub in the selenium-grid-extensions project. The hub-extensions component will proxy requests made to the Selenium Hub of the following format:

/grid/admin/HubRequestsProxyingServlet/session/${SESSIONID}/YourServletName

and then re-route them to the correct node with the path of /extra/YourServletName.

You can fetch the session ID from your client code with this:

RemoteWebDriver driver;

String sessionId = driver.getSessionId().toString();
Scott Dudley
  • 3,256
  • 1
  • 18
  • 30