Creating Cross-Browser Mobile Web Tests Using Emulator

Applies to TestComplete 15.68, last modified on October 16, 2024

It is important to test mobile web applications in different browsers with different screen sizes to ensure that the application looks and works correctly with all of them. This topic explains how you can create cross-browser mobile web tests in TestComplete.

General Notes

  • Google Chrome must be closed before you run mobile web tests. Cross-browser tests must also close Chrome at the end of each browser iteration. A running Chrome instance will interfere with mobile tests - the test engine will open the tested web site in the running Chrome instead of the mobile browser emulator.

  • Mobile browser profiles to be used in tests are configured in the project’s Virtual Browsers properties. Note that only the enabled profiles (those that are checked in the table) are listed in the Virtual Browser Loop keyword operation and are available through the VirtualBrowsers scripting object. By checking and unchecking the browsers in the project properties, you can change the set of browsers used in cross-browser mobile web tests without actually modifying the tests.

Creating a Test for a Single Browser

To create a cross-browser mobile web test, you first need to create a test for a single mobile browser. You can do this, for example, by recording a test in the mobile browser emulator. For more information, see Creating and Recording Mobile Web Tests.

The recorded test includes the name of the browser profile you used during the test recording (for example, Apple iPad). To make the test cross-browser, you can wrap it in a loop that iterates through all the needed browsers and use the current iteration’s browser name instead of the hard-coded browser name. The following sections describe the necessary changes for keyword tests and scripts.

Using Multiple Mobile Browsers in Keyword Tests

In keyword tests, you can use the Virtual Browser Loop operation to repeat test operations in different mobile browsers. To add a Virtual Browser Loop into a recorded mobile web test, do the following:

  1. Insert the Virtual Browser Loop operation at the beginning of the test and configure its parameters:

    • Select whether to use all enabled mobile browser profiles or just specific ones.

    • Specify the URL of the tested web site.

  2. Delete (or disable) the recorded Run Virtual Browser and browser.ToUrl operations at the beginning of the test. The Virtual Browser Loop operation replaces them.

  3. Place the test operations inside the loop (indented to the right of it).

  4. Before the end of the loop, add an operation that would close the browser emulator. For example, you can use the On-Screen Action operation to call the Close method of the Aliases.browser object.

During the test run, the Virtual Browser Loop operation launches the mobile browser emulator with each of the specified browser profiles in turn and executes the loop’s child operations against the tested web site in the emulator.

Using the Virtual Browser Loop keyword operation

Using Multiple Mobile Browsers in Scripts

In scripted tests, you can repeat test operations in all mobile browsers using a for loop through the VirtualBrowsers collection as shown in the example below. Make sure to close the browser at the end of each loop iteration.

JavaScript, JScript

function Test1()
{
  for (var i = 0; i < VirtualBrowsers.Count; i++)
  {
    VirtualBrowsers.Item(i).Run("http://m.smartbear.com");

    // Perform test operations
    ...

    Aliases.browser.Close();
  }
}

Python

def Test1():
  for i in range (0, VirtualBrowsers.Count - 1):
    VirtualBrowsers.Item(i).Run("http://m.smartbear.com");

    # Perform test operations
    # ...

    Aliases.browser.Close();

VBScript

Sub Test1
  Dim i

  For i = 0 To VirtualBrowsers.Count
    VirtualBrowsers.Item(i).Run "http://m.smartbear.com"

    ' Perform test operations
    ...

    Aliases.browser.Close
  Next
End Sub

DelphiScript

procedure Test1;
var i;
begin
  for i := 0 to VirtualBrowsers.Count - 1 do
  begin
    VirtualBrowsers.Item(i).Run('http://m.smartbear.com');

    // Perform test operations
    ...

    Aliases.browser.Close;
  end;
end;

C++Script, C#Script

function Test1()
{
  for (var i = 0; i < VirtualBrowsers["Count"]; i++)
  {
    VirtualBrowsers["Item"](i)["Run"]("http://m.smartbear.com");

    // Perform test operations
    ...

    Aliases["browser"]["Close"]();
  }
}

Another way to loop through all mobile browsers is to use an enumerator. TestComplete provides a language-independent Utils.Enumerator method that returns the Enumerator object. You can also use language-specific statements. In VBScript, enumeration is done by using the For Each statement; in JScript, Python, C++Script and C#Script - the Enumerator object; DelphiScript does not provide a native way to create an enumerator loop.

JavaScript

function Test2()
{
  for (let browser of VirtualBrowsers)
  {
    browser.Run("http://m.smartbear.com");

    // Perform test operations
    ...

    Aliases.browser.Close();
  }
}

JScript

function Test2()
{
  var enumBrowsers = new Enumerator(VirtualBrowsers);
  while (!enumBrowsers.atEnd())
  {
    enumBrowsers.item().Run("http://m.smartbear.com");

    // Perform test operations
    ...

    Aliases.browser.Close();
    enumBrowsers.moveNext();
  }
}

Python

def Test2():
  enumBrowsers = Utils.Enumerator(VirtualBrowsers);
  while not enumBrowsers.AtEnd():
    enumBrowsers.item.Run("http://m.smartbear.com");

    # Perform test operations
    # ...

    Sys.Browser.Close();
    enumBrowsers.MoveNext();

VBScript

Sub Test2
  Dim browser
  For Each browser In VirtualBrowsers
    browser.Run "http://m.smartbear.com"

    ' Perform test operations
    ...

    Aliases.browser.Close
  Next
End Sub

DelphiScript

procedure Test2;
var enumBrowsers;
begin
  enumBrowsers := Utils.Enumerator(VirtualBrowsers);
  while not enumBrowsers.AtEnd do
  begin
    enumBrowsers.item.Run('http://m.smartbear.com');

    // Perform test operations
    ...

    Sys.Browser.Close;
    enumBrowsers.MoveNext;
  end;
end;

C++Script, C#Script

function Test2()
{
  var enumBrowsers = new Enumerator(VirtualBrowsers);
  while (!enumBrowsers["atEnd"]())
  {
    enumBrowsers["item"]()["Run"]("http://m.smartbear.com");

    // Perform test operations
    ...

    Aliases["browser"]["Close"]();
    enumBrowsers["moveNext"]();
  }
}

If you want to run a test using only some of the mobile browser profiles, you can use a script like the one below. It takes the browser names to iterate through from an array.

JavaScript, JScript

function Test3()
{
  var arrBrowsers, strBrowserName, i;

  arrBrowsers = ["Apple iPhone 7", "Samsung Galaxy S3", "Nokia Lumia 800"];

  for (i = 0; i < arrBrowsers.length; i++)
  {
    strBrowserName = arrBrowsers[i];
    VirtualBrowsers.Item(strBrowserName).Run("http://m.smartbear.com");

    // Perform test operations
    ...

    Aliases.browser.Close();
  }
}

Python

def Test3():

  arrBrowsers = ["Apple iPhone 7", "Samsung Galaxy S3", "Nokia Lumia 800"];

  for i in range (0, arrBrowsers.length):
    strBrowserName = arrBrowsers[i];
    VirtualBrowsers.Item(strBrowserName).Run("http://m.smartbear.com");

    # Perform test operations
    # ...

    Aliases.browser.Close();

VBScript

Sub Test3
  Dim arrBrowsers, strBrowserName, i

  arrBrowsers = Array("Apple iPhone 7", "Samsung Galaxy S3", "Nokia Lumia 800")

  For i = 0 To UBound(arrBrowsers)
    strBrowserName = arrBrowsers(i)
    VirtualBrowsers.Item(strBrowserName).Run "http://m.smartbear.com"

    ' Perform test operations
    ...

    Aliases.browser.Close
  Next
End Sub

DelphiScript

procedure Test3;
var arrBrowsers, strBrowserName, i;
begin
  arrBrowsers := ['Apple iPhone 7', 'Samsung Galaxy S3', 'Nokia Lumia 800'];

  for i := 0 to VarArrayHighBound(arrBrowsers, 1) do
  begin
    strBrowserName := arrBrowsers[i];
    VirtualBrowsers.Item(strBrowserName).Run('http://m.smartbear.com');

    // Perform test operations
    ...

    Aliases.browser.Close;
  end;
end;

C++Script, C#Script

function Test3()
{
  var arrBrowsers, strBrowserName, i;

  arrBrowsers = ["Apple iPhone 7", "Samsung Galaxy S3", "Nokia Lumia 800"];

  for (i = 0; i < arrBrowsers["length"]; i++)
  {
    strBrowserName = arrBrowsers[i];
    VirtualBrowsers["Item"](strBrowserName)["Run"]("http://m.smartbear.com");

    // Perform test operations
    ...

    Aliases["browser"]["Close"]();
  }
}

See Also

Testing Mobile Web Applications Using Emulator
Testing Mobile Web Applications Using Emulator - Overview
Changing Mobile Browser Orientation
Virtual Browser Loop Operation
VirtualBrowsers Object

Highlight search results