Verifying Sortable Tables with Selenium for Java
Verifying Sortable Tables with Selenium for Java
Learn how to sort and verify tables with Selenium for Java
Need to verify a sortable table?
No worries. I have you covered in this recipe. The tricky thing about sortable tables is that not only does the column being sorted change, but also the related cells change as well.
For example, if I sort this table by Name, the fruits and veggies are now in alphabetical order, but you must also make sure that the Type and the Flavor remains with the proper food or vegetable.
Let’s write a test for this. For starters, we’ll grab the locator for this table, “fruits-vegetables”.
How to Verify a Sortable Table with Selenium Code
To verify a table with Selenium for Java, we first have to declare our “table” as a variable.
Then, we’ll say driver.findElement (By.id()) and specify the ID of the table.
Now we want to click the name column. Let’s get the locator for that, which is “column-button-name”. So, we’ll say table.findElement(By.id()) and we’ll specify the ID of that column.
And then we’re going to say .click. Great, that sorts the table.
Now let’s print out the table values so that we can manually verify its output. We’ll say System.out.println(table.getText()).
All right, we’ll run this and here’s the output. We see the columns, which are Name, Type, and Flavor, and then each of those roles is on a new line.
Let’s just quickly verify. Yep, the apple is a fruit and it’s sweet. Okay, all of this looks great.
What we’re going to do now is copy this and we’re going to store this as our expected value.
We’ll say the expectedTableValues…
I’m going to store this using a text block. If you don’t have Java 13 or later, then you won’t have access to text blocks and you can use just a regular String.
So, I’m going to paste our expected values inside of here and let’s get rid of all the extra space there.
Okay, great. Now we can write an assertion using assertEquals to compare this expected value with the actual one in future regression runs.
Awesome, the test passed.
While this is fine for smaller tables, if there were dozens of roles, our test code would get pretty long here. And it’s not really good to attempt to maintain the entire table’s data within our test code.
Not only that, but we can’t be sure that the table still actually looks correct as we’re only verifying the data and not the appearance. For tasks such as these, I prefer to use visual testing.
How to Do Visual Testing on Sortable Tables with Selenium Java
Applitools is the most sophisticated visual testing tool and can take a screenshot of our table every time this test runs and make sure that all the data is correct. It also verifies that visually it appears just as we want.
To verify this using Applitools, we would simply do a new Eyes instantiation. So, Eyes is the name of the Applitools API. And then we have 3 commands — open, checkWindow, and close.
- We say, open your eyes with eyes.open(). Now we pass in the driver, the name of our application which is “The Kitchen”, and then the name of our test. We’ll say, “sort by name”.
- Now for the magic, we say eyes.checkWindow(). This’ll take a screenshot of the window and it’ll send it up to the Applitools Cloud.
- Then the final command is to close your eyes with eyes.close
And we’re done, this will do a visual check.
By doing this, since it’s going to take a picture of the application, we no longer need this assertion, nor do we need to store our test data.
So, our test becomes a lot more maintainable. Let’s run this one just to make sure it works as well.
Wonderful, our test has passed, and I’ll show it to you in the dashboard.
As you can see here, we have a baseline image from the very first time I ran the test. Then for every regression run, it’ll take a new screenshot and compare the 2 using machine learning.
Resources
- Git Repo – TableTests.java
- Applitools Kitchen – Testing Sortable Tables
- Test Automation University – Automated Visual Testing with Java
- Applitools Eyes SDK for Selenium – npm @applitools/eyes-selenium
Schedule a free Applitools Demonstration
Request DemoHere’s the code sample from our tutorial on how to sort and verify tables with Selenium for Java