HomeOATS

Learn How to Customize Xpath with Wildcard and Regular Expressions

Like Tweet Pin it Share Share Email

In this article,we are going to understand the need for customizing xpath and learn tips and tricks on how to achieve it in Open Script. This is really very useful concept to run automation scripts independent of environment and data used .To start with,I have mentioned below few frequently ocurring scenarios which requires xpath customization that will help you to understand in a better way.

 

Few Scenarios for Customizing XPath

1. No wonder in whole testing cycle,there are many instances like Dev,QA ,production,etc where the applications needs to be tested. With change in instances, xpath variation comes into picture.

e.g xpath for a particular link is as below.

/web:window[@title=’Admin Portal’]/web:document[@index=’1′]/web:a[@text=’Select View ‘ or @href=’https://ibpm-stage.test.com/fin-qa2/icms/login/v_FWdXdybh9pA0D97KGfUWwCXcQcwv3p*/!TABTHREAD0?pyActivity=%40baseclass.doUIAction&action=display&harnessName=AdmindashBoard&className=Data-Portal&label=Dashboard&contentID=36fe5fd4-ab9e-4474-8ded-f216985024d2&dynamicContainerID=a8c28be1-b99f-400e-a517-38773f1fa435&tabIndex=1&portalThreadName=STANDARD&portalName=AdminPortal&pzHarnessID=HID16B53CB4FBDA2A039EEEB4CF0FC39F41#’]”)

Here,the attribute  ‘href’ contains  url(here it is ,https://ibpm-stage.test.com/fin-qa2),which will vary with every test cycle.There is a need to customize it to avoid failing the script in different environments.

2. Not only this, we have encountered scenarios where different ids are generated for objects  with few characters changing every time for new instances.

e.g

Sample xpath where each id is dynamically generated with a variable component. The below  highlighted one shows the variable part.

/web:window[@index=’0′ ]/web:document[@id=’H7F46A8F1CC45DFA5C77F81BFEA457DFF_HistoryAndAttachments’]/web:a[@text=’Attach a Note’] .

It is not only restricted to ‘id’ attribute ,but it can be any attribute describing the object.

3. What if the page title changes with every purchase order where purchase order is embedded in title attribute.

e.g sample xpath where the title of the page varies with each  purchase order .In this case ,it is ‘612345’.  It has to be customized otherwise the script will fail.

/web:window(@title=’Purchase_Order_612345‘]/web:document[@index=’0′ or @name=’Attachments’]/web:form[@name=’main’ or @index=’0′]/web.button[@text=’Save’]”)

All the above issues can be addressed by  regularizing .

Openscript provides the feature to regularize the xpath using wild characters and regular expression.

Wild Character is a character substituting zero or more characters.

*   The star character symbolizes zero or more occurrences.

?   The question mark symbolizes single occurrence.

e.g    a*b   It matches acdf123%b ,ab,aw344b

a??b  It matches a12b,axtb

a??c  It does not match adrtc

Regular expression extends features of wild character adding the ability to define class,range and number of characters.

[a-z]   It matches any character ranging from a to z

[^a-e] It does not  match any character ranging from a to e

{4}      It matches four occurrences of the preceding character.

{3,7}   It matches occurrences count starting from 3 to 7 of the preceding character.

Ways of Customizing XPath in Open Script 

Now we have learnt about wild characters, regular expression and the necessity to do it,below are the steps to apply it in automation scripts.

It can be done in two ways in openscript. One is through tool itself and the other is through coding.

  1. Configuring in the tool :

Below are the steps to configure in the tool.

a.Navigate to Openscript >View >Preferences >Playback>Web Functional >Object Identification

Openscript Preferences

b.Select ‘Use Xpath with smart Match’. for Object Identification(Default)

c.Select ‘Wildcard then Regular Expression’ for Match Format

Now,The above  xpath concerning ‘href’ can be modified as below.

/web:window[@title=’Admin Portal’]/web:document[@index=’1′]/web:a[@text=’Select View ‘ or @href=’*/icms/login/v_FWdXdybh9pA0D97KGfUWwCXcQcwv3p*/!TABTHREAD0?pyActivity=%40baseclass.doUIAction&action=display&harnessName=AdmindashBoard&className=Data-Portal&label=Dashboard&contentID=36fe5fd4-ab9e-4474-8ded-f216985024d2&dynamicContainerID=a8c28be1-b99f-400e-a517-38773f1fa435&tabIndex=1&portalThreadName=STANDARD&portalName=AdminPortal&pzHarnessID=HID16B53CB4FBDA2A039EEEB4CF0FC39F41#’]”)

It does wild character pattern matching and passes the step.

Xpath concerning ‘purchase order ‘ can be modified as below assuming the purchase order may contains digit from 0 to 9 and letters from a-z where the length of the purchase order is exactly 6.

/web:window(@title=’Purchase_Order_[0-9a-z]{6}‘]/web:document[@index=’0′ or @name=’Attachments’]/web:form[@name=’main’ or @index=’0′]/web.button[@text=’Save’]”)

It does regular expression matching  and passes the step.

 

2. Through Coding :

Add the below code in the beginning of your script.

To change the matching format to ‘Wildcard then Regualar Expression’ refer below code:

getSettings().set(“ft.matchformat”,”wildcardThenRegex”);

To change the matching format to ‘wildcard’ use below:

getSettings().set(“ft.matchformat”,”wildcard”);

To change the matching format to ‘Regular Expression’

getSettings().set(“ft.matchformat”,”Regex”);

 

Openscript provides  below three options to choose for match format.

1. Wildcard

2.Wildcard then Regular Expression

3.Regular Expression

One can configure any of the above as per the requirement.It is suggested to configure ‘Wildcard then Regular Expression’ as it supports both regular expression and wildcard concept.

Few are some of the commonly encountered scenarios where these can be used. Nevertheless,the usage of regular expression is not only limited to above cases,but can be utilized to benefit in many other similar scenarios.

lambdatest

Comments (7)

  • Nice article, Will try to implement it. I have so many scenarios where the need to use the regular expression.

    Only I have concern if I changed the Match format to “Wildcard then Regular Expression” and if in next XPath there will be no regular expression then script will fail or continue as it is?

    Reply
    • Hi Vivek . Thanks for your comment. If matching format is changed to ‘Wildcard then Regular Expression’ and the subsequent XPaths do not have regular expression ,the script will still continue.

      Reply
  • Hi Namrata,

    Very useful post.

    I am using object libraries to store object paths in key-value pair. Can we use this wild card and regular expressions within that object library ?

    Thanks.

    Reply
  • Hi Srinivas, thank you for the topic. I have a question about path. Index value of web:document changes in every environment. And it fails if I don’t update the index value. In QA, it is 13, UAT, it is 16. I replaced with star. ‘1*’. It didn’t work. I Did replace dynamic values with [0-9] and etc. but still no luck.
    Please advice.

    Reply
    • Hi Anar,

      As discussed with you, we may have to handle it with a programming logic to iterate with numbers and see if the document exists and then fix on to that document number.
      thats the only way i know of 🙂 in case you found any other solution, feel free to share it with us. It would help others too

      Reply
  • Dear Srinivas,
    I am new to openScript. Though I am good at writing xpath for Selenium,OpenScript looks different and annoying.

    Is it possible that we can write custom xpath as we do in selenium. I am really not liking the spy and the lengthy object identifier path it is returning. (window..doc..form..ele..)

    can we just do like : //div//tr[contains(@id,’abc’)]/ …. etc., using keywords like parent,siblings,contains,starts-with etc etc… as we do in Selenium xpath.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *