Applies to TestComplete 14.10, last modified on June 5, 2019

Many web applications, especially those created using ASP, ASP.NET, PHP and other technologies, produce dynamic contents. This means that both the web page contents and address change dynamically based on requests sent from the client computer to the server. Quite often, data is passed to the server via query strings. A query string is a part of the web page URL that follows the question mark, consisting of name-value pairs separated with ampersands:

http://my_url.com/showpage?page_id=sample_page&session_id=4s35f8b15

The values of query string parameters depend on the current context. Some of them (for instance, IDs of requested web pages) can be static while others (such as session IDs) change from time to time. To make your test scripts independent of dynamic changes in web page URLs, TestComplete lets you use wildcardsand regular expressions when addressing web pages.

To access a page displayed in a browser, you use the Page and WaitPage methods of the window object that corresponds to the web page window, or the process object that corresponds to the browser process. The URL parameter of these methods accepts the * and ? wildcards and regular expressions in the "regexp:pattern" format. You can use them to match dynamic parts of the URL both in keyword tests and in scripts.

To match all possible values of a query string parameter, you can use the * wildcard:

w.Page("http://my_url.com/showpage?page_id=*&session_id=*")

You can also replace the entire query string with the * wildcard to ignore differences in the parameter set and the parameter values:

w.Page("http://my_url.com/showpage*")

The sample script below illustrates the usage of wildcards in place of query strings:

JavaScript

function Test()
{

  // Obtain the Google page
  let url = "www.google.com";
  Browsers.Item(btIExplorer).Run(url);
  let browser = Sys.Browser();
  let page = browser.Page("*");

  // Obtain the search field
  let searchField = page.NativeWebObject.Find("name", "q", "input");

  // Enter "TestComplete" in the search field
  searchField.SetText("TestComplete");
  searchField.Keys("[Enter]");

  // Wait until the results are loaded
  page.Wait();
  page = browser.page("https://www.google.com/?gws_rd*");

  // Obtain the result link
  let results = page.FindChildByXPath("//div[@class='rc']//h3//a");

  if (!strictEqual(results, null))
  {
    // Click the link
    results.Click();
  }
  else 
    Log.Error("Cannot obtain the search results");

}

JScript

function Test()
{

  // Obtain the Google page
  var url = "www.google.com";
  Browsers.Item(btIExplorer).Run(url);
  var browser = Sys.Browser();
  var page = browser.Page("*");

  // Obtain the search field
  var searchField = page.NativeWebObject.Find("name", "q", "input");

  // Enter "TestComplete" in the search field
  searchField.SetText("TestComplete");
  searchField.Keys("[Enter]");

  // Wait until the results are loaded
  page.Wait();
  page = browser.page("https://www.google.com/?gws_rd*");

  // Obtain the result link
  var results = page.FindChildByXPath("//div[@class='rc']//h3//a");

  if (results != null)
  {
    // Click the link
    results.Click();
  }
  else 
    Log.Error("Cannot obtain the search results");

}

Python

def Test1():
  # Obtain the Google page
  url = "www.google.com";
  Browsers.Item[btIExplorer].Run(url);
  browser = Sys.Browser();
  page = browser.Page("*");

  # Obtain the search field
  searchField = page.NativeWebObject.Find("name", "q", "input");

  # Enter "TestComplete" in the search field
  searchField.SetText("TestComplete");
  searchField.Keys("[Enter]");

  # Wait until the results are loaded
  page.Wait();
  page = browser.page("https://www.google*");

  # Obtain the result link
  results = page.FindChildByXPath("//div[@class='rc']//h3//a");

  if (results != None):
    # Click the link 
    results.Click();
  else:
    Log.Error("Cannot obtain the search results");

VBScript

Sub Test

  Dim url, browser, page, searchField, results

  ' Obtain the Google page
  url = "www.google.com"
  Browsers.Item(btIExplorer).Run url
  Set browser = Sys.Browser
  Set page = browser.Page("*")

  ' Obtain the search field
  Set searchField = page.NativeWebObject.Find("name", "q", "input")

  ' Enter "TestComplete" in the search field
  searchField.SetText("TestComplete")
  searchField.Keys("[Enter]")

  ' Wait until the results are loaded
  page.Wait
  Set page = browser.page("https://www.google.com/?gws_rd*")

  ' Obtain the result link
  Set results = page.FindChildByXPath("//div[@class='rc']//h3//a")

  If Not IsNull(results) Then
    ' Clicks the link
    results.Click

  Else 
    Log.Error("Cannot obtain the search results")
  End If

End Sub

DelphiScript

procedure Test();

var url, browser, page, searchField, results;

begin 

   // Obtain the Google page
   url := 'www.google.com';
   Browsers.Item(btIExplorer).Run(url);
   browser := Sys.Browser;
   page := browser.Page('*');

   // Obtain the search field
   searchField := page.NativeWebObject.Find('name', 'q', 'input');

   // Enter "TestComplete" in the search field
   searchField.SetText('TestComplete');
   searchField.Keys('[Enter]');

   // Wait until the results are loaded
   page.Wait;
   page := browser.page('https://www.google.com/?gws_rd*');

   // Obtain the result link
   results := page.FindChildByXPath('//div[@class="rc"]//h3//a');

   if aqObject.GetVarType(results) <> varNull then 
     // Clicks the link
     results.Click()
   else
     Log.Error('Cannot obtain the search results');

end;

C++Script, C#Script

function Test()
{

  // Obtain the Google page
  var url = "www.google.com";
  Browsers["Item"](btIExplorer).Run(url);
  var browser = Sys["Browser"]();
  var page = browser["Page"]("*");

  // Obtain the search field
  var searchField = page["NativeWebObject"]["Find"]("name", "q", "input");

  // Enter "TestComplete" in the search field
  searchField["SetText"]("TestComplete");
  searchField["Keys"]("[Enter]");

  // Wait until the results are loaded
  page["Wait"]();
  page = browser["page"]("https://www.google.com/?gws_rd*");

  // Obtain the result link
  var results = page["FindChildByXPath"]("//div[@class='rc']//h3//a");

  if (results != null)
  {
    // Click the link
    results["Click"]();
  }
  else 
    Log["Error"]("Cannot obtain the search results");

}

To specify more complicated parts of the URL, use regular expressions. The following example shows how you can specify different domains of the URL:

JavaScript

function Test()
{

  // Obtain the Google page
  let url = "www.google.com";
  Browsers.Item(btIExplorer).Run(url);
  let browser = Sys.Browser();
  let page = browser.Page("*");

  // Obtain the search field
  let searchField = page.NativeWebObject.Find("name", "q", "input");

  // Enter "TestComplete" in the search field
  searchField.SetText("TestComplete");
  searchField.Keys("[Enter]");

  // Wait until the results are loaded
  page.Wait();

  // Get the page with one of the specified domains
  page = browser.page("regexp:https://www.google.(com)|(de)|(fr)|(es)/\?.*");

  // Obtain the result link
  let results = page.FindChildByXPath("//div[@class='rc']//h3//a");

  if (!strictEqual(results, null))
  {
    // Click the link
    results.Click();
  }
  else 
    Log.Error("Cannot obtain the search results");

}

JScript

function Test()
{

  // Obtain the Google page
  var url = "www.google.com";
  Browsers.Item(btIExplorer).Run(url);
  var browser = Sys.Browser();
  var page = browser.Page("*");

  // Obtain the search field
  var searchField = page.NativeWebObject.Find("name", "q", "input");

  // Enter "TestComplete" in the search field
  searchField.SetText("TestComplete");
  searchField.Keys("[Enter]");

  // Wait until the results are loaded
  page.Wait();

  // Get the page with one of the specified domains
  page = browser.page("regexp:https://www.google.(com)|(de)|(fr)|(es)/\?.*");

  // Obtain the result link
  var results = page.FindChildByXPath("//div[@class='rc']//h3//a");

  if (results != null)
  {
    // Click the link
    results.Click();
  }
  else 
    Log.Error("Cannot obtain the search results");

}

Python

def Test2():
  # Obtain the Google page
  url = "www.google.com";
  Browsers.Item[btIExplorer].Run(url);
  browser = Sys.Browser();
  page = browser.Page("*");

  # Obtain the search field
  searchField = page.NativeWebObject.Find("name", "q", "input");

  # Enter "TestComplete" in the search field
  searchField.SetText("TestComplete");
  searchField.Keys("[Enter]");

  # Wait until the results are loaded
  page.Wait();

  # Get the page with one of the specified domains
  page = browser.page("regexp:https://www.google.(com)|(de)|(fr)|(es)|(ru)/\?.*");

  # Obtain the result link
  results = page.FindChildByXPath("//div[@class='rc']//h3//a");

  if (results != None):
    # Click the link 
    results.Click();
  else:
    Log.Error("Cannot obtain the search results");

VBScript

Sub Test

  Dim url, browser, page, searchField, results

  ' Obtain the Google page
  url = "www.google.com"
  Browsers.Item(btIExplorer).Run url
  Set browser = Sys.Browser
  Set page = browser.Page("*")

  ' Obtain the search field
  Set searchField = page.NativeWebObject.Find("name", "q", "input")

  ' Enter "TestComplete" in the search field
  searchField.SetText("TestComplete")
  searchField.Keys("[Enter]")

  ' Wait until the results are loaded
  page.Wait

  ' Get the page with one of the specified domains
  Set page = browser.page("regexp:https://www.google.(com)|(de)|(fr)|(es)/\?.*")

  ' Obtain the result link
  Set results = page.FindChildByXPath("//div[@class='rc']//h3//a")

  If Not IsNull(results) Then
    ' Click the link
    results.Click

  Else 
    Log.Error("Cannot obtain the search results")
  End If

End Sub

DelphiScript

procedure Test();

var url, browser, page, searchField, results;

begin 

   // Obtain the Google page
   url := 'www.google.com';
   Browsers.Item(btIExplorer).Run(url);
   browser := Sys.Browser;
   page := browser.Page('*');

   // Obtain the search field
   searchField := page.NativeWebObject.Find('name', 'q', 'input');

   // Enter "TestComplete" in the search field
   searchField.SetText('TestComplete');
   searchField.Keys('[Enter]');

   // Wait until the results are loaded
   page.Wait;

  // Get the page with one of the specified domains
   page := browser.page('regexp:https://www.google.(com)|(de)|(fr)|(es)/\?.*');

   // Obtain the result link
   results := page.FindChildByXPath('//div[@class="rc"]//h3//a');

   if aqObject.GetVarType(results) <> varNull then 
     // Click the link
     results.Click()
   else
     Log.Error('Cannot obtain the search results');

end;

C++Script, C#Script

function Test()
{

  // Obtain the Google page
  var url = "www.google.com";
  Browsers["Item"](btIExplorer).Run(url);
  var browser = Sys["Browser"]();
  var page = browser["Page"]("*");

  // Obtain the search field
  var searchField = page["NativeWebObject"]["Find"]("name", "q", "input");

  // Enter "TestComplete" in the search field
  searchField["SetText"]("TestComplete");
  searchField["Keys"]("[Enter]");

  // Wait until the results are loaded
  page["Wait"]();

  // Get the page with one of the specified domains
  page = browser["page"]("regexp:https://www.google.(com)|(de)|(fr)|(es)/\?.*");

  // Obtain the result link
  var results = page["FindChildByXPath"]("//div[@class='rc']//h3//a");

  if (results != null)
  {
    // Click the link
    results["Click"]();
  }
  else 
    Log["Error"]("Cannot obtain the search results");

}

You can also configure TestComplete so that it ignores values of specific query string parameters during test recording. To do this, enable the Ignore dynamic URL parameters property of your project and specify the list of parameters to ignore. For example, for the above URL, you can add the page_id and session_id parameters to the ignore list. After you have set up your project in this way, TestComplete will record the * wildcard for the specified query string parameter values, thus making the recorded script more stable.

Moreover, if a web page or another element was mapped using its URL containing dynamic parameters as a recognition attribute, and one of its dynamic parameters was added to the ignore list, TestComplete will suggest you to update the mapped item’s recognition attributes as well.

See Also

Web Testing - Examples
Common Tasks for Web Testing
Testing Web Applications

Highlight search results