05. TestNG - Scripts to run for Multiple Browers

TestNG - Scripts to run for Multiple Browers:

we want to run our test on multiple browsers, all we need to do is parameterize our initDriver() method and pass it the name of the browser on which we want to run our test. Based on the browser, we will create either ChromeDriver or FirefixDriver. We will pass the browser name throughTestng.xml.

package Others;
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.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class TestNG_Sample {
            private WebDriver driver;      
            @Parameters({"browser", "driverPath"})
            @BeforeTest
            public void initDriver(String browser, @Optional("") String driverPath) throws Exception {
                        System.out.println("You are testing on browser " + browser);
                        browser = browser.toLowerCase();
                        if (!driverPath.equals("")) {
                                    System.setProperty("webdriver.chrome.driver", driverPath);
                        }
                        if (browser.equals("chrome")) {                                 
                                    driver = new ChromeDriver();
                        } else if (browser.equals("firefox")) {
                                    driver = new FirefoxDriver();
                        } else {
                                    throw new RuntimeException("Please create a driver for " + browser);
                        }
            }

            @Test(dataProvider = "searchStrings")
            public void searchGoogle(final String searchKey) {
                        System.out.println("Search " + searchKey + " in google");
                        driver.navigate().to("http://www.google.com");                    
                        WebElement element = driver.findElement(By.name("q"));
                        System.out.println("Enter " + searchKey);
                        element.sendKeys(searchKey);
                        System.out.println("submit");
                        element.submit();
                         (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Object>() {
                        public Boolean apply(WebDriver d) {
                            return d.getTitle().toLowerCase().startsWith(searchKey.toLowerCase());
                        }
                                               
                    });
                        System.out.println("Got " + searchKey + " results");
            }

            @DataProvider
            private Object[][] searchStrings() {
                        return new Object[][] { { "TestNG" }, { "Selenium" } };
            }

            @AfterTest
            public void quitDriver() throws Exception {
                        driver.quit();
            }         
}

Testng.xml will have two tests and both the tests will run the same test class. We want TestNgSeleniumChromeTest to run on a chrome browser so we have set the browser parameter to chrome. Likewise, we want TestNgSeleniumFirefoxTest to run on a firefox browser so the browser is set to value firefox.

Testng.xml:

<?xml version="1.0" encoding="UTF-8"?>
<suite name="TestNgSeleniumSuite" parallel="false">
            <test name="TestNgSeleniumChromeTest">
                        <parameter name="browser" value="chrome"></parameter>
                        <parameter name="driverPath" value="D:\\Softwares\\chromedriver_win32\\chromedriver.exe"></parameter>
                        <classes>
                                    <class
                                                name="Others.TestNG_Sample" />
                        </classes>
            </test>
           
            <test name="TestNgSeleniumFirefoxTest">
                        <parameter name="browser" value="firefox"></parameter>
                        <classes>
                                    <class
                                                name="Others.TestNG_Sample" />
                        </classes>
            </test>
</suite>

Output:

You are testing on browser chrome
Starting ChromeDriver 2.14.545313 (3d64c9aa096063e707c9cf5c400edf2e2c500566) on port 7334
Only local connections are allowed.
Search TestNG in google
Enter TestNG
submit
Got TestNG results
Search Selenium in google
Enter Selenium
submit
Got Selenium results
You are testing on browser firefox
Search TestNG in google
Enter TestNG
submit
Got TestNG results
Search Selenium in google
Enter Selenium
submit
Got Selenium results


No comments:

Post a Comment