There are a variety of ways you can go about this. Do you know what the reference number is in advance? In other words, do you know that you are looking for row data for ref 24034?
If so, then you can find that TD on the page... and go up one level to the TR and get all descendent TDs... that way you'll have all the data for that particular row (you can use a variable in the repository to refer to the reference number).
Assuming your column headers/order are always the same and exactly match the header row and data row... you will know that the first TD in the TR is the date.
We actually have a method that does this for us, but I'm not sure how well explaining it over the forum will work.
What we have is a header row in the repository, and a data row in the repository (with a locator value finding some unique value in the row - like the ref in the example above).
We then "find descendants or children accordingly to make a list of the headers and data cells, pass that to the function as well as the column name we want and then we get the data back.
It might be a little difficult to understand at first, but once you have it, and the method, it's a great way to pull data from a web table. The order of the columns doesn't matter because you go by column name and not index.
Below is a copy of the code, followed by an example how to use it:
Code: Select all
public static string GetDataByColumn<T>(IList<T> columnRow, IList<TdTag> dataRow, string columnName) where T : WebElement
{
try
{
string cellValue = null;
List<T> columnHeaders = new List<T>(columnRow);
int index = columnHeaders.FindIndex(element => element.InnerText != null && element.InnerText.Trim() == columnName);
if (index != -1)
{
cellValue = dataRow[index].InnerText;
// Check if cell contains nested elements and retrieve innertext if there is only one nested element
List<Unknown> childElements = new List<Unknown>(dataRow[index].Children);
if (string.IsNullOrEmpty(cellValue) || childElements.Count != 0)
{
if (childElements.Count == 1)
{
cellValue = childElements[0].As<WebElement>().InnerText;
}
}
}
if(string.IsNullOrEmpty(cellValue))
{
return cellValue;
}
else
{
return cellValue.Trim();
}
}
catch (Exception ex)
{
throw new InternalTestException("Failed to get get data by column name for column '" + columnName + "' due to the following error: " + ex.Message + ex.StackTrace);
}
Then to use it:
Code: Select all
SSTRepository.Instance.locatorValue = orderNumberFromTest;
SSTRepositoryFolders.ApprovalAppFolder approvalObjects = SSTRepository.Instance.Approval;
IList<TdTag> columnHeaders = approvalObjects.RowHeaderRow.FindChildren<TdTag>();
IList<TdTag> dataCells = approvalObjects.RowDataRow.FindChildren<TdTag>();
SSTPayments paymentFromApplication = new SSTPayments();
paymentFromApplication.OrderNumber = Utilities.GetDataByColumn(columnHeaders, dataCells, "Order Number");
paymentFromApplication.AccountName = Utilities.GetDataByColumn(columnHeaders, dataCells, "Account");
paymentFromApplication.TransactionType = Utilities.GetDataByColumn(columnHeaders, dataCells, "Transaction Type");
paymentFromApplication.SettlementCurrency = Utilities.GetDataByColumn(columnHeaders, dataCells, "Currency");
paymentFromApplication.InitiatedDate = Convert.ToDateTime(Utilities.GetDataByColumn(columnHeaders, dataCells, "Initiated Date"));
paymentTotal = Convert.ToDecimal(Utilities.GetDataByColumn(columnHeaders, dataCells, "Amount"));
numberOfItemsFromApplication = Utilities.GetDataByColumn(columnHeaders, dataCells, "Number Items");
So you can see, but using Convert.ToDecimal(Utilities.GetDataByColumn(columnHeaders, dataCells, "Amount"));I can pull whatever value is in the "Amount" column by passing in my list of columnHeaders and dataCells along with the column name I'm want to look at (after setting my unique locator value).
This is great if you want everything in the entire row. If you only cared about an image you need to click, then you can create a repository entry for the image itself, and use the ref# as a locator variable in your repository.
I hope it's clear enough, again hard to explain over the forum.
Carson.
EDIT: I should add, this is how our data row looks like in the repository (basically we are saying find the cell that has this certain value in it, and then go up to the row, and give me that row back):
Code: Select all
.//td[@innertext=$locatorvalue]//..//tr
And the header row:
Code: Select all
.//td[@innertext~'Select']//..//tr
Depending on how "nicely" your webpage is coded will depend on how cleanly you can set up your repository. You can see that we have a variable in our data row ($locatorvalue), and we just put our orderId in there:
Code: Select all
SSTRepository.Instance.locatorValue = orderNumberFromTest;
then we have the entire row based on the order ID we're looking at. Note that our search values are pretty generic (just looking at the innertext of everything), but it's part of a rooted folder that narrows down the search considerably. You want to make your repository entry as narrow as possible to speed up finding the item. Hope that helps!