I have a table with list of items and its count displayed on it. If the count of it is ">0" then it will be a linkable text. But if it is "==0" then it will be a normal span text. I need to validate if it is '0', then the application is displayed that control as normal text content and if it is ">0" then its a clickable (hyperlink) control. Any help on how to validate from the displayed input and tag.
The sample html content for the last rows of the above table is given.
<tr data-ng-repeat="task in $ctrl.clientPortalAdminDashboardState.taskList track by $index" class="ng-scope">
<td>
<span class="ng-binding">
Pending bank account verification
</span>
</td>
<td>
<!-- ngIf: $ctrl.checkEntitlementsForTask(task) -->
<a class="pull-right ng-binding ng-scope" data-ng-if="$ctrl.checkEntitlementsForTask(task)" href="" data-ng-click="$ctrl.onTaskItemCountClick(task.type)">
1
</a><!-- end ngIf: $ctrl.checkEntitlementsForTask(task) -->
<!-- ngIf: !$ctrl.checkEntitlementsForTask(task) -->
</td>
</tr>
<tr data-ng-repeat="task in $ctrl.clientPortalAdminDashboardState.taskList track by $index" class="ng-scope">
<td>
<span class="ng-binding">
EWS Manual Verification Pending
</span>
</td>
<td>
<!-- ngIf: $ctrl.checkEntitlementsForTask(task) -->
<!-- ngIf: !$ctrl.checkEntitlementsForTask(task) -->
<span class="pull-right ng-binding ng-scope" data-ng-if="!$ctrl.checkEntitlementsForTask(task)">
0
</span><!-- end ngIf: !$ctrl.checkEntitlementsForTask(task) -->
</td>
</tr>
CodePudding user response:
Accordingly to the sample HTML you presented when it is just a 0 text the element will have a span
tag name while in case of non-zero value and a link the element will have a
tag name and href
attribute.
So, I can't be sure about correctness of the following locator, but accordingly to what you have shared the locator for all the clickable links will be something like this:
//tr[@ata-ng-repeat]//a[@href]
CodePudding user response:
You haven't posted your code with the issues you are running into so we can't help solve that issue. You also haven't posted valid sample HTML, e.g. TRs without TABLE tags are invalid. Skipping over all that...
This is how I would do it. I've run this code against the incomplete HTML you provided and it works.
List<WebElement> rows = driver.findElements(By.cssSelector("tr"));
for (WebElement row : rows) {
WebElement count = row.findElement(By.cssSelector(".pull-right.ng-binding.ng-scope"));
if (count.getText().trim().equals("0")) {
Assert.assertEquals(count.getTagName(), "span");
} else {
Assert.assertEquals(count.getTagName(), "a");
}
}
Basically I'm grabbing all the TR tags and then looping through each one looking for the count. If the count is "0", assert that the tag is a SPAN. Otherwise assert that the tag is an A.
BTW, I'm using JUnit for the asserts. You should be using some library like JUnit or TestNG to do your asserts/validations, if you aren't already. If you are using something other than JUnit, you'll have to switch the asserts to whatever library you are using.