Running Tests in Multiple Browsers

Applies to TestComplete 15.69, last modified on November 13, 2024

Most likely, you will want to run the created cross-browser test in a loop using a new browser on each iteration. The sections of this topic describe different ways to achieve this:

If your test installs or uninstalls a browser during the run, then call the Browsers.Refresh method to update the TestComplete collection of available browsers before working with the browsers. Call this method before the first keyword-test operation or script statement that enumerates browsers, launches a browser, opens a web page or perform any other browser-related operation after the installation or ininstallation is over.

Iterating Through Browsers in Keyword Tests

To perform the same web test in different browsers, you can use the Browser Loop keyword operation. In this operation you define a list of browsers to be launched: these can be all the installed browsers, or just some particular browsers, versions or editions. During the test run TestComplete starts the browsers from that list one after another and executes the child operations in each started browser.

Using the Browser Loop keyword operation
Using the Browser Loop keyword operation
Tip: To convert a Run Browser operation to a Browser Loop operation, right-click the Run Browser operation in the Keyword Test editor and select Make Browser Loop from the context menu.

Enumerating Browsers in Scripts

TestComplete stores the collection of the available browsers in the Browsers object and you can use this object to run a web test in different browsers. The Browsers object allows you to use the built-in syntax of the scripting languages supported by TestComplete to iterate through the object’s collection of items. You can use the Utils.Enumerator method to iterate through browsers. You can also use language-specific statements. In JavaScript, enumeration is done by using the for...of statement, in JScript, Python, C#Script and C++Script - by using the Enumerator object, In VBScript - by using the For Each statement; DelphiScript does not provide a native way to create an enumerator loop.

JavaScript

function IterateBrowsers1()
{
  for (let browser of Browsers)
  {
    // Start a browser instance
    browser.Run();
    // Perform web testing
    // ...
    // Close the browser instance
    Aliases.browser.Close();
    
  }
}

JScript

function IterateBrowsers1()
{
  var browsersEnum, browser;
  browsersEnum = new Enumerator(Browsers);
  while (!browsersEnum.atEnd())
  {
    browser = browsersEnum.item();
    // Start a browser instance
    browser.Run();
    // Perform web testing
    // ...
    // Close the browser instance
    Aliases.browser.Close();
    
    browsersEnum.moveNext();
  }
}

Python

def IterateBrowsers1():
  browsersEnum = Utils.Enumerator(Browsers);

  while not browsersEnum.AtEnd:
    browser = browsersEnum.item;
    # Start a browser instance
    browser.Run();
    # Perform web testing
    # ...
    # Close the browser instance
    
    Aliases.browser.Close();
    browsersEnum.MoveNext();

VBScript

Sub IterateBrowsers1
  Dim browser
  For Each browser In Browsers
    ' Start a browser instance
    browser.Run
    ' Perform web testing
    '...
    ' Close the browser instance
    Aliases.browser.Close
  Next
End Sub

DelphiScript

procedure IterateBrowsers1;
var browsersEnum, browser;
begin
  browsersEnum := Utils.Enumerator(Browsers);

  while not browsersEnum.AtEnd do
  begin
    browser := browsersEnum.item;
    // Start a browser instance
    browser.Run;
    // Perform web testing
    // ...
    // Close the browser instance
    Aliases.browser.Close;
    browsersEnum.MoveNext;
  end;
end;

C++Script, C#Script

function IterateBrowsers1()
{
  var browsersEnum = new Enumerator(Browsers);
  while (!browsersEnum["atEnd"]())
    {
    browser = browsersEnum["item"]();
    // Start a browser instance
    browser["Run"]();
    // Perform web testing
    // ...
    // Close the browser instance
    Aliases["browser"]["Close"]();
    browsersEnum["moveNext"]();
    }
}

Iterating Through Browsers in Scripts

Another way is to retrieve the total number of the browser versions available for web testing through the Browsers.Count property and then use the standard loop statements of the corresponding scripting languages:

JavaScript, JScript

function IterateBrowsers2()
{
  for (var i = 0; i < Browsers.Count; i++)
  {
    browser = Browsers.Item(i);
    // Start a browser instance
    browser.Run();
    // Perform web testing
    // ...
    // Close the browser instance
    Aliases.browser.Close();
  }
}

Python

def IterateBrowsers2():
  for i in range (0, Browsers.Count-1): 
    browser = Browsers.Item[i];
    # Start a browser instance
    browser.Run();
    # Perform web testing
    # ...
    # Close the browser instance
    Aliases.browser.Close();
    Delay(3000);

VBScript

Sub IterateBrowsers2
  Dim browser
  For i = 0 to Browsers.Count - 1
    Set browser = Browsers.Item(i)
    ' Start a browser instance
    browser.Run
    ' Perform web testing
    '...
    ' Close the browser instance
    Aliases.browser.Close
  Next
End Sub

DelphiScript

procedure IterateBrowsers2;
var browser, i;
begin
  for i := 0 to Browsers.Count-1 do 
    begin
      browser := Browsers.Item(i);
      // Start a browser instance
      browser.Run;
      // Perform web testing
      // ...
      // Close the browser instance
      Aliases.browser.Close;
      Delay(3000);
    end;
end;

C++Script, C#Script

function IterateBrowsers2()
{
  for (var i = 0; i < Browsers["Count"]; i++)
  {
    browser = Browsers["Item"](i);
    // Start a browser instance
    browser["Run"]();
    // Perform web testing
    // ...
    // Close the browser instance
    Aliases["browser"]["Close"]();
  }
}

See Also

About Cross-Browser Testing in TestComplete
Launch Web Browsers
Parameterizing the Browser for a Test Run
Handling Browser Differences

Highlight search results