Mandar Kale
Mandar Kale

Reputation: 337

What is the best way to deal with multiple browsers in selenium?

Let me explain the problem statement : I want to design Page Object Model for a page using selenium. And the requirement is, scripts executing on multiple browsers will use this class. How should I deal with element locators in my Page class ? What I can think of is

  1. Get the driver object, and using if else, pick the browser specific XPath for locating the element . Like if the driver is chrome then locateChromeElement.

  2. Create different page classes for different browsers.

  3. Create base page class and extend it based on browsers.

What is the best way? What is used in industry?

Upvotes: 1

Views: 1001

Answers (2)

Pankaj Sharma
Pankaj Sharma

Reputation: 79

It depends entirely on your AUT (Application under test). If you have different locators for the same webelement on a page (e.g. in case of multi-lingual sites), then use properties file for storing the webelements and name it as per your page (e.g. for HomePage class you can have different files HomePage.properties (English), HomePage_it.properties (Italian) etc.)

Usually, if you go for CSS for location webelement, you will find it same for almost every browser.

Upvotes: 2

Gaurav Thantry
Gaurav Thantry

Reputation: 803

The xpaths will be the same regardless of which browser you use. to make a script work in multiple browsers, you can create multiple TestNg suites for different browsers, and have the same script for all the suites. All you need to change in the suites are the Browser classes. Consider the following script

You can run this entire code in one go. All the test suites will be executed one after the other

class MultipleBrowser{
 
 //for Firefox
 @Test
 public void FirefoxBrowser()
 {
 WebDriver driver = new FirefoxDriver();
 driver.get("http://www.google.com");
 driver.findElement(By.id("lst-ib")).sendKeys("Automating in firefox Browser");
 }
 
 //for ChromeBrowser
 @Test
 public void ChromeBrowser()
 {
  WebDriver driver = new ChromeDriver();  //only the class is changed from firefoxDriver to ChromeDriver
 driver.get("http://www.google.com");
 driver.findElement(By.id("lst-ib")).sendKeys("Automating in Chrome Browser");
 }
 
 //for InternetExplorer
 @Test
 public void IEBrowser()
 {
 WebDriver driver = new InternetExplorerDriver();  //only the class is changed from ChromeDriver to IEDriver
 driver.get("http://www.google.com");
 driver.findElement(By.id("lst-ib")).sendKeys("Automating in IE Browser");
 }
 }
 

Upvotes: 0

Related Questions