Looking for new xpath during the test

Hello dear forum members.

Please I have a following problem, it might be simple for advanced users :}

I have a object which is a edit field with validation, called by xpath: //span[@class=‘au-target’] - this is for filled field

for empty field a validation comes up and the xpath is changing like this: //span[@class=‘au-target has-error’]

And Im trying to verify that object by its new xpath which is //span[@class=‘au-target has-error’]

I do not know how to look for new xpath as the “has-error” is added to class container during the test.

Firstly Im verifying filled field and secondly empty field. And the test fails on the second run, as it can not find the object by its new xpath.

To make it clear, it is only one element, but its xpath is changing:

//span[@class='au-target'] for filled field

//span[@class='au-target has-error'] for empty field

Thank you for any kind of response.

Your post above contains strange strings: //span[@target'], //span[@target has-error'].
I suppose those are not what you intended to present to us.

This Forum has a problem in processing a text containing @. You need to make a bit of efforts in typing a xpath string with @ in this Forum. But how? — I will tell it to you.

When you open editting mode in this Forum, you can find a button </> as this.
If you click this “HTML-mode button”, the textarea changes from WYSIWG mode to HTML mode. In the HTML mode, please type xpath string enclosing with and . as follows:

...I have a object which is a edit field with validation, called by xpath: <code>//span[@target='foo'] </code> - this is for filled field...

By enclosing a xpath with @name with and , you can present the xpath in this forum as you see in the raw code.

Let me confirm your requirement. In the target HTML,

(1) Do you have a single element of which class attribute can either be ‘au-target’ or ‘au-target has-error’ ?

(2) Or do you possibly have 2 elements: one is , another is ?

I can only make assumptions what you are trying to achieve but maybe this’ll help

You can try with string interpolation following (dont know if it’ll work didnt test it):

Create your test object in repository //span[@class=“au-target”] as following:

  1. Under Attributes (Selection Method)
  • Add entry with the name “xpath”
  • match condition: equals
  • value should look like this: //span[@class=“au-target ${variablePart}”]
  • also make sure you use " instead of ’ because interpolation works only within “…”
  1. Before filling the text field make sure you preset the the variable with an empty string
  • In script mode within your test case: def variablePart = “”
  1. In your test case right before you check the empty field set the variable to “has-error”
    def variablePart = “has-error”

If its not working this could also help you
https://docs.katalon.com/katalon-studio/tutorials/handling_static_dynamic_test_objects.html#create-a-method-which-returns-a-dynamic-test-object

Omg thats something, now you imagine you have 24 objects and not only one. You will end up with a long code. So probably easier would be create two separate object and after event look for the one with the different xpath. In this case I would end up with a long objects repository, so I have to choose then. Thank you