Tuesday 24 March 2015

Running testng class file from cmd prompt and jenkins

we can run the testng class files through eclipse by adding the testng libraries in Build path.
If we need to run the same testng class files from command prompt and through any tools we could do so by adding the testng libraries to the CLASSPATH variable. or by giving the path with -cp option available in java. Also can add any jar files which are referred in the class files.



set CLASSPATH = %CLASSPATH%;D:\Are\Sel\Ecom_project\TestNG\testng-6.8.jar

D:\Area\Sel\>java org.testng.TestNG testng.xml
[TestNG] Running:
  D:\Are\Sel\Ecom_project\src\scripts\testng.xml

TestNG is working fine
===============================================
Suite1
Total tests run: 1, Failures: 0, Skips: 0
===============================================



To run the same testng scripts via Jenkins can add the CLASSPATH variable at master level or at node level specifying the path of the jar files. Represented the same in below snaps.










 

Friday 13 March 2015

Selenium grid implementation on two different machines and browsers


This post guides us for the GRID implementation of Selenium where we launch a grid server and register two nodes in two different machines.

Here we launch the HUB in machine1 and start node 1 with two instances of chrome browser and node 2 with two instances of Firefox.


To start Hub

HUB: Machine 1

java -jar selenium-server-standalone-2.43.1.jar -role hub -port 4455

To start nodes

Node : Machine 2 (launching only chrome browser of 2 instances)
 

java -jar selenium-server-standalone-2.43.1.jar -role node -hub http://<ip_of_hub_machine:4455>/grid/register -port 5557 -Dwebdriver.chrome.driver=chromedriver.exe -browser browserName=chrome,platform=WINDOWS,maxInstances=2

Node: Machine 3 (
launching only firefox browser of 2 instances)

java -jar selenium-server-standalone-2.45.0.jar -role node -hub http://
<ip_of_hub_machine:4455>/grid/register -port 5587 -browser browserName=firefox,platform=WINDOWS,maxInstances=2

After launching hub and nodes we can see the instances of the nodes in grid console under URL http://localhost:4455/grid/console

















 

Sample program:

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class GridTest {

    static DesiredCapabilities dc = null;
    WebDriver driver;

    public GridTest() throws MalformedURLException {
        dc = new DesiredCapabilities();
       

    }

    public void firefox() throws MalformedURLException, InterruptedException {
        dc.setBrowserName(DesiredCapabilities.firefox().getBrowserName());
        excute("firefox");
    }

    public void chorme() throws MalformedURLException, InterruptedException {
        dc.setBrowserName(DesiredCapabilities.chrome().getBrowserName());
        excute("chrome");
    }

    public void excute(String browsername) throws MalformedURLException, InterruptedException {
       
        dc.setPlatform(Platform.WINDOWS);
        driver = new RemoteWebDriver(new URL(
                "http://<ip_of_hub_machine:4455>/wd/hub"), dc);
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        System.out.println("In "+browsername);
        driver.get("https://www.google.co.in/");
        System.out.println(driver.getCurrentUrl());
        driver.quit();
    }

    public static void main(String[] args) throws MalformedURLException, InterruptedException {
        // TODO Auto-generated method stub

        GridTest t1 = new GridTest();

        t1.firefox();
        t1.chorme();

    }
}




Firefox in Machine 1

Chrome in Machine 2