Changing a TestObject's HTML Attribute


#1

Hi,
I’m trying to figure out how I can change the “href” attribute of an anchor . Below is some code that I’m trying. When I click the object, it does not go to the new page, and the href attribute is not changed. I’m using ModifyObjectProperty, but I don’t think there is a ModifyObjectAttribute command,

href_example

Thanks, in advance, for any help on this. In the meantime, I’m just using a NavigatToUrl command, with the new href, which is working, but I want to know what I’m doing wrong, so I don’t keep repeating the issue.


#2

In modify property you have “false” at the end. Change that to “true”.

When modifying a property false or true at the end is like checking the box. Adding the property and not checking the box will not actually add the property when using the object or looking for it.


#3

This still isn’t working. When I replace the “false” with “true”, ThebtnNewHTML object is not being passed corectly, and the script errors out with the following error

com.kms.katalon.core.exception.StepFailedException: Unable to get attribute ‘href’ of object ‘’


#4

So what is throwing me off is you are creating the object:
TestObject btnLearnHTML = new TestObject()
btnLearnHTML.addProperty(“xpath”, ConditionType.EQUALS, “//a[contains(.,‘LEARN HTML’)]”)

But you are not giving it an href attribute. So your property would not exist. If I try to replicate your code here and use the comment you’ll notice it’s just printing your url basically.

If you want it to have that property you should be adding the property, not modifying it as it doesn’t exist for the new object you just created. I’m not really sure what you are trying to do here. There is no object on that page that has a url “https://www.codecademy.com/catalog/subject/all” that also has the xpath of //a[contains(.,‘LEARN HTML’)]

I mean for example you could do this:

WebUI.openBrowser('')
WebUI.navigateToUrl('https://www.w3schools.com/')
TestObject btnLearnHTML = new TestObject()
btnLearnHTML.addProperty("xpath", ConditionType.EQUALS, "//a[contains(.,'LEARN HTML')]")
String hrefOldAttribute = WebUI.getAttribute(btnLearnHTML, 'href')

String newUrl = "https://www.codecademy.com/catalog/subject/all"
TestObject btnNewHTML = new TestObject()
btnNewHTML.addProperty("xpath", ConditionType.EQUALS, "//a[contains(.,'LEARN CSS')]").addProperty("href", ConditionType.EQUALS, "/css/default.asp")

String hrefNewAttribute = WebUI.getAttribute(btnNewHTML, 'href')
WebUI.click(btnNewHTML)

WebUI.comment(hrefOldAttribute)
WebUI.comment(hrefNewAttribute)

or if you where gonna modify you would need it in the original like this:

WebUI.openBrowser('')
WebUI.navigateToUrl('https://www.w3schools.com/')
TestObject btnLearnHTML = new TestObject()
btnLearnHTML.addProperty("xpath", ConditionType.EQUALS, "//a[contains(.,'LEARN HTML')]")
String hrefOldAttribute = WebUI.getAttribute(btnLearnHTML, 'href')

String newXPath = "//a[contains(.,'LEARN CSS')]"
TestObject btnNewHTML = new TestObject()
btnNewHTML = WebUI.modifyObjectProperty(btnLearnHTML, 'xpath', 'equals', newXPath, true)
String hrefNewAttribute = WebUI.getAttribute(btnNewHTML, 'href')
WebUI.click(btnNewHTML)

WebUI.comment(hrefOldAttribute)
WebUI.comment(hrefNewAttribute)

In the second scenario here I am modifying the only property I am giving to the object which is xpath.


#5

I think I’ll stick with my workaround. The reason why I’m requesting this, is because I’m working on the signup page of a webapp and when I click the button to sign in, I need to go to a test signup page, instead of the actual signup page.If I go to the signup page, I’ll create a bunch accounts. I used the example in the message because it is the same idea. I didn’t want to give out the real site.

My workaround is to navigate to the URL instead of clicking the button. I don’t think I could change the actual “href” of the object on the page


#6

Understand…

  1. When you modify a TestObject, you’re modifying the TestObject.
  2. When you modify the HTML element, you’re modifying the HTML element (which is frowned upon).

They are not the same things. TestObjects merely represent HTML elements found on a page, ideally or typically. You can create a TestObject for elements that don’t even exist on the page. (Why? I don’t know, but you can, is the point.)


#7

You can use “Verify element clickable” on your sign in button then use navigate to url to the test signup page. It’s not a good practice to modify HTML element during a test anyway.

An alternative would be to mock the sign up server side and execute as much sign up as you want.