Handling Multiple Browsers

Selenium Element Locators, Inspecting Elements, Selenium WebDriver API Commands and Writing Selenium Test Cases.
Post Reply
Vijay Kumar Varma
Posts: 58
Joined: Mon Nov 26, 2018 1:51 am

Handling Multiple Browsers

Post by Vijay Kumar Varma » Thu Dec 06, 2018 2:50 pm

How to handle multiple browsers instants using Selenium WebDriver?

Imran
Posts: 23
Joined: Thu Dec 06, 2018 11:50 am

Re: Handling Multiple Browsers

Post by Imran » Sun Dec 09, 2018 1:37 am

In Selenium WebDriver software testing tool, We can use "WebDriver.getWindowHandles()" to get the handles of all opened windows by webdriver and then we can use that window handle to switch from from one window to another window.

Selenium WebDriver has built in "WebDriver.switchTo().window()" method available to switch from one window to another window so it is very easy to handle multiple windows in webdriver.

sekhar
Posts: 47
Joined: Mon Dec 31, 2018 6:40 am

Re: Handling Multiple Browsers

Post by sekhar » Thu Jan 03, 2019 9:31 am

In every project, it is required to perform multi-browser testing to make sure that the functionality is working as expected with every browser to give equal user experience to all of the wide range of audience. It takes a considerable time to test everything on every browser and when we have used automation to reduce the testing efforts then why don’t we perform the multi-browser testing using automation. TestNG gives us functionality to perform same test on different browsers in a simple and easy way.

How to do it…

1)Create your Script to test a LogIn application using TestNG class.

2) Pass ‘Browser Type’ as parameters using TestNG annotations to the before method of the TestNG class. This method will launch only the browser, which will be provided as a parameter.

public class MultiBrowser {

public WebDriver driver;

@Parameters("browser")

@BeforeClass

// Passing Browser parameter from TestNG xml

public void beforeTest(String browser) {

// If the browser is Firefox, then do this

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

driver = new FirefoxDriver();

// If browser is IE, then do this

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

// Here I am setting up the path for my IEDriver

System.setProperty("webdriver.ie.driver", "D:\Library\selenium\drivers\IEDriverServer.exe");

driver = new InternetExplorerDriver();

}

// Doesn't the browser type, launch the Website

driver.get("http://www.store.demo.com");

}

// Once Before method is completed, Test method will start

@Test public void login() throws InterruptedException {

driver.findElement(By.xpath(".//*[@id='account']/a")).click();

driver.findElement(By.id("log")).sendKeys("testuser_1");

driver.findElement(By.id("pwd")).sendKeys("Test@123");

driver.findElement(By.id("login")).click();

}

@AfterClass public void afterTest() {

driver.quit();

}

}

3) Create a TestNG XML for running your test. Configure the TestNG XML for passing parameters i.e. to tell which browser should be used for Running the Test.


<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="Suite" parallel="none">

<test name="FirefoxTest">

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

<classes>

<class name="automationFramework.MultiBrowser" />

</classes>

</test>

<test name="IETest">

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

<classes>

<class name="automationFramework.MultiBrowser" />

</classes>

</test>

</suite>


Note: You can set any number of Browsers here and just for the example purpose I have set up only two main
browsers.
TestNg will execute the test one by one. You may like to perform parallel tests,

Post Reply

Return to “Selenium WebDriver”