How to run single selenium test case in two different nodes?

5k views Asked by At

I am new to selenium,I need to run my single selenium test case in two different nodes using FIREFOX browser (selenium grid),I have started my hub using below command

java -jar selenium-server-standalone-2.32.0.jar -role hub

Node 1: java -jar selenium-server-standalone-2.32.0.jar -role webdriver -hub http://localhost:4444/grid/register -port 9595

Node 2: java -jar selenium-server-standalone-2.32.0.jar -role webdriver -hub http://localhost:4444/grid/register -port 8585

two nodes has been created to hub .But when i run a testcase in hub,only one node is executing the test case and the other node remain available but not executing the testcase .

2) The node is selected randomly by hub while executing the testcase.

My question: Run testcase in both the nodes simultaneously

2

There are 2 answers

9
Vishal Jagtap On BEST ANSWER

Nodes can be declared as follow:-

Node 1 for chrome java -Dwebdriver.chrome.driver=C:\drivers\chromedriver.exe -jar selenium-server-standalone-2.44.0.jar -role node -hub http://localhost:4444/grid/register -port 8585 -browserName=chrome

Node 2 for firefox: java -jar selenium-server-standalone-2.44.0.jar -role node -hub http://localhost:4444/grid/register -port 9595 -browserName=firefox

You need to have following testng.xml to run same test case on different browsers:-

<suite name="Selenium TestNG Suite" parallel="tests" thread-count="5">

    <test name="Selenium TestNG - 1">
        <parameter name="browser" value="firefox" />
        <parameter name="port" value="9595" />
        <classes>
            <class name="grid.testcase" />
        </classes>
    </test>
    <test name="Selenium TestNG - 2">
        <parameter name="browser" value="chrome" />
        <parameter name="port" value="8585" />
        <classes>
            <class name="grid.testcase" />
        </classes>
    </test>

</suite>

In your test case, write code something like this:-

package grid;

import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class testcase {
    public WebDriver driver=null;
    @Parameters({"browser","port"})
    @BeforeClass
    public void initialize(String browser, String port) throws MalformedURLException{
        DesiredCapabilities capability= new DesiredCapabilities();
        capability.setBrowserName(browser);
        driver= new RemoteWebDriver(new URL("http://localhost:".concat(port).concat("/wd/hub")), capability);

    }
    @Test
    public void testThis() throws InterruptedException {
        String url="https://www.google.com";
        driver.get(url);
        driver.manage().window().maximize();
        //do something
        driver.close();
    }

}
2
dinesh On
  1. I am not sure what framework you are using. I did parallel execution using "testng" framework.
  2. For using testng you have to add testng jar file in your project build path or if you used IDE like eclipse add testng plugin.
  3. Then create your class with proper annotation(ie. @test).
  4. Create testNG.xml file and configure the things.(make sure You mentioned parallel=2. the count 2 is only how many test you want to execute in parallel)
  5. Then right click the xml file and click runas testNG..

Refer :

  1. testng-executing-parallel-tests

  2. parallel-execution-of-test-methods-in-testng