1

It's more like a theoretical question and I am seeking the best practice here.

Given an application where you have different user types, newly registered users get a new UI while old users are getting the old UI.

When you login with a user you are getting into a main page where your user can do stuff, but the two UIs are very much different (different buttons, elements with different locators) except search bar and some others.

I have thought about a few options here:

a. Create seperate page objects (AMainPage, BMainPage) for the two landing pages which both implements an IMainPage interface and use them like

LoginPage loginPage = new LoginPage(GetDriver());
IMainPage mainPage = loginPage.loginWithUser(user);

And then implement the login something like

public IMainPage loginWithUser(User user)
{    
//implement the actual action
//return page object based on user.UserType (e.g. AMainPage or BMainPage)
}

b. Similar to the first but with an abstract parent which implements the actions where the two UIs are the same, and define other actions where it differs.

c. Use one common page object to implement all actions for both UIs.

Or something else could be better? Suggestions?

2 Answers 2

1

(c) clearly breaks the Single Responsibility Principle (SRP) (a) would result in duplicated code, which is solved by (b).

A few other things you can do would depend on how you control the environment.

You can have different login page objects for each user type, which implement the method login returning the user-specific main page. This way, you replace a if/switch for polymorphism, which is more extendable.

Then a factory can create the specific Login Page based on test data (data provider).

1
  • Thanks for your reply. "You can have different login page objects for each user type" Maybe I'm misunderstanding you, but both users have the same login page; if I'd create a seperate login page for each usertype it would also be code duplication. Also, one another thing I wanted to ask. If I go with solution B, then I would have to inject the usertype to every action which returns to the Main Page, just to return with AMainPage, BMainPage based on usertype, which would end up in a lot of if/else statements and yet again, duplication. Do you have any suggestions the handle this?
    – dtoman
    Commented Jun 10, 2019 at 20:46
1

Your solution is good.

But if it's only different from locators but same steps. I usually use only xpath and separate string of locators from each page.

public class aLogin{
string str_username = 'xpath to username text box of page A'; 
string str_password = 'xpath to password text box of page A';
}

public class bLogin{
string str_username = 'xpath to username text box of page B'; 
string str_password = 'xpath to password text box of page B';
}
public class Login {
WebDriver driver;

By userNameATxt = By.xpath(aLogin.str_username);
By passWordATxt = By.xpath(aLogin.str_password);

By userNameBTxt = By.xpath(bLogin.str_username);
By userNameBTxt = By.xpath(bLogin.str_password);

By loginButton = "";

public Login(WebDriver driver){

    this.driver = driver;

}

//Set user name in textbox

public void setUserName(By userName){
    driver.findElement(userName).sendKeys(strUserName);
}

//Set password in password textbox
public void setPassword(By password){
     driver.findElement(password).sendKeys(strPassword);
}

//Click on login button
public void clickLogin(){
        driver.findElement(loginButton).click();
}

public void loginPage(By userNameTxt, By passWordTxt){
    //Fill user name
    this.setUserName(userNameTxt);

    //Fill password
    this.setPassword(passWordTxt);
    //Click Login button

    this.clickLogin();        
}
}

public void runTestCaseLogin(){
Login login = new Login();
login.loginPage(login.userNameATxt,login.passWordATxt)
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.