How to Deal with Variable Query Strings in URLs


Often web applications are designed such that they use a variable or random query string in the URL address. The order status page at is a good example of this. Every time you go to their order status page, a random ID is added to the URL as shown in this screenshot:


Newegg random IE in URL


When recording this type of web application using Test Studio's default recording settings, it will start recording multiple page nodes in the Elements Explorer each time you record a test that interacts with this type of web page. You will also see what will appear to be duplicate elements, one for each duplicate page node.


In this screen shot I recorded entering and leaving the Newegg order status page three times. Notice how there are three different logout elements under three different page nodes. All three are actually the same logout link on the same webpage:


Duplicate elements


You can easily recover from what appears to be a mess. Before I explain how to clean up this mess, I'd like to reassure you that what looks like a mess in Elements Explorer is not really a problem during test execution. The framework actually doesn't use the page nodes during test execution. It only uses the Find Expressions for the elements that the test steps interact with. So even though you have multiple page nodes, each with their own element, executing the recorded test will work correctly since it is actually ignoring the variable URL.


To clean things up requires just two steps:

  1. Modify the CompareMode property for all page nodes involved (click on a page node in Elements Explorer, then open the Properties window). Change it from FullPathAndQuery to FullPath.

    Page Node - CompareMode
  2. After modifying all the page nodes involved, click the Refresh button located at the top of the Elements Explorer pane.

    Elements Explorer Refresh Button


Now your duplicate pages will be merged into one. Any duplicate elements will be removed leaving behind only the unique elements displayed under the now truly unique page nodes. Here's a screenshot after performing this process on my test recording:


Elements Explorer Clean


By now you're probably asking yourself, "how can I avoid this duplication mess in the first place?" Change a setting in the Recording Options panel of the Project Settings will avoid this problem from the beginning.

  1. Open your project in Test Studio.
  2. Click the Project Settings button.

    Developer Edition User Settings Button
    Visual Studio plugin

    QA Edition User Settings Button
      Standalone Version.

  3. Click Recording Options on the left to open the Recording Options panel.
  4. Change the Elements Page Compare Mode from FullPathAndQuery to FullPath.

    Recording Options - Elements Page Compare Mode


After making this change, recording your test scripts will automatically merge duplicate pages and elements into just one page node with the unique elements showing under it.

Connecting to HTML Popup Windows

The other place you may have to deal with variable query strings is in the URL of pop-up windows. Unfortunately these have to be cleaned up manually after each recording. Test Studio currently does not have a feature to automatically strip or ignore the query strings when recording and/or executing a "Connect to pop-up window" test step. If you try to execute a test that connects to a pop-up window with a variable query string in the URL, the test will probably fail because you are almost guaranteed to get a different query string at run time then you had when you recorded your test script. Here's a screenshot of an example:


Pop-up URL


The good news is that it's quite simple to strip the query string after recording your test script, allowing your test to work the way you intended. Simply delete all characters at the end of the URL, starting with the "?" character (which defines the start of a query string). For example:


Pop-up URL With Query String Removed