Print

Running Tests on Selenium Grid 2.0 with Web Driver

In this section, you will use one computer as a hub and as a node to run a test with Web Driver on Selenium Grid 2.0.

 

Step 1: launch the hub with the command line below. 

C:\Selenium Server>java -jar selenium-server-standalone-2.39.0.jar -role hub

Step 2: launch the node with the command line below. 

 

C:\Selenium Server>java -jar selenium-server-standalone-2.39.0.jar -role webdriver -hub http://localhost:4444/grid/register -port 5566

 

Step 3: verify Selenium Grid Hub console to make sure that hub and node are running. Open a browser and type the URL "http://localhost:4444/grid/console".

 

Step 4: on Eclipse, create a test project and write scripts. Create a Java project named "SeleniumGridWithTestNG". Add a package named "com.seleniummaster.seleniumgridtestng" and add a TestNG class. Make sure that selenium-server-standalone-2.39.0.jar and selenium-driver.jar file are added to the build path. If TestNG plug-in has not been installed, you can install it from the Eclipse market place. 

 

SeleniumMasterLoginTest.Java source code

package com.seleniummaster.seleniumgridtestng;
import static org.testng.AssertJUnit.assertEquals;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;

public class SeleniumMasterLoginTest {
WebDriver driver;
String baseUrl, nodeUrl;
  @Test
  public void LoginTest() {
    driver.navigate().to("http://demo.mahara.org/index.php");
    driver.manage().window().maximize();
    driver.findElement(By.id("login_login_username")).sendKeys("test");
    driver.findElement(By.id("login_login_password")).sendKeys("XXXXXXX")//here password is omitted
   driver.findElement(By.id("login_submit")).click();
     for (int second = 0;; second++) {
      if (second >= 60Assert.fail("timeout");
      try 
        assertEquals("Online users", driver.findElement(By.cssSelector("#sb-onlineusers > h3")).getText());
        break
      catch (Exception e) {}
         }
       
    }
  
  @BeforeTest
  public void setup() throws MalformedURLException {
 baseUrl="http://demo.mahara.org";
 nodeUrl="http://localhost:5566/wd/hub";
 DesiredCapabilities capability=DesiredCapabilities.firefox();
 capability.setBrowserName("firefox");
 capability.setPlatform(Platform.WINDOWS);
 driver=new RemoteWebDriver(new URL(nodeUrl),capability);
  }

  @AfterTest
  public void afterTest() {
driver.quit();
  }

}

Step 5: run the project as TestNG Test. 

Test result showed that test passed. In this test:

  • DesiredCapabilites is used to set the type of browser and OS that we will automate
  • RemoteWebDriver is used to set which node (or machine) that our test will run against.