QTP uft tutorial: Cross browser testing: Creating one dynamic script for all browsers

Hello. I've gotten a lot of questions about cross-browser testing. I've received concerns such as "My script runs in IE, but not in firefox", "the object property values differ from browser to browser, how can I adjust for that within my script?". This tutorial will answer those questions. I will show you how to take your scripts and easily convert them to run with any browser.

Today, I will be using the www.qtptutorial.net home page as an example.

1. Open the url www.qtptutorial.net in internet explorer

2. Now open the same url www.qtptutorial.net in firefox

3. Select your object spy tool and spy on the object "I WANT MY 7 DAY TRIAL" in internet explorer. Now spy on the same object in firefox. Use the copy to clipboard icon and copy and paste both IE and firefox objects into a note pad. Delete every other property except for outerhtml. You will see that although this is the same object, the property values differ.

firefox browser:
"outerhtml:=<A href=http://www\.qtptutorial\.net/exclusive-member-pricing/ class=et_pb_promo_button>I Want My 7 Day Trial</A>",

internet explorer browser:
"outerhtml:=<a class=""et_pb_promo_button"" href=""http://www\.qtptutorial\.net/exclusive-member-pricing/"">I Want My 7 Day Trial</a>",

Now in this scenario, let's pretend that outerhtml is the only property we can use to identify this object. In many applications, you may not have an html id, a name, or title. You will have to use other properties such as outerhtml to identify an object.

4. Now let's throw these properties and their values into two lines of code to click both the IE and Firefox object. Adjust the Index for the browser so that the object is being clicked correctly in your scripts.

Browser("Index:=0").Page("title:=.*").WebElement("outerhtml:=<A href=http://www\.qtptutorial\.net/exclusive-member-pricing/ class=et_pb_promo_button>I Want My 7 Day Trial</A>").Click
Browser("Index:=1").Page("title:=.*").WebElement("outerhtml:=<a class=""et_pb_promo_button"" href=""http://www\.qtptutorial\.net/exclusive-member-pricing/"">I Want My 7 Day Trial</a>").Click

With these two lines of code, we can now click the same object in both IE and firefox. However, that causes a lot more work for maintainability in the future, and a lot more lines of code in general. So let's take these two lines of code and condense them into one that can click both of the objects in IE and firefox. The main problem is that the outerhtml properties have differing values.

outerhtml:=<A href=http://www\.qtptutorial\.net/exclusive-member-pricing/ class=et_pb_promo_button>I Want My 7 Day Trial</A>"
outerhtml:=<a class=""et_pb_promo_button"" href=""http://www\.qtptutorial\.net/exclusive-member-pricing/"">I Want My 7 Day Trial</a>"

5. Let's delete these differences in the property values, and replace them with .* This is a regular expression that we use in QTP/UFT to tell QTP/UFT to make our property value dynamic. Our code should not look like the line of code below.

"outerhtml:=.*http://www\.qtptutorial\.net/exclusive-member-pricing/.*I Want My 7 Day Trial.*"

Now we have one line of code that uniquely identifies this link, but is still is dynamic enough to work for both IE and firefox. Let's place the outerhtml property and value back in our code, and we can see that it now works for both browsers.

Browser("Index:=0").Page("title:=.*").WebElement("outerhtml:=.*http://www\.qtptutorial\.net/exclusive-member-pricing/.*I Want My 7 Day Trial.*").Click

Adjust the index value above to correctly click on the object within any of the browsers. There we go. We now made your script flexible for both IE and firefox. Do this throughout your whole script so your script is cross-browser ready. Just make sure to keep your property values unique when using regular expressions.

Post your comments and questions below 🙂

 

Pin It on Pinterest

Clef two-factor authentication