1

I am trying to handle dynamic label names in Selenium.

Say I have:

Website 1 > Login page > Username : TextBox 
Website 2 > Login page > User     : TextBox
Website 3 > Login page > Login    : TextBox

So there are 3 different types of login label names and obviously their textbox locator names or ids will also be different. How do I keep it generic so that the code directly focuses on the login textbox for entering username irrespective of different id or name or xpath for the textbox.

Any suggestions would be of great help.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Bishwaroop
  • 33
  • 1
  • 10

2 Answers2

2

This is where Page Object pattern would help you.

The idea here would be to define a LoginPage class (example) that would have "login", "password" fields, "submit" button and everything else needed. You can either have a constructor define the website-specific locators, or subclass the LoginPage and have WebSite1LoginPage, WebSite2LoginPage etc classes with custom locators.

In any case, this approach would help you to abstract away the login page and hide implementation/location details making your tests easy to read and support.

See also:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
0

If the path to the login field between all 3 windows/sites dont share a common path that will uniquely map to the element, then you have no choice but define 3 elements...

Given the example you provided, a way to reduce duplication is paramertise the "username,user,login" bit based on the website you are hitting...

something like:

if website1
  login = "username"
elsif website2
  login = "user"
elsif website3
  login = "login"
end

element = find("xpath#{login}xpath")
Pippo
  • 905
  • 11
  • 22