Problem
Imagine the following situation: after accepting a confirmation dialog you will be redirected to another page. This recipe will explain how to deal with this inevitable situation.
Prerequisites
We have made a class file for every unique page. So in theory every page is accessible.
How to do it…
We have to change the return-type of the method, in this case we set it to MemberPage. The doLogin method will look like this:
public MemberPage doLogin(String email, String password) {
WebElement emailEl = driver.findElement(By.name("email"));
emailEl.sendKeys(email);
WebElement passwordEl = driver.findElement(By.name("password"));
passwordEl.sendKeys(password);
WebElement loginForm = driver.findElement(By.name("login"));
loginForm.submit();
return new MemberPage();
}
1 2 3 4 5 6 7 8 9 |
public MemberPage doLogin(String email, String password) {
WebElement emailEl = driver.findElement(By.name("email"));
emailEl.sendKeys(email);
WebElement passwordEl = driver.findElement(By.name("password"));
passwordEl.sendKeys(password);
WebElement loginForm = driver.findElement(By.name("login"));
loginForm.submit();
return new MemberPage();
}
|
How it works…
The MemberPage object is returned, once we call the doLogin method. From our testscript perspective we can access all the public methods in the MemberPage class.
Ambiguous focus
Set the return-type to void, if the focus of an certain action is ambiguous. Like in highly dynamic websites, where we have no control on the input/output data. In this case we can use the code below:
public void doLogin(String email, String password) {
WebElement emailEl = driver.findElement(By.name("email"));
emailEl.sendKeys(email);
WebElement passwordEl = driver.findElement(By.name("password"));
passwordEl.sendKeys(password);
WebElement loginForm = driver.findElement(By.name("login"));
loginForm.submit();
}
1 2 3 4 5 6 7 8 |
public void doLogin(String email, String password) {
WebElement emailEl = driver.findElement(By.name("email"));
emailEl.sendKeys(email);
WebElement passwordEl = driver.findElement(By.name("password"));
passwordEl.sendKeys(password);
WebElement loginForm = driver.findElement(By.name("login"));
loginForm.submit();
}
|

Wonderful explanation! Helps a lot for beginners and freshers in this field and may be adding more examples and real time scenarios will make it still more informative and complete.