Showing Posts From

Java

Verify record in HTML table

Verify record in HTML table

Though not very often encountered, this again is an interesting problem to solve in automation testing. It’s quite obvious to come across table validation as an automation tester and there are quite a lot of options to do that. The most common verifications includeVerifying a unique identifier in a columnVerifying a specific text (cell) in a table based on a unique identifierVerifying a specific row (multiple values) in a table based on multiple identifiers (whether row exists)Hosted table for testing here and HTML Table source hereThe basic solution, as discussed here, solves 80% of all the listed problems. However, that only works when the table has a unique identifier. The solution for points 1 and 2 are already part of the previous post. So, let’s see how to solve the next complex problem # 3 This is again not a complex one to understand, however quite complex to validate with just xpath. There is still an option to validate this with some basic xpath’s and handling the validation part in the script/code but we are not gonna see that here for the same reason mentioned in my previous post. And it’s way too costly in this case. To make things clear, the problem that we intend to solve here is We have a table with multiple columns and rows; and our task is to validate whether a record exist with 3 or more values in a specific order in a row, or in other words, under specific headers. And we intend to solve this using only xpath i.e., without iteration in the code. Let’s take on one of the complex records to find in the table. In our example, let’s plan to find the value CodePen under the header Sites and 2002 under the header Views and 4135 under the header Clicks For our initial test, let’s first assume that we already know the index of the header column for each of these values that we need to verify in which case, the xpath would be something like //tr[td[.='CodePen' and position()=1] and td[.='2002' and position()=2] and td[.='4135' and position()=3]]As you can see, the position() keyword comes in handy in this case. So, the actual code to validate these values would be String expectedValue1 = "CodePen"; String expectedValue2 = "2002"; String expectedValue3 = "4135";String derivedXpath = "//tr[td[.='" + expectedValue1 + "' and position()=1] and td[.='" + expectedValue2 + "' and position()=2] and td[.='" + expectedValue3 + "' and position()=3]]";boolean isElementPresent = driver.findElements(By.xpath(derivedXpath)).size() > 0;if (isElementPresent) { // Do something after test step is passed } else { // Do something after test step is failed }Moving on to the next part where you do not know the position of each of the headers from which you need to validate the value. If you have already read my previous post on verifying text in HTML table, you might probably have guessed that we can use the count() operator to find the index! And guess what, that is correct 🙂 However, this time instead of verifying the existence of the header, we will be using a different approach. We will still use the count() operator, but in conjunction with number() and boolean() operators to validate whether the header exists. The xpath to get the position of a particular header would be count(//th[.='Clicks']/preceding-sibling::th)+number(boolean(//th[.='Clicks']))The above xpath will get the count of preceding siblings with tag name ‘th’ and then adds the boolean value for the existence of the object. Basically, the number() operator returns 1 (true)/0 (false) based on the input passed into it and the boolean() operator returns true/false based on the object existence. In simple termsboolean(Object) – true // Object is any matching tag from the given xpathboolean(null) – false // null is no matching tag from the given xpathnumber(true) – 1number(false) – 0The new xpath would be //tr[td[.='CodePen' and position()=count(//th[.='Sites']/preceding-sibling::th)+number(boolean(//th[.='Sites']))] and td[.='2002' and position()=count(//th[.='Views']/preceding-sibling::th)+number(boolean(//th[.='Views']))] and td[.='4135' and position()=count(//th[.='Clicks']/preceding-sibling::th)+number(boolean(//th[.='Clicks']))]]Hope you get the idea. If so, all you gotta do is to parameterize the position values in the above code. The new code would now be String expectedHeader1 = "Sites"; String expectedHeader2 = "Views"; String expectedHeader3 = "Clicks";String expectedValue1 = "CodePen"; String expectedValue2 = "2002"; String expectedValue3 = "4135";String derivedXpath = "//tr[td[.='" + expectedValue1 + "' and position()=count(//th[.='" + expectedHeader1 + "']/preceding-sibling::th)+number(boolean(//th[.='" + expectedHeader1 + "']))] and td[.='" + expectedValue2 + "' and position()=count(//th[.='" + expectedHeader2 + "']/preceding-sibling::th)+number(boolean(//th[.='" + expectedHeader2 + "']))] and td[.='" + expectedValue3 + "' and position()=count(//th[.='" + expectedHeader3 + "']/preceding-sibling::th)+number(boolean(//th[.='" + expectedHeader3 + "']))]]";boolean isElementPresent = driver.findElements(By.xpath(derivedXpath)).size() > 0;if (isElementPresent) { // Do something after test step is passed } else { // Do something after test step is failed }It’s a quite complex xpath to derive when it comes to validating dynamic values with multiple headers and values, but considering the amount of time this xpath saves in execution, I would say “worth it!”

Verify text in HTML table

Verify text in HTML table

For any automation tester, this is a common task/problem to solve. Almost every automation project will have a verification point, which involves verifying a value in a table Before we move on to the solution for our problem, let us create a sample HTML table, so that the work is not just theoretical.Hosted table for testing here and HTML Table source hereAssuming that you already know which column to loop through, and all you need to check is that whether the identifier exist, the normal approach would be to get the column that you need to check the value for, and then loop through the values until you find the expected value that you are looking for. Pretty straight forward. However, sometimes it gets a little complex where you need to find the index of the header and then find the value in that column. Something like below WebElement table1HeaderRow = driver.findElement(By.xpath("//table/thead/tr")); List<WebElement> headerValues = table1HeaderRow.findElements(By.tagName("th"));String expectedHeader = "Clicks"; String expectedValue = "2961";int i = 1; for(WebElement e : headerValues) { if(e.getText() == expectedHeader) break; i++; } // variable i will have the matching header here // now verify value in all rows in this columnList<WebElement> values = driver.findElements(By.xpath("//table/tbody/tr/td[" + i + "]")); // manipulate xpath of the matching column and get all values from the matching column boolean match = false; // default match to false, so that no match will be considered failurefor(WebElement e : values) { if(e.getText() == expectedValue) { match = true; break; } } if (match) { // Do something after test step is passed } else { // Do something after test step is failed }Let's see the problem with the above code, before we move on to the possibly different solution. One of the significant problem with the above code is the execution time. Though we know the header and value, we still are verifying each element in the column (header) and row (value) to verify its existence, instead of directly searching the DOM for the presence of the element. Facts and Assumptions We have two values here. One is the header and the other is the value in the header column. In most of the cases, the value will be dynamic, which means, we would not be able to derive the xpath before execution, and has to be derived during runtime. Therefore, we will assume that the value is always dynamic and the header may/may-not be dynamic. Let’s look at both the cases one-step at a time. Static Header and Dynamic Value Here, let’s assume that the header column is something that we already know. In this case where we already know the index of the header column (column number of header) we can do something like below List<WebElement> values = driver.findElements(By.xpath("//table/tbody/tr/td[3]"));Since, we do not have to search for the header column; we already skipped one loop in the code. So what’s next? Let’s see if we can skip the next loop. During the execution, once we find the value (2961) to be verified, the xpath could now be manipulated as //table/tbody/tr/td[3 and .='2961']The code can now be changed as boolean isElementPresent = driver.findElements(By.xpath("//table/tbody/tr/td[3 and .='2961']")).size() > 0;As you can see, the variable isElementPresent will now have the validation result without looping through all the values Dynamic Header and Dynamic Value What happens if we do not know the index of the header column and we need to find the header column number dynamically? Here is where things get interesting! As xpath does not directly support indexing, we would need to tweak our xpath with interesting features that xpath already provides. Let’s start by tackling the index. Xpath provides an option to find the index of the current node by manipulating the count() method. We can get the index of the node that we are searching for using the below xpath. count(//table/thead/tr/th[.='Clicks']/preceding-sibling::th)+1The above xpath will give the index of the Clicks column. Since, the index starts with 0, we are incrementing the index by 1 to find out the actual index (3). We can now get the derived xpath by using the xpath as //tbody/tr/td[count(//table/thead/tr/th[.='Clicks']/preceding-sibling::th)+1 and .='2961']However, there is a caveat in this xpath. The index of the first header will be returned as 1 and the index of a non-existing header will also return 1 i.e., count(//table/thead/tr/th[.='Sites']/preceding-sibling::th)+1returns 1 and count(//table/thead/tr/th[.='Rank']/preceding-sibling::th)+1will also return 1 In other words, the below xpath’s //tbody/tr/td[count(//table/thead/tr/th[.='Sites']/preceding-sibling::th)+1 and .='LinkedIn']//tbody/tr/td[count(//table/thead/tr/th[.='Rank']/preceding-sibling::th)+1 and .='LinkedIn']Will return the same node (row # 4 in column 1), however, the second xpath (with Rank header) does not exist That brings us to the next problem which is to verify whether the actual heading exist before deriving the index. Now, how do we do that? The trick is to find the header column first, then navigate to it's ancestor, then to the ancestor's sibling and derive actual index (above index manipulation code) appended by value. Sounds confusing, but it’s worth a try! The new xpath would be //table/thead/tr/th[.='Clicks']/ancestor::thead/following-sibling::tbody/tr/td[count(//table/thead/tr/th[.='Clicks']/preceding-sibling::th)+1 and .='2961']The above xpath first searches for the existence of the actual heading that we are searching for (Clicks column), then moves up to the 'thead' tag (ancestor) and then moves down to the 'tbody' sibling (following-sibling), then takes the header index by manipulating the count() operator followed by the condition with the value (2961) To check the correctness of the xpath, let’s validate the above xpath for a different value for a different header. To validate row # 4 in column 1, the xpath would be //table/thead/tr/th[.='Sites']/ancestor::thead/following-sibling::tbody/tr/td[count(//table/thead/tr/th[.='Sites']/preceding-sibling::th)+1 and .='LinkedIn']Now changing the header value to Rank which does not exist, let’s try again with the below xpath //table/thead/tr/th[.='Rank']/ancestor::thead/following-sibling::tbody/tr/td[count(//table/thead/tr/th[.='Rank']/preceding-sibling::th)+1 and .='LinkedIn']The above xpath will not return any node as Rank header does not exist. Finally, the new code would now be String expectedHeader = "Clicks"; String expectedValue = "2961";String derivedXpath = "//table/thead/tr/th[.='" + expectedHeader + "']/ancestor::thead/following-sibling::tbody/tr/td[count(//table/thead/tr/th[.='" + expectedHeader + "']/preceding-sibling::th)+1 and .='" + expectedValue + "']";boolean isElementPresent = driver.findElements(By.xpath(derivedXpath)).size() > 0;if (isElementPresent) { // Do something after test step is passed } else { // Do something after test step is failed }And that’s how we deal with dynamic headers and dynamic values in HTML table. Ending Note: If you have to validate specific values in a table, just remember that you don't have to loop through the entire table! Hungry for more. Bon appetit!