Dealing with moving focus

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

 

Dealing with moving focus
0 votes, 0.00 avg. rating (0% score)

One thought on “Dealing with moving focus

  1. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>