My preference is to use attributes (as opposed to xpath) for test objects in the object repository. However, is there a way to specify the second occurrence/index of a match?
For example, let’s say I want to capture the text of the second link labeled “Click” on a page…
tag equals a
text equals Click
index equals 2
I mention “index” because that’s what it’s called in UFT.
I know that I could specify [2] if I’m using xpath, but I’d rather stick to attributes if possible.
Pretty sure that’s not possible using attrs. Even CSS can’t do that if you need to match the text “Click”. If they’re near each other structurally, then you could use CSS :nth-child() or :nth-of-type().
Failing that, there’s always JavaScript… but I’m guessing you don’t want that if you’re looking for a declarative/static solution stored in OR.
In the following screenshot, I chose “Attributes” out of 3 options of “Selected Method”, and I wrote a XPath expression in the list of “Detect objects by” of an “Attributes”.
Hi Kazurayam,
Not really no. Using XPaths can be a little fragile at times, whether you specify them with the Xpath or Attributes method. I would like to specify something like “click on the second occurrence of a link that is labeled as ‘Home’” for example. It would be nice if I could add three attributes, like this:
tag=a
text=“Home”
index=1 (assumes zero based, so the first occurrence would be 0, the second would be 1)
If you’ve ever used QTP/UFT before, this is an option of “Ordinal Identifier”. See attached screenshot.
Note that this isn’t too big a deal now, I can still use XPaths to do what I want, I was just surprised that Katalon didn’t also have this index option.
OK, you can express this in XPath: (//a[text()="Home"])[2]
In XPath, the index of node-set starts with 1, not 0.
Yes, you are right. Katalon Studio generates several instances of XPath expressions as candidates. Many of them are fragile for HTML design changes.
However XPath is a language that can express a node in a document by many variations of expressions. You would be intelligent enough to think and write more robust XPath expressions manually just as you are capable in QTP/UFT.
Thanks Russ, I think the problem here is that I’m not doing a good job of explaining what I am looking for. I’ve used the XPATH solution, but what I’m after is a way to do this through Katalon’s UI with Attributes. For example, if I choose the Attributes method over the XPath method, I can click the “Add” button and select “tag” and type in the value “a”, and type a property of “id” and type a value, I can type property of “href” and type a corresponding value, etc, but… to my knowledge there’s no way to specify a value of “index” (or some other equivalent keyword) and specify a value of 0, 1, 2 or whatever. The only way I can do this is to manually edit the XPath. Does that make any more sense?
Hi Kazurayam. Please see the response I just type to Russ here, I don’t think I’m doing a good job of explaining exactly what I mean. It appears that the only way to do this is by manually editing the Xpath (appending “[2]” for example), whereas what I would like is to use the Attributes method, select a value of “index” (or some other similar keyword) and typing a property of 0, 1, 2 or whatever. In other words, making it more UI driven than manually editing an Xpath. Does that make any sense?
The best way to describe your problem is to share the HTML source code of your target Web page. Without the HTML source, you can not describe problems in automated testing of Web UI clear enough.
I suppose that you just did not know how to use the pair of ( and ) to construct a FilterExpr in XPath. If you use it appropriately && manually, you should be able solve your problem using XPath completely.
Katalon Studio is ignorant of the FilterExpr either. But you shouldn’t blame them. The FilterExpr is a Lightsaber
Thanks kazurayam, I really appreciate you taking the time over this, but this is not what I’m asking, I’m having trouble communicating my thoughts. What I’m asking for (if it doesn’t already exist) is a slight tweak in the UI for specifying the instance number. Your website example correctly illustrates what I’m trying to test against, but I’m trying to avoid having to add (for example) “[2]” to the end of an xpath manually, and do it through Katalon’s UI attributes “helper” that constructs the xpath.
If I click on the Add button of the Object’s Properties pane, I have a dropdown list of class, css, id, name, title and xpath. It would be nice to have an additional option for the instance number, so if you set it to “2” it would append “[2]” to the xpath. Does that make more sense? See screenshot attached.
I would like to remind you that my previous “Test Case/TC2”, which does not use “Object Repository” at all, already used the concept of “Parameterized Test Object”.
Please find the code
for (int i = 1; i <= numberOfAnchors; i++) {
TestObject anchor = byXPath("(//a[text()='Click'])[${i}]")
...
Thanks again kazurayam. On the one hand, this still isn’t quite what I was looking for, and I really don’t know how else to try and explain myself. I think Russ nailed it from the start by saying that what I am after isn’t possible using attributes. I was hoping to find a way for some of our team members that are new to automation and unfamiliar with xpath to identify objects purely from making selections from the attributes view. It’s not a big deal though, this doesn’t come up very often and I can advise they use XPaths and append the instance number to it.
On the other hand, I had never considered the ability to parameterize test objects in the way you described, and this is awesome, so thanks!! This will come in most handy when I need to traverse web tables and the like.