Refresher - how to pull text value from individual TestObject elements?

Hopefully this is an easy one, jumping back and forth between toolsets and languages and forgetting things, I have a webelement in my page that has four rows:

  1. Item Name
  2. Item text property 1
  3. Item text property 2
  4. Item text property 3

So what I’m trying to do is pull this entire element into a testobject and then loop through rows 2-4 above validating expected results…

Everything seems to be working correctly except retrieving the individual row text, I’m 99% sure it’s just me forgetting what the correct command is to retrieve the text, as my count size, etc is working correctly, thoughts?

//Define Text Block TestObject
TestObject TextBlock = new TestObject ("Http Responses Text Block " + counter)
String TextBlockXpath = "//*[@id='response_native-data_" + counter + "_" + object_id + "']//*[contains(@class,'')]"
KeywordUtil.logInfo ("HTTPResponsesTextBlockXpath: " + TextBlockXpath)
TextBlock.addProperty('xpath',ConditionType.EQUALS, TextBlockXpath)
				
elements_found = WebUI.findWebElements(TextBlock, 10)
element_total = elements_found.size()
KeywordUtil.logInfo ("TextBlock Elements Found: " + element_total)
				
//If the Elements Count equals four, validate each element; if not then throw an error
if (element_total == 4) {
					
	//Number of Requests row
	String request_number_row = WebUI.getAttribute(elements_found[1], "value")
	KeywordUtil.logInfo ("Request Number Row value: " + request_number_row)
					
	//Avg. Requests Per Minute row
	String request_per_minute_row = WebUI.getAttribute(elements_found[2], "value")
	KeywordUtil.logInfo ("Avg. Requests Per Minute value: " + request_per_minute_row)
					
	//Avg. Load Time row
	String request_load_time_row = WebUI.getAttribute(elements_found[3], "value")
	KeywordUtil.logInfo ("Avg. Requests Per Minute value: " + request_load_time_row)
					
					
	} else {
		KeywordUtil.markFailed ('Native App HTTP Responses Widget row: ' + counter + ' ,element count of ' + element_total + 'does not match the expected count of 4')
		object_found_tracker = false
}
1 Like

The issue is when you start, you are creating a TestObject(s), but when you are using getAttribute, you have WebElement(s) after the below statement.

elements_found = WebUI.findWebElements(TextBlock, 10)

So, “WebUI” works on TestObjects, but since we now have a WebElement(s), you should use:

//Number of Requests row
String request_number_row = elements_found[1].getAttribute("value")
KeywordUtil.logInfo ("Request Number Row value: " + request_number_row)
					
//Avg. Requests Per Minute row
String request_per_minute_row = elements_found[2].getAttribute("value")
KeywordUtil.logInfo ("Avg. Requests Per Minute value: " + request_per_minute_row)
					
//Avg. Load Time row
String request_load_time_row = elements_found[3].getAttribute("value")
KeywordUtil.logInfo ("Avg. Requests Per Minute value: " + request_load_time_row)				
1 Like

Hmmm…okay, not getting any errors now but everything is returning nulls, when it should be the text objects above, thoughts?

image

The updated code:

//If the Elements Count equals four, validate each element; if not then throw an error
if (element_total == 4) {
					
//Number of Requests row
def request_number_row = elements_found[1].getAttribute("value")
KeywordUtil.logInfo ("Request Number Row value: " + request_number_row)
					
//Avg. Requests Per Minute row
def request_per_minute_row = elements_found[2].getAttribute("value")
KeywordUtil.logInfo ("Avg. Requests Per Minute value: " + request_per_minute_row)
					
//Avg. Load Time row
def request_load_time_row = elements_found[3].getAttribute("value")
KeywordUtil.logInfo ("Avg. Requests Per Minute value: " + request_load_time_row)
					
}					

and how things appear in the object I’m referencing:

I would take a look at the above line and print it out to ensure it is what you want. You have a mix of single and double quotes and maybe you have “mixed” them up. Alternatively, you can remove the plus signs and instead, put your variables in the below format.

String TextBlockXpath = "//*[@id='response_native-data_${counter}_${object_id}']//*[contains(@class,'')]"
WebUI.comment("my pathway is: ${TextBlockXpath}") 

And, since you are only getting the TEXT of the element(s), and not a tag’s attribute value, perhaps you should use “getText” instead.

Maybe like:
//If the Elements Count equals four, validate each element; if not then throw an error
if (element_total == 4) {
					
//Number of Requests row
def request_number_row = elements_found.get(1).getText()
KeywordUtil.logInfo ("Request Number Row value: " + request_number_row)
					
//Avg. Requests Per Minute row
def request_per_minute_row = elements_found.get(2).getText()
KeywordUtil.logInfo ("Avg. Requests Per Minute value: " + request_per_minute_row)
					
//Avg. Load Time row
def request_load_time_row = elements_found.get(3).getText()
KeywordUtil.logInfo ("Avg. Requests Per Minute value: " + request_load_time_row)
					
}

So cleaned the code up a bit, no difference.

When I look at the Xpath in Xpath Helper I can see all four elements, and the counter shows that it’s retrieving four elements, so why can’t I get the text to show up?

UPDATE: Aha! Figured that part out, I needed to use a different property lookup per Get Attribute as null while there is value? - #4 by Andrej_Podhajsky

This lookup returns the desired text:

def request_per_minute_row = elements_found[2].getAttribute("innerText")
2 Likes