Today, I’ll update the selenium automation test to check whether the user exists before adding. The general algorithm goes like this:
- Use DataTables search on email address
- Check the table rows to see if the user already exists
- If the user does not exist, then add the user
- 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 | public void addUser(WebDriver driver, String email, String givenName, String surname) |
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 | List<WebElement> rows = driver.findElements(By.cssSelector("#users tbody tr")); |
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 | WebElement openModal = driver.findElement(By.xpath("//button[contains(text(),'Add User')]")); |
Now we can just add another method to add multiple users.
1 | public void addUsers( WebDriver driver ) |
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