1

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?

1
  • No need to create the different class for each browser because most of the xpath or other locators will work in all browser. one few objects may require different xpath, we can use multiple xpaths or locator for single element in page objects. Commented Sep 6, 2017 at 9:35

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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"); } } 

1 Comment

Thanks for the reply, but my problem is dealing with multiple browsers in Page class and not in the test script/suite.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.