Verify text on P tag doesn't seem to work

Hello Katalon,

The web recorder gets an XPath, but when I play it back it doesn’t work.

=============== ROOT CAUSE =====================
Caused by: com.kms.katalon.core.exception.StepFailedException: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘Object Repository/Page_/p_Im the 2nd page’ located by ‘(.//*[normalize-space(text()) and normalize-space(.)=concat(‘/html[1]/body[1]/p[@class=’, ‘"’, ‘mytext’, ‘"’, ‘]’)])[1]/preceding::p[1]’ not found

Here’s my simple web page.

<html>

<head>
</head>

<body>
<p class="mytext">I'm the 2nd page!</p>
</body>

</html>

The recorder got the following XPath.

(.//*[normalize-space(text()) and normalize-space(.)=concat('/html[1]/body[1]/p[@class=', '"', 'mytext', '"', ']')])[1]/preceding::p[1]

Why would this XPath fail?

To fix, I switched it to use CSS Selector and it works just fine, in fact the test runs a lot faster!

p.mytext

Cheers,

1 Like

This portion is just nonsense.

Recorder tool seems to have a bug here.

Could be that the apostrophe confuses recorder and will do a wrong split, but looks like a valid bug for my eyes.

That XPath is atrocious.

IMV, the recorder/spy tool should be retired. There are so many tools available, free in browsers, that enable users to remove a weak link in the tool chain and build robust selectors (XPath or CSS) that there’s simply no need for it.

@edward.davies you can clearly author CSS, so why use the recorder? You would do far better creating your Test Objects dynamically – search for makeTO.

makeTO("p.mytext")

No costly maintenance headaches with a jam-packed Object Repository, no setup cost for a tool that fails on even the simplest of HTML layouts (as you’ve seen), no need to spend time reporting issues on a forum to entertain us guys :stuck_out_tongue_winking_eye:

Thanks guys, glad it’s generated some conversation! =)

Why report it? My main idea is to introduce this tool to guys who cannot code at all on the test team.

I switched it to default to CSS selector targets only and it works perfectly. Guys can come in and fiddle with the CSS selector and it’s a lot more readable than XPath.

Just wanted to raise awareness, because ultimately I would offer this tool to someone who doesn’t know how to code in Groovy and with maybe 1 hour’s training in CSS selectors, they can use this tool for their automation. Fix their own stuff on CI. I want them to be able to work on their own, without me helping them. =)

You wouldn’t get any argument fro me. CSS (along with JS and HTML) is how the web is built. Learning how to read/write CSS selectors is a much more transferable skill in a Web Dev world.

:dart:

1 Like

@edward.davies

I tried to examine what you reported.

I examined 3 cases of Selection Method of a Test Object.

When Selection Method: XPath is chosen

(.//*[normalize-space(text()) and normalize-space(.)=concat('/html[1]/body[1]/p[@class=', '"', 'mytext', '"', ']')])[1]/preceding::p[1]

When Selection Method: Attribute is chosen

//*[(text() = concat("I" , "'" , "m the 2nd page!") or . = concat("I" , "'" , "m the 2nd page!"))]

When Selection Method: CSS is chosen

p.mytext

The “Selection Method: XPath” gave me just a nonsense. Clearly Katalon Studio is buggy for this.

c/c
@Jass
@duyluong

The “Selection Method: Attribute” gave me a locator which makes sense as an XPath.

//*[(text() = concat("I" , "'" , "m the 2nd page!") or . = concat("I" , "'" , "m the 2nd page!"))]

This xpath locator refers to the element’s content text. But this strategy is not the best one to take in this case. I would prefer an alternative as follows.

//p[@class='mytext']

A man can think and choose a strategy out of some choices, but Recorder works only as statically configured.

The “Selection Method: CSS” gave me the best reasonable locator “p.mytext”. I can guess why. It is because CSS Selector specification does NOT not support locating an HTML element by its content text like “I'm the 2nd page!”.

I think it is not fair to say CSS is better than XPath. It depends on the cases. In some cases an HTML element may require referring to the content text to locate it.

In many cases Recorder & Spy tool can suggest good enough CSS Selectors for those who do not know how to code.

I guess, still you would be called for help sometimes, @edward.davies.

Nobody said it was better (at least, in this thread).

@kazurayam, thanks for highlighting the usefulness of attribute selection. It’s good to have more than 1 strategy, in case for example XPath or CSS don’t work. We can optimise the CSS even further, so instead of “p.mytext” you can use “.mytext” and it still works a charm. Yes, for me it’s quite easy to use Katalon, that’s the main appeal I think. I may still get calls on my time to help, but it should be fairly easy to sort out.

Thanks @Russ_Thomas, learning CSS selectors, at least basic ones will definitely help. Attribute and CSS selections both work for what I had in mind.

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.