0

I am needing some help being able to take database results and verify they are displaying on the screen correctly. The table will be as big or little as the amount of data in the database.

Here's some sample HTML for the table:

<table class="table table-hover">
<thead>
<tbody>
<tr class="ng-scope" ng-repeat="item in PaymentHistoryCtrl.paymentHistoryArray | orderBy: 'date' track by $index ">
<td class="ng-binding" ng-bind="item.date | date:'longDate'">February 12, 2016</td>
<td class="ng-binding" ng-bind="item.policyNumber">BTB1716401</td>
<td class="ng-binding" ng-bind="item.total | currency">$3,000.00</td>
<td class="ng-binding" ng-bind="item.referenceNumber">3***8431</td>
</tr>
<tr class="ng-scope" ng-repeat="item in PaymentHistoryCtrl.paymentHistoryArray | orderBy: 'date' track by $index ">
<td class="ng-binding" ng-bind="item.date | date:'longDate'">February 12, 2016</td>
<td class="ng-binding" ng-bind="item.policyNumber">BTB1716401</td>
<td class="ng-binding" ng-bind="item.total | currency">$55.00</td>
<td class="ng-binding" ng-bind="item.referenceNumber">3***8431</td>
</tr>

Here's a sample visual of the table:

enter image description here

I have a sql query to gather the information displayed on the screen. How would I go about matching up the records with the rows/columns in the table, though? Here's the java I'm using for connecting to the db:

//Load the required JDBC Driver class
Connection conn = null;
Statement stmt = null;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  

//Create a connection to the database
String connection = "jdbc:sqlserver://" + dbName + ":" + dbPort;
conn = DriverManager.getConnection(connection,dbUsername,dbPassword);

//Execute SQL query
stmt = conn.createStatement();
String sql = "select * from Table";
ResultSet rs = stmt.executeQuery(sql);
1
  • Consider writing a PageObject that can handle the table data. Commented Feb 22, 2016 at 15:57

2 Answers 2

1

It is rather difficult to understand what is generating the html you provided, but if you have full control over the generation of each data row, you can assign an ID to each element. This is especially helpful when you are creating Jasper reports, which turn out to be an html table.

Let's say your table row looks like this:

<tr id="result_1" class="ng-scope" ng-repeat="item in PaymentHistoryCtrl.paymentHistoryArray | orderBy: 'date' track by $index ">
    <td id="result_1_date" class="ng-binding" ng-bind="item.date | date:'longDate'">February 12, 2016</td>
    <td id="result_1_policyNum" class="ng-binding" ng-bind="item.policyNumber">BTB1716401</td>
    <td id="result_1_total" class="ng-binding" ng-bind="item.total | currency">$3,000.00</td>
    <td id="result_1_refNum" class="ng-binding" ng-bind="item.referenceNumber">3***8431</td>
</tr>

What you can do then, is make a simple call to the selenium webDriver to get the text of an element.

WebElement element = driver.findElement(By.id("result_1_total"))

If this is not the case, or if you do not care about the individual <td> elements, aside from the fact that they only exist on the page, you can simply loop through them.

//find the table on the page
WebElement tblEle = driver.findElement(By.id("myTable"))
List<WebElement> tblElements = tblEle.findElements(By.Xpath("/tbody/tr/"));
for (WebElement tblElementRow : tblElements) {
    List<WebElement> allTds = tblElementRow.findElement(By.xpath("td"))
    // do somthing with the list of allTds
}

Also, if I were you, instead of doing an stmt.executeUpdate(), I would do a stmt.executeQuery, which returns a resultset. This way, you can loop through the results, and add them to the array. When the array is filled, you can add simple mechanisms to it, to relate it to the table you generated.

Ex:

List<MyObject> objs = new ArrayList<>();
ResultSet rs = stmt.executeQuery("Select * from Table");
while (rs.next()) {
    // construct obj from rs
    objs.add(myCreatedObj);
}

MyObject findFromObjs(final String id) {
    for (MyObject cool : objs) {
        if (cool.getId().equals(id)) return cool
    }
}

then, when you do a call this function:

WebElement element = driver.findElement(By.id(findFromObjs("myFirstRs")))

Again, this all depends on how much power, granularity, and control you have. That will determine how you can approach this problem. When dealing with technologies that do not make absurd xpaths, sometimes, it is easier to hook onto the Id's. On the contrary, if you cannot assign IDs, you can also use xpath, but it might be a bit more difficult to use.

Lastly, a simple way to check your xpath (if that is what you plan on using) is to run it in console in Firefox. Something like this would be a good start:

$x("//tbody/tr/td[contains(text(), 'news')]")
Sign up to request clarification or add additional context in comments.

Comments

0

This answer makes a lot of assumptions about how the data from the database is returned. I am going to assume that the rows returned from your SQL statement will be in the same order as what is being shown in your web table. I am also assuming that the columns from your sql result matches up with the columns being displayed on your web table, if not, you'll need to crop your sql query to only select the columns visible in the web table. Your process would look something like this:

  1. Loop through each row in sql result, call it iterator i
  2. Loop through each cell in row, call it iterator j
  3. Get text from table via XPath: By.XPath("//table/tbody/tr["+(i+1)+"]/td["+(j+1)+"]").Text
  4. Compare text returned from td with value stored within sql cell.
  5. Return false if any comparison fails
  6. Return true if the for loop ends.

If the rows returned from your SQL statement are in a different order you have 2 options, add an ORDER BY to your sql statement or apply similar functionality to your web table so that the order will match. Or alternatively, compare every row against the current row in the i for loop. I don't recommend the second one as it will slow down a lot if you have a lot of data.

Hope that helps

1 Comment

No this was years ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.