5 ms·
You're advocating to turn this: public class Login { public void testLogin() { // fill login data on sign-in page driver.findElement(By.name("user_n
by edmcnulty101 5y ago
You're advocating to turn this:
public class Login {
public void testLogin() {
// fill login data on sign-in page
driver.findElement(By.name("user_name")).sendKeys("userName");
driver.findElement(By.name("password")).sendKeys("my supersecret password");
driver.findElement(By.name("sign-in")).click();
// verify h1 tag is "Hello userName" after login
driver.findElement(By.tagName("h1")).isDisplayed();
assertThat(driver.findElement(By.tagName("h1")).getText(), is("Hello userName"));
}
}
Into this?:
public class SignInPage {
protected WebDriver driver;
// <input name="user_name" type="text" value="">
private By usernameBy = By.name("user_name");
// <input name="password" type="password" value="">
private By passwordBy = By.name("password");
// <input name="sign_in" type="submit" value="SignIn">
private By signinBy = By.name("sign_in");
public SignInPage(WebDriver driver){
this.driver = driver;
}
/**
* Login as valid user
*
* @param userName
* @param password
* @return HomePage object
*/
public HomePage loginValidUser(String userName, String password) {
driver.findElement(usernameBy).sendKeys(userName);
driver.findElement(passwordBy).sendKeys(password);
driver.findElement(signinBy).click();
return new HomePage(driver);
}
}
/**
* Tests login feature
*/
public class TestLogin {
@Test
public void testLogin() {
SignInPage signInPage = new SignInPage(driver);
HomePage homePage = signInPage.loginValidUser("userName", "password");
assertThat(homePage.getMessageText(), is("Hello userName"));
}
}
- exdsq 5y agoThere are better ways to write the pattern but the idea is having the elements in one place so if the site changes you can update them quickly, and then in your code accessing them by variable name. It's much cleaner, easier to edit if they change the UI (which happens) or if they use a web framework like React that has strange elements to access.
- edmcnulty101 5y agoI don't know. I guess that's subjective. I would personally rather work on code with much less abstraction. All of the Java codebases I've worked with, that have 20 levels of hierarchy to 'clean up' the code bases and make it easier to edit, turn into giant balls of spaghetti at some point.
- exdsq 5y agoI normally agree with code being subjective but with test code around the UI I'd argue actually that this is a good abstraction. For example this code would completely break if they added another div with text on a page. It'd also completely break the Page Object Model. But imagine there are 50 tests covering these pages. In the iterative example you'd need to update the divs for all 50 tests, with a page object model you'd need to update them in one place and the tests would then be updated. I appreciate this argument might come down to "trust me, I've seen this" but it really is a good abstraction to look at if you need to write selenium or cypress tests.
- edmcnulty101 5y agoSelenium is admittedly not one of my major skillsets.. so I'll defer to your seemingly more knowledgable judgement on this. :) Abstraction can be very useful, but it can also definitely lead to spaghetti if it gets out of hand or is done incorrectly.