Checking if Browser Is Running

Applies to TestComplete 14.30, last modified on November 21, 2019

Playing back the recorded tests always requires that the tested application be in the same state as it was before test recording. For instance, if your tested browser was running before you started the recording, you have to launch it manually each time before you run the test. Also, we recommend that your test returns the tested application to the initial state at the end of the test. In other words, if your test launches the browser automatically at the beginning, it is recommended that the test closes the browser at the end of the run. This approach allows you to avoid making additional preparations before a new test run.

However, closing the browser at the end of the test does not guarantee the browser is actually closed. For instance, your test may fail before it reaches the operation that closes the browser. In such cases, the browser will stay open after the test run is over.

Although the test will not fail if you try to run a new browser instance at the beginning of the test execution, it is not recommended to use the previous session for a new test run. In such cases, the tested browser may behave unexpectedly. For example, the cookies or browser cache may store information specific to the open session. To minimize the influence of such factors and to make your web tests more reliable, we recommend that you check whether the browser is running at the beginning of each test run. If it is running, close it and then reopen it with the desired page loaded. This approach will help you avoid test failures caused by the previous browser sessions.

This topic describes how you can check whether the tested web browser is running from your tests.

Checking if the Browser Is Running From Keyword Tests

There can be several instances of running browsers. To close all of them, you can check for a running browser and close it until no running browsers are left. Alternatively, to check for a running browser only once, you can use a simple conditional branch.

Repeated Checks for Running Browsers

To make sure that all running browsers are closed, you need to perform a repeated check for a running browser. For this purpose, we recommend that you use the While Loop operation with the following conditional statement: Sys.WaitBrowser().Exists Equals True. For detailed information on specifying conditions, see the operation’s description.

To close a running browser you can, for example, call the Close method of the Browser object using the On-Screen Action operation. See Closing Browsers for details. The closing actions must be added as child operations of the While Loop operation. Otherwise, an infinite loop will occur.

To re-launch the browser, use the Run Browser operation.

The image below is a snapshot of a sample keyword test that checks if one or more browsers are running, closes all running browsers (if any) and then launches the browser with the http://smartbear.com page open in it.

Closing all running browsers from keyword tests.

Single Check for Running Browsers

To close only one instance of a browser, or to perform any other action when a browser is already running, you can also use conditional operations. In this case, the test will search for a running browser only once and will then proceed to the other instructions.

For this purpose, you can use the If Object operation where the Object parameter is set to Sys.WaitBrowser() and the Condition parameter is set to Exists.

The image below demonstrates a sample keyword test that checks if a browser is running and displays a custom warning message on success.

Checking if browser is running from keyword tests.

Checking if the Browser Is Running From Scripts

To check whether the browser is running, you can use the Exists property of the Browser test object that corresponds to the web browser. To obtain the Browser test object, you can use the Sys.WaitBrowser() method, or the Aliases.browser mapped name (if Name Mapping is used).

To close the browser, you can use any of the approaches described in the Closing Browsers help topic. For example, you can call the Browser.Close() method.

To close all the instances of running browsers, check for a browser within the while loop. To close only one instance of a browser, or to perform any other action when a browser is already running, check for a running browser within the if...then statement.

To launch a new instance of the web browser, use the Browsers.Item(...).Run method. See Launching Browsers for details.

The following code closes all running browsers (if any) and then launches a new instance of Internet Explorer:

JavaScript, JScript

function Check()
{
  while (Sys.WaitBrowser().Exists)
  // if (Sys.WaitBrowser().Exists)
    Sys.WaitBrowser().Close();

  Browsers.Item(btIExplorer).Run("http://smartbear.com");
}

Python

def Check():
  while Sys.WaitBrowser().Exists:
  # if Sys.WaitBrowser().Exists then 
    Sys.WaitBrowser().Close();

  Browsers.Item[btIExplorer].Run('http://smartbear.com');

VBScript

Sub Check
  While Sys.WaitBrowser().Exists
  ' If Sys.WaitBrowser().Exists Then
    Call Sys.WaitBrowser().Close()
  Wend
  ' End If
  Call Browsers.Item(btIExplorer).Run("http://smartbear.com")
End Sub

DelphiScript

procedure Check;
begin
  while Sys.WaitBrowser().Exists do
  // if Sys.WaitBrowser().Exists then
    Sys.WaitBrowser().Close();

  Browsers.Item(btIExplorer).Run('http://smartbear.com');
end;

C++Script, C#Script

function Check()
{
  while ( Sys["WaitBrowser"]()["Exists"] )
   // if ( Sys["WaitBrowser"]()["Exists"] )
    Sys["WaitBrowser"]()["Close"]();

  Browsers["Item"](btIExplorer)["Run"]("http://smartbear.com");
}

See Also

Testing Web Applications
Common Tasks for Web Testing
Launching Browsers
Closing Browsers

Highlight search results