Sunday, 19 July 2015

Working with Multiple browsers...

package com.actitime.demo;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class Driver {

public static WebDriver driver;


/**
*
* @param browser
* pass string parameter like firefox , ie or chrome
* @return
* based on your argumnet , mtd will return browser object
*/

public static WebDriver getDriver(String browser){
if(browser.equals("firefox")){
driver = new FirefoxDriver();
}else if (browser.equals("ie")){
System.setProperty("webDriver.ie.driver", "");
driver = new InternetExplorerDriver();
}else{
System.setProperty("webDriver.chrome.driver", "");
driver = new ChromeDriver();
}
return driver;
}


}



Cross Browser Testing Code

package parallelTest;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.Parameters;

import org.testng.annotations.Test;

public class Guru99CrossBrowserScript {

    WebDriver driver;

    

    /**

     * This function will execute before each Test tag in testng.xml

     * @param browser

     * @throws Exception

     */

@BeforeTest

    @Parameters("browser")

    public void setup(String browser) throws Exception{

        //Check if parameter passed from TestNG is 'firefox'

        if(browser.equalsIgnoreCase("firefox")){

        //create firefox instance

            driver = new FirefoxDriver();

        }

        //Check if parameter passed as 'chrome'

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

            //set path to chromedriver.exe You may need to download it from http://code.google.com/p/selenium/wiki/ChromeDriver

            System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");

            //create chrome instance

            driver = new ChromeDriver();

        }

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

            //set path to IEdriver.exe You may need to download it from

     // 32 bits http://selenium-release.storage.googleapis.com/2.42/IEDriverServer_Win32_2.42.0.zip

     // 64 bits http://selenium-release.storage.googleapis.com/2.42/IEDriverServer_x64_2.42.0.zip

            System.setProperty("webdriver.ie.driver","C:\\IEdriver.exe");

            //create chrome instance

            driver = new InternetExplorerDriver();

        }

        else{

            //If no browser passed throw exception

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

        }

        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    }

No comments:

Post a Comment