Friday, 5 February 2016

Cross browser testing in selenium webdriver

Cross browser testing in selenium web driver:
===================================

By using testNG annotation ie BY USING @PARAMETERS ANNOTATION we can perform cross browser testing.

Cross browser Testing is a technique to test web application with different web browsers.

@Parameters("browser")
 for this parameter we need to pass the parameter value from testng.xml


<parameter name="browser" value="Firefox" /> 

example:
--------------
CrossBrowserTestingDemo.java
--------------------------------------------
package testngdemo;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class CrossBrowserTestingDemo {

WebDriver webDriver;


@BeforeTest

@Parameters("browser")

    public void setup(String browser) throws Exception{
        if(browser.equalsIgnoreCase("firefox")){

        webDriver = new FirefoxDriver();
            webDriver.manage().window().maximize();
    webDriver.manage().deleteAllCookies();
    webDriver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);

        }


        else if(browser.equalsIgnoreCase("chrome")){

            System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
            webDriver = new ChromeDriver();
            webDriver.manage().window().maximize();
    webDriver.manage().deleteAllCookies();
    webDriver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);

        }

else if(browser.equalsIgnoreCase("ie")){

            System.setProperty("webdriver.ie.driver","C:\\IEdriver.exe");
            webDriver = new InternetExplorerDriver();
            webDriver.manage().window().maximize();
    webDriver.manage().deleteAllCookies();
    webDriver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);

        }

        else{
            throw new Exception("Browser is not correct");

        }
    }

    @Test

    public void testParameter() throws InterruptedException{

    webDriver.get("https://google.com");
webDriver.findElement(By.name("q")).sendKeys("selenium by ramesh");
webDriver.findElement(By.name("btnG")).click();
Thread.sleep(3000);
webDriver.findElement(By.linkText("Selenium By Ramesh Anapati")).click();

    }


}

testng.xml:
--------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="TestSuite" thread-count="2" parallel="tests">

<parameter name="browser" value="firefox"></parameter>

<test name="FirefoxTest">

<parameter name="browser" value="Firefox" />

<classes>

<class name="testngdemo.CrossBrowserTestingDemo">

</class>

</classes>

</test>
<test name="ChromeTest">

<parameter name="browser" value="Chrome" />

<classes>

<class name="testngdemo.CrossBrowserTestingDemo">

</class>

</classes>

<test name="IETest">

<parameter name="browser" value="IE" />

<classes>

<class name="testngdemo.CrossBrowserTestingDemo">

</class>

</classes>

</test>
</suite> <!-- Suite -->

No comments:

Post a Comment