Parameterizing the Browser for a Test Run

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

Instead of hard-coding the browser name in a web test, you can modify it so that it accepts the desired browser as an external parameter.

The Run Browser and Navigate keyword operations, as well as the Browsers.Item scripting property, allow specifying the desired browser indirectly: as an input test parameter, project variable, value retrieved from the data-driven engine and so on.

In this case, the browser that should be used in the test is specified programmatically, so, you can easily command TestComplete to perform the same test in more than one browser by passing different browser IDs (process names, constants, indexes) as test parameters.

Parameterizing Keyword Tests

  1. Suppose you have recorded a keyword test that starts Internet Explorer, performs the needed test actions and then closes the browser. In this test, the browser name is assigned explicitly.

    Recorded test
  2. Now you can add test parameters. Click the Add Parameter button on the toolbar of the Keyword Test editor. In the ensuing Add Parameter wizard, create a string parameter and name it BrowserName.

    Adding test parameter
  3. And finally, you need to replace the hard-coded browser name in the Run Browser operation with the created BrowserName parameter.

    Specifying web browser using a test parameter
  4. That's it. Now you can call this parameterized test from another keyword test:

    To call another test from keyword tests, you can use the Run Test , Run Keyword Test and Run Script Routine operations.

    Parameterized cross-browser tests in keyword test

    Also, you can call a parameterized test as a test item:

    Parameterized cross-browser tests as test items

Parameterizing Script Tests

  1. The recorded script test that starts Internet Explorer, performs the needed test actions and then closes the browser looks like this (note that the browser is specified as a hard-coded constant):

    JavaScript, JScript

    function TestControlsAndDialogs1()
    {
      // Launch the browser
      Browsers.Item(btIExplorer).Run("http://smartbear.com");

      // Test the web site
      ...

      // Close the browser
      Aliases.browser.BrowserWindow.Close();
    }

    Python

    def TestControlsAndDialogs1():
      # Launch browser
      Browsers.Item[btIExplorer].Run('http://smartbear.com');
    
      # Test the web site
      # ...
    
      # Close browser
      Aliases.browser.BrowserWindow.Close();

    VBScript

    Sub TestControlsAndDialogs1
      ' Launch the browser
      Browsers.Item(btIExplorer).Run "http://smartbear.com"

      ' Test the web site
      ...

      ' Close the browser
      Aliases.browser.BrowserWindow.Close
    End Sub

    DelphiScript

    procedure TestControlsAndDialogs1();
    begin
      // Launch browser
      Browsers.Item(btIExplorer).Run('http://smartbear.com');

      // Test the web site
      ...

      // Close browser
      Aliases.browser.BrowserWindow.Close();
    end;

    C++Script, C#Script


    function TestControlsAndDialogs1()
    {
      // Launch the browser
      Browsers["Item"](btIExplorer)["Run"]("http://smartbear.com");

      // Test the web site
      ...

      // Close the browser
      Aliases["browser"]["BrowserWindow"]["Close"]();
    }

  2. Transforming the recorded test into a parameterized test is as easy as adding an input parameter to a routine and replacing a hard-coded constant with a parameter name:

    JavaScript, JScript

    function TestControlsAndDialogs(BrowserName)
    {
      // Launch the browser
      Browsers.Item(BrowserName).Run("http://smartbear.com");

      // Test the web site
      ...

      // Close the browser
      Aliases.browser.BrowserWindow.Close();
    }

    Python

    def TestControlsAndDialogs(BrowserName):
      # Launch the browser
      Browsers.Item[BrowserName].Run("http://smartbear.com");
    
      # Test the web site
      # ...
    
      # Close the browser
      Aliases.browser.BrowserWindow.Close();

    VBScript

    Sub TestControlsAndDialogs(BrowserName)
      ' Launch the browser
      Browsers.Item(BrowserName).Run "http://smartbear.com"

      ' Test the web site
      ...

      ' Close the browser
      Aliases.browser.BrowserWindow.Close
    End Sub

    DelphiScript

    procedure TestControlsAndDialogs(BrowserName);
    begin
      // Launch browser
      Browsers.Item(BrowserName).Run('http://smartbear.com');

      // Test the web site
      ...

      // Close browser
      Aliases.browser.BrowserWindow.Close();
    end;

    C++Script, C#Script


    function TestControlsAndDialogs(BrowserName)
    {
      // Launch the browser
      Browsers["Item"](BrowserName)["Run"]("http://smartbear.com");

      // Test the web site
      ...

      // Close the browser
      Aliases["browser"]["BrowserWindow"]["Close"]();
    }

  3. Now you can call this parameterized routine from another script test:

    JavaScript, JScript

    function TestSite()
    {
      //Test Controls and Dialogs
      TestControlsAndDialogs(Browsers.btIExplorer);
      TestControlsAndDialogs(Browsers.btFirefox);
      TestControlsAndDialogs(Browsers.btChrome);
      TestControlsAndDialogs(Browsers.btEdge);
    }

    Python

    def TestSite():
      # Test Controls and Dialogs
      TestControlsAndDialogs(Browsers.btIExplorer);
      TestControlsAndDialogs(Browsers.btFirefox);
      TestControlsAndDialogs(Browsers.btChrome);
      TestControlsAndDialogs(Browsers.btOpera);
      TestControlsAndDialogs(Browsers.btEdge);

    VBScript

    Sub TestSite
      'Test Controls and Dialogs
      TestControlsAndDialogs(Browsers.btIExplorer)
      TestControlsAndDialogs(Browsers.btFirefox)
      TestControlsAndDialogs(Browsers.btChrome)
      TestControlsAndDialogs(Browsers.btEdge)
    End Sub

    DelphiScript

    procedure TestSite;
    begin
      //Test Controls and Dialogs
      TestControlsAndDialogs(Browsers.btIExplorer);
      TestControlsAndDialogs(Browsers.btFirefox);
      TestControlsAndDialogs(Browsers.btChrome);
      TestControlsAndDialogs(Browsers.btEdge);
    end;

    C++Script, C#Script

    function TestSite()
    {
      //Test Controls and Dialogs
      TestControlsAndDialogs(Browsers["btIExplorer"]);
      TestControlsAndDialogs(Browsers["btFirefox"]);
      TestControlsAndDialogs(Browsers["btChrome"]);
      TestControlsAndDialogs(Browsers["btEdge"]);
    }

    -- or as a test item:

    Parameterized cross-browser tests as test items

See Also

About Cross-Browser Testing in TestComplete
Running Tests in Multiple Browsers
Handling Browser Differences

Highlight search results