Handling Query Strings During Test Recording and Playback

The query string is a part of the URL that begins with a question mark
(?) and contains the list of parameters in the name=value form. Query strings are one of the techniques used to pass information from the client computer to the server.

Query string parameters can be static (for instance, id of the requested web page) or dynamic (for instance, session id). Dynamic parameters can cause problems
when playing back a test, because they change from time to time, thus invalidating the recorded scripts. TestComplete users often ask how to workaround changes in query string parameter values,
so, we composed this blog article to show you how to do it.


In most cases, the first time you access the tested web page (that is, when you open a browser and navigate to the page), the query string is not required - it will be generated by the server after you open subsequent pages. So, if you start recording in the very beginning of the test, the recorded script should work just fine. However, if you start test recording when the page is already opened and its URL contains a query string, then the query string will be presented in the recorded script. For instance:

Page = Sys.Process("IEXPLORE").Page("http://www.example.com/index.asp?act=welcome&sid=7c9ce2e18fc0c756f0ee93")

If parameters in the query string are dynamic, the recorded script will fail to play back, because the parameter value will be different and thus TestComplete will be unable to access the page. To make your scripts independent of dynamic changes in web page URLs, TestComplete provides two features:

  • Wildcards in web page URLs. When addressing web pages using the process.Page, process.WaitPage, windows.Page or window.WaitPage methods, you can use the * and ? wildcards to indicate
    the variable parts of the URL, such as dynamic query string parameters, and thus make your script more stable.

    For instance, you can use the * wildcard to match any possible value of a specific query string parameter:

    Page = Sys.Process("IEXPLORE").Page("http://www.example.com/index.asp?act=*&sid=*")

    Or, you can replace the entire query string with the * wildcard to ignore differences in the parameter set and the parameter values:

    Page = Sys.Process("IEXPLORE").Page("http://www.example.com/index.asp*")

    These techniques require that you go over the recorded script and make the appropriate changes in the Page and WaitPage method calls.

  • The Ignore dynamic URL parameters option. The latest TestComplete update - 6.11 - introduces a new project option, Ignore dynamic URL parameters. It tells TestComplete to ignore values of certain query string parameters during test recording and replace them with wildcards in the recorded script (so that you don’s have to do this manually). The list of parameters to ignore is customizable, you can specify parameters that are used by your web app. For instance, in
    the above URL, you can add the act and sid parameters to the ignore list:

    When this option is active, TestComplete will automatically record the * wildcard for the act and sid parameter values:

    Page = Sys.Process("IEXPLORE").Page("http://www.example.com/index.asp?act=*&sid=*")


You now know how to ignore query string parameters, but, what if you need to store a specific query string parameter, such as session id, in the beginning of the test and use it later? For instance, you can
have several test items, the first one logging into the site without the session id in the URL and the others working with this site after the login. The latter test items need to access the site under the same session id that is generated during the
first test item execution. So, you need to retrieve the session id value when it appears in the URL for the first time and store it in a script or project variable. This way, you will be able to use the session id value
later in your tests.

Below is a sample script that illustrates this approach. It contains the GetQueryStringParam function that returns the value
in a query string parameter specified by the 0-based index. The Test1 function logs into the web site and stores the generated session id into a global variable, and the Test2 function accesses the web site using the stored session id value:



Close

By submitting this form, you agree to our
Terms of Use and Privacy Policy

Thanks for Subscribing

Keep an eye on your inbox for more great content.

Continue Reading

Add a little SmartBear to your life

Stay on top of your Software game with the latest developer tips, best practices and news, delivered straight to your inbox