DataTables and Selenium

Today, I’ll update the selenium automation test to check whether the user exists before adding. The general algorithm goes like this:

  1. Use DataTables search on email address
  2. Check the table rows to see if the user already exists
  3. If the user does not exist, then add the user
  4. If the user does exist, then clear the search

I start by using DataTables search - DataTables adds pagination, searching on the email address should narrow things to one page. If I was truly paranoid, then I’d also increase the number of entries on the first page. As before, I explicitly wait for the DataTables search input element to become visible. Once visible, I enter the email address into the search element.

1
2
3
4
5
6
public void addUser(WebDriver driver, String email, String givenName, String surname)
{

WebDriverWait block = new WebDriverWait(driver,10);
WebElement search = block.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#users_filter input")));
search.clear();
search.sendKeys(email);

Then I get all the displayed rows in the table and test the email address in each row. I refresh the page to clear the search before returning on a found user or continuing on to add the new user - there ought to be an easier way, but I wasn’t able to suss it out.

1
2
3
4
5
6
7
8
9
10
List<WebElement> rows = driver.findElements(By.cssSelector("#users tbody tr"));
for ( WebElement row : rows ) {
WebElement colEmail = row.findElement(By.cssSelector("td"));
System.out.println("Compare " + email + " to " + colEmail.getText() );
if ( colEmail.getText().equals(email) ) {
driver.navigate().refresh();
return;
}
}
driver.navigate().refresh();

Now we just add the user as before. I explicitly test for the modal to disappear before returning to protect whatever automation step comes next.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
WebElement openModal  = driver.findElement(By.xpath("//button[contains(text(),'Add User')]"));
openModal.click();

WebElement modal = block.until(ExpectedConditions.visibilityOfElementLocated(By.id("add-user-modal")));
WebElement input;
input = modal.findElement(By.id("add-user-email"));
input.sendKeys(email);
input = modal.findElement(By.id("add-user-givenName"));
input.sendKeys(givenName);
input = modal.findElement(By.id("add-user-surname"));
input.sendKeys(surname);

input = driver.findElement(By.id("user-post"));
input.click();

block.until(ExpectedConditions.invisibilityOfElementLocated(By.id("add-user-modal")));
}

Now we can just add another method to add multiple users.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public void addUsers( WebDriver driver )
{

addUser(driver, "george@example.com", "George", "Washington");
addUser(driver, "john@example.com", "John", "Adams");
addUser(driver, "thomas@example.com", "Thomas", "Jefferson");
addUser(driver, "james@example.com", "James", "Madison");
addUser(driver, "james2@example.com", "James", "Monroe");
addUser(driver, "john2@example.com", "John Q", "Adams");
addUser(driver, "andrew@example.com", "Andrew", "Jackson");
addUser(driver, "martin@example.com", "Martin", "Van Buren");
addUser(driver, "william@example.com", "William", "Harrison");
addUser(driver, "john3@example.com", "John", "Tyler");
addUser(driver, "james3@example.com", "James", "Polk");
addUser(driver, "zachary@example.com", "Zachary", "Taylor");
addUser(driver, "millard@example.com", "Millard", "Fillmore");
addUser(driver, "abraham@example.com", "Abraham", "Lincoln");
}

The test automation step will add the user on first execution and simply test for user existence on subsequent execution.

1
mvn test

The full source for this version of Jersey, Gson and DataTables is on github.

07 Jun: Bootstrap Modals and Selenium
21 Jun: jQuery Ajax and Selenium