Count the positions and see if it matches with total

I want to count the positions and see if it matches with the total
Here is my code:
WebUI.click(findTestObject(‘Object Repository/Page_Dashboard/a_Rostering’))

WebUI.click(findTestObject(‘Object Repository/Page_Dashboard/a_Planned Roster’))

WebUI.click(findTestObject(‘Object Repository/Page_Planned Roster/td_MAG’))

WebUI.click(findTestObject(‘Object Repository/Page_Planned Roster/td_MAG’))

WebUI.click(findTestObject(‘Object Repository/Page_Planned Roster/td_MAG’))

WebUI.click(findTestObject(‘Object Repository/Page_Planned Roster/td_MAG’))

WebUI.click(findTestObject(‘Object Repository/Page_Planned Roster/td_MAG’))

WebUI.click(findTestObject(‘Object Repository/Page_Planned Roster/td_’))

WebUI.click(findTestObject(‘Object Repository/Page_Planned Roster/td_’))

WebUI.click(findTestObject(‘Object Repository/Page_Planned Roster/td_4230’))

In this case “td_MAG” = 8hours 30 minutes
“td_” = 0 hours
"td_(Followed by a number) is the total

If the total positions hours matches with total hours i want the test to pass
and if it does not match i want the test to fail.

If I get what you are trying to do you can try the below to see if it works. My attempt is based upon:

Maybe like:
WebUI.click(findTestObject('Object Repository/Page_Dashboard/a_Rostering'))
WebUI.waitForPageLoad(10)

def date = new Date()
def todaysDate = date

WebUI.click(findTestObject('Object Repository/Page_Dashboard/a_Planned Roster'))
WebUI.waitForElementVisible(findTestObject('Object Repository/Page_Planned Roster/td_MAG'), 10)

WebUI.click(findTestObject('Object Repository/Page_Planned Roster/td_MAG'))
use (TimeCategory ) {
	date += 8.hours + 30.minutes
}

WebUI.click(findTestObject('Object Repository/Page_Planned Roster/td_MAG'))
use (TimeCategory ) {
	date += 8.hours + 30.minutes
}

WebUI.click(findTestObject('Object Repository/Page_Planned Roster/td_MAG'))
use (TimeCategory ) {
	date += 8.hours + 30.minutes
}

WebUI.click(findTestObject('Object Repository/Page_Planned Roster/td_MAG'))
use (TimeCategory ) {
	date += 8.hours + 30.minutes
}

WebUI.click(findTestObject('Object Repository/Page_Planned Roster/td_MAG'))
use (TimeCategory ) {
	date += 8.hours + 30.minutes
}

WebUI.click(findTestObject('Object Repository/Page_Planned Roster/td_'))

WebUI.click(findTestObject('Object Repository/Page_Planned Roster/td_'))

WebUI.click(findTestObject('Object Repository/Page_Planned Roster/td_4230'))

// compare days hours minutes or just put everything to minutes ??
use (TimeCategory) {
 def duration = date - todaysDate
 int totalTime = (duration.days * 24) + duration.hours
 println "Days: ${duration.days}, Hours: ${duration.hours}, Minutes: ${duration.minutes}"
 println "Total hours ${totalTime.toString()} ${duration.minutes.toString()}"
}
assert ("42 30" == "${totalTime.toString()} ${duration.minutes.toString()}")
WebUI.verifyMatch("42 30", "${totalTime.toString()} ${duration.minutes.toString()}", false, FailureHandling.STOP_ON_FAILURE)

1 Like

Would it be possible to send a screenshot of the HTML for the td elements?

There could be an HTML attribute we could use for the calculation or maybe the inner text.

Something like this perhaps:

image


would this help ?

Almost, can you also include the line for the Total.

I also have a few other questions.

1) Would td_MAG always be equal to 8hours 30 minutes?
and
2) Do you have to click on each td to get the value?

1 Like

td_MAG would always be 8hours and 30 mins becasue it is an position but htere are other positions as well.

and yes in the test i click on each position td to get the value

I do have a concern that your test case is clicking the same td_MAG and td_ every time as they are all the same testObjects. Also that if the total is not 42:30, your test case won’t be able to find the td_4230 testObject. But that is not the problem you are trying to solve at the moment.

Let’s see if this works.

The following is written under the assumption that:

  1. The rows ‘MAG’ = 8.5
  2. There is only 1 Total on the page
  3. Not all td rows needs to be clicked on to get the text the td contains

Add the following below your td_4230 and run and send through any errors

//Sets expected total
String expectedTotal = calculateTotal()

//Sets actual total
String actualTotal = WebUI.executeJavaScript('return document.querySelectorAll(\'[data-field*="Week"]\').innerText', [])

//Compares actual total to expected total
WebUI.verifyMatch(actualTotal, expectedTotal, false)

//Method to calculate the expected total and return its value
def calculateTotal() {
	Integer tdCount = WebUI.executeJavaScript('return document.querySelectorAll(\'[role="gridcell"]\').length', [])
	Double total = 0.0
	
	for(def i = 0; i < tdCount; i++) {
		if(WebUI.executeJavaScript('return document.querySelectorAll(\'[role="gridcell"]\')[' + i + '].innerText', []) == 'MAG') {
			total = total + 8.5
		}
	}
	
	String strTotal = total.toString()
	
	if(strTotal.substring(strTotal.indexOf('.')) == '.50') {
		strTotal = strTotal.replace('.50', ':30')
	}else {
		strTotal = strTotal.replace('.00', ':00')
	}
	
	return strTotal
}