0

I'm automating an application with the Robot Framework (Python 2.7). I use the xpath to locate the objects. I have seen that the xpath of the objects is different when I change the language application. For example:

ES

// * [@ id = "dnn_ctr489_ViewINT_Cupones_ibtnAdd"]

FR

// * [@ id = "dnn_ctr644_ViewINT_Cupones_ibtnAdd"]

EN

// * [@ id = "dnn_ctr583_ViewINT_Cupones_ibtnAdd"]

This forces me to redo the Test Case because the xpath is different.

Is it possible to use a regular expression to form the xpath? How can I avoid having to redo the test cases for each different language?

Thank you. Marta

2 Answers 2

3

XPath 1

xpath 1.x doesn't support wildcards or regular expressions. However, xpath supports several string functions which can be combined to match what you want. For example, there's a starts-with function. There's no ends-with, but there is a contains, so you could use something like this:

//*[starts-with(@id, 'dnn_ctr') and contains(@id, 'ibtnAdd')]

Of course, that will match somethign like "dnn_ctr blah ibtnAdd blah". If you need precisely "starts with cnn_ctr" and "ends with ibtnAdd" you can do that with a little more work:

//*[starts-with(@id, 'dnn_ctr') and substring(@id, string-length(@id) - string-length('ibtnAdd') + 1) = 'ibtnAdd' ]

XPath 2

With xpath version 2 (which not all browsers support at the time I write this) you can use ends-with, making for a much more readable expression:

//*[starts-with(@id, 'dnn_ctr') and ends-with(@id, 'ibtnAdd')]

You can also match against a regular expression with matches:

//*[matches(@id, 'dnn_ctr.*ibtnAdd')]
Sign up to request clarification or add additional context in comments.

1 Comment

Bryan Oakley, Thanks a lot. It has worked for me with the first option: "//*[starts-with(@id, 'dnn_ctr') and contains(@id, 'ibtnAdd')]"
0

You can use a variable file as input and put all your xpath values in it. I am using JSON in my example.Robot framework JSONlibrary can be used to parse JSON file (https://github.com/nottyo/robotframework-jsonlibrary)

input.json

{
    "Languages": {
         "ES": {
            "xpath1": "// * [@ id = "dnn_ctr489_ViewINT_Cupones_ibtnAdd"]",
             },
         "FR": {
            "xpath1": "// * [@ id = "dnn_ctr644_ViewINT_Cupones_ibtnAdd"]",
             },
         "EN": {
            "xpath1": "// * [@ id = "dnn_ctr583_ViewINT_Cupones_ibtnAdd"]",
             }

you can pass values to test cases based on your language selection in application by using a variable something like following

test.py

language_selected = 'EN'

3 Comments

Thanks for your answer. I have installed the JSON library but I do not know how to put your example into practice. Can you help me? How do I use the .json file?
What would be the motivation to create an object store in JSON when Robot Framework natively supports variable files in Python or YAML format
@A.Kootstra That is a good point, It was just to show an example but Python dictionary can also be used instead of JSON format for input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.