This should work (getAttribute)..why doesn't it?

These are the little things that drive me completely bonkers, because they should be working correctly, but are not.

I have a page that has this object setup:

image

so I’m using this code to extract the value of the “style” attribute:

TestObject sidenav_section = new TestObject ("Side Navigation Window")
sidenav_section.addProperty('xpath', ConditionType.EQUALS, "//*[@id='bi_mySidenav']")
String styleValue = WebUI.getAttribute(sidenav_section, 'style')

seems simple, right? Yet in my output log:

image

This makes no sense unless I’m missing something really small, because the console shows I should be getting a value back:

image

What the heck am I missing here?

1 Like

As with many things in testing, timing is critical. In this case, are you sure your object is “visible and stable” on the page when you issue your statement. Perhaps, you can try to ensure it is and just see:

TestObject sidenav_section = new TestObject ("Side Navigation Window")
sidenav_section.addProperty('xpath', ConditionType.EQUALS, "//*[@id='bi_mySidenav']")
WebUI.waitForElementVisible(sidenav_section, 10)
WebUI.verifyElementVisible(sidenav_section)

String styleValue = WebUI.getAttribute(sidenav_section, 'style')

Another item to note is if you have an <iframe> in the pathway to this object.

Aye, I threw in a 10 second wait on page open, just for kicks with no luck.

The iframe intrigues me though, there are other iframes on this page as well that completely block any interactivity with the elements within; however looking at the full xpath to this element I’m not seeing an iframe callout:

/html/body[@class='theme--dark-plus']/main[@id='page-wrapper']/div[@id='page-contents']/div[@class='container-fluid']/div[@id='bi_mySidenavClosed']/a[@class='bi_openbtn']

Would an iframe allow the console to be able to retrieve the element attribute value but not Katalon?

I guess that the problem style attribute is dynamically updated by javascript (jQuery?) — I am not sure. Only you, @kevin.jackey, can tell.

If this is the case, the presence of the ‘style’ attribute highly depends on the timing. Also the test case may find the value of the ‘style’ attribute varies accidentally.

Perhaps you should somehow wait for the attribute to become present before calling WebUI.getAttribute(). You can use WebUI.waitForElementAttributeValue. For example, you can write:

boolean b = WebUI.waitForElementAttributeValue(sidenav_section, 'style', 'width: 0px;', 10)

Unfortunately WebUI.waitForElementAttributeValue(TestObject, attributeName, attributeValue, timeout) keyword requires you to specify, as the 3rd parameter, a concrete value which you expect. But what if a different value, like 'width: 6px;'? The built-in keyword can work only in a particular case, so that it can not be applicable to the moving attributes.

I suppose that you need a keyword that can wait for the presence of an attribute while matching the value with a regex pattern. I am afraid, you need to invent a custom implementation yourself.

2 Likes

Thanks for your response on this, as usual I’ve gotten buried in other items, will circle back on this later this week and let you know what I find, appreciate everyone’s help so far!!

Yes, an <iframe> can definitely mess with Katalon Studio’s ability to interact with elements, even if the browser console says they’re available. That’s because elements inside an <iframe> are pretty much stuck in their own little world, separate from the rest of the page. To get to those elements, you need to switch to the right <iframe> first.

So, it’s a good idea to use the WebUI.switchToFrame() method before you try doing anything with an element inside an <iframe>. This way, Katalon Studio knows to look in the right spot to interact with the element properly.