Refresh/renew TestObject

Hello, is it possible to renew TestObject data (like attributes and/or text if it’s changed after some UI action)? Now I have to create new TestObject to refresh it, but some internal method would be definitely more elegant.

Thanks.

Hi Marek

I’m not sure what you mean… findTestObject(id) should return the testobject as it is stored currently. Unless you mean a WebElement?

you can use “Modify Object Property” to do that, this keyword changes the attributes in the object repository when you run the script, but I am not sure if it works.

Russ Thomas said:

Hi Marek

I’m not sure what you mean… findTestObject(id) should return the testobject as it is stored currently. Unless you mean a WebElement?

Hello, yes of course, WebElement’s attributes stored in TestObject. I will give you an example. You have button identified by some specific XPath (//button[@id=“123”]). This button has few HTML attributes and after UI click, few of them are changed. XPath is still the same. Now I want to verify if parameters are changed. I have to create a new TestObject (with the same selector) and then I get actual attributes. Simple snippet:

TestObject to = new TestObject("MyButton")
.addProperty('xpath', ConditionType.EQUALS, '//button[@id="123"]')

WebUI.click(to)

/* 
* and now I have to recreate object to get new attributes
* And following line can be replaced by something like 'to.refresh()'
*/

to = new TestObject("MyButton").addProperty('xpath', 
         ConditionType.EQUALS, '//button[@id="123"]')

WebUI.getAttribute(to, "myAttribute")

@Luis:
Thanks for response, but this is not my issue. See comment above.

Hi Marek

I think your problem maybe be related to the fact that your original TestObject is made “dynamically”, in code. I’m not certain, but I think if it were stored in the Object Repository and then returned by findTestObject, you might not have these issues. Again, I’m not certain – perhaps one of the support guys can help us understand better.

That said, I wanted to see if I could mimic your TestCase but use a regular TestObject. So I made one using a call to JavaScript (jQuery) to mimic your scenario where an attribute changes after a click.

In one of my KeyWords files I created a static method which I can call from my TestCase:

  /**
   * Click an element using jQuery.
   * @param to (TestObject) The element to be clicked.
   */
  static void jQ_click(TestObject to) {
    String id = to.getObjectId()
    String js = "var elem = \$(arguments[0]).attr('data-russ', 'russ').click();"
    WebElement element = WebUiCommonHelper.findWebElement(to, 30)
    WebUI.executeJavaScript(js, Arrays.asList(element))
    comment(id + ' clicked!')
  }

Notice that the attribute “data-russ” did NOT exist on this element before this code is executed.

Then, in my TestCase, I did this:

jQ_click(findTestObject('sampleNew/txtSAMPLE_ID'))
WebUI.delay(2)
WebUI.verifyElementAttributeValue(findTestObject('sampleNew/txtSAMPLE_ID'), "data-russ", "russ", 60)
WebUI.comment('Success: [data-russ] is russ')

And the output in my Log Viewer…

So the jQ_click method finds the element on the page using jQuery, adds the attribute “data-russ”, gives it the value “russ” and issues a comment. The TestCase verifies the attribute and value are applied using the same TestObject returned from findTestObject.

If your dynamic TestObject is going “stale” when all it really needs to do is supply the xpath to find the web element on the page, I’d say that’s a bug in KS. Either that or I don’t understand TestObjects (likely!)

I hope this was some help, Marek.

Russ

lv.jpg

Hi Russ,

thanks for your input. I think that this is the difference, that findTestObject returns “fresh” test object with latest attributes, but TestObject instance created in the code does not have such capability. I prefer creating test object directly in the code mainly when I have to pass some variable into xpath (e.g. text, index etc.). It is easier and nicer solution. Thus I am looking for possibility how to renew this kind of TestObject instead of recreating its instance.

WebUI.getText(findTestObject(’-----Object xpath string-----’) - gives the current value of the object.

The above is with version 7.9 Katalon.