Closing Browsers

Applies to TestComplete 12.60, last modified on September 17, 2018

For a successful test playback, you must make sure that the initial conditions of the test run correspond to those you had when creating the test. To limit the number of additional preparations before the test run, your test must return the tested application to the initial state at the end of the playback.

Typically, when testing web applications, you run the desired web page in the web browser and perform some actions over the page. If you are playing back your test using multiple browsers, you must close the browser when the test run is over. When the next test starts, it will launch another browser. That is, if the previous browser is still running, it can cause ambiguous object identification and your tests will fail. To avoid the problem, close the browser automatically at the end of each test.

This topic explains how you can automatically close a web browser from your keyword tests and scripts.

Closing a Browser From Keyword Tests

When recording a test, close the web browser after all the desired operations are performed. Closing the browser is recorded as the Close method of the BrowserWindow object that corresponds to the current browser window.

The Close method has the WaitTimeout parameter that pauses the script execution for the specified time or until the window closes. If any of your browsers closes slowly, in order to avoid starting a new test before the browser closes, increase the parameter’s value.

The image below is a sample keyword test that performs some actions over the tested web page and then closes the active browser.

Sample keyword test that closes the browser after all the operations are performed
Sample keyword test that closes the browser after all the operations are performed

Another way to close the browser is to call the Close method of the corresponding Browser process. This method closes the browser process directly. For browsers with a multi-process architecture (like Google Chrome or Internet Explorer 10 and later), the Browser.Close() method closes the main browser process along with the child browser processes. The method’s WaitTimeout parameter pauses the test execution for the specified time or until the main and auxiliary  browser processes are closed.

If you recorded a test without closing the browser, you can add the closing operation manually. To do this, use the Call Object Method operation. For more information on how to call object methods from keyword tests, see Calling Object Methods.

Closing a Browser From Scripts

To close a web browser from scripts, you can use the BrowserWindow.Close() or Browser.Close() scripting method:

  • The BrowserWindow.Close() method closes the corresponding browser window. Typically, a browser has only one window (additional browser windows can be opened using the browser’s New Window or Open Link In New Window command.) If there is a single browser window, closing the window will result in closing the browser.

  • The Browser.Close() method closes the browser process directly. At that, it also closes all browser windows and all auxiliary browser processes.

Both methods have the WaitTimeout parameter that specifies the amount of time to wait before closing the browser window or process, respectively. For the Browser.Window.Close() method, the default WaitTimeout parameter value is 2000, for Browser.Close() – 60000, but you can specify any other value.

The following sample script demonstrates how you can close the web browser after all the desired actions are performed.

JavaScript, JScript

function Test1()
{
  // Run the web browser and obtain a web page
  Browsers.Item(btIExplorer).Run("http://smartbear.com/");
  var browser = Sys.Browser();
  var page = browser.Page("*smartbear.com*")

  ...
  // Test the page
  ...

  // Close the web browser
  //browser.Close();
}

Python

def Test1():
  # Run the web browser and obtain a web page
  Browsers.Item[btIExplorer].Run("http://smartbear.com/");
  browser = Sys.Browser();
  page = browser.Page("*smartbear.com*")

  # ...
  # Test the page
  # ...

  # Close the web browser
  browser.Close();

VBScript

Sub Test1
  Dim page, browser

  ' Run the web browser and obtain a web page
  Call Browsers.Item(btIExplorer).Run("http://smartbear.com/")
  Set browser = Sys.Browser
  Set page = browser.Page("*smartbear.com*")

  ...
  ' Test the page
  ...

  ' Close the web browser
  'browser.Close
End Sub

DelphiScript

procedure Test1;
var browser, page;
begin
  // Run the web browser and obtain a web page
  Browsers.Item(btIExplorer).Run('http://smartbear.com/');
  browser := Sys.Browser;
  page := browser.Page('*smartbear.com*');

  ...
  // Test the page
  ...

  // Close the web browser
  //browser.Close();
end;

C++Script, C#Script

function Test1()
{
  // Run the web browser and obtain a web page
  Browsers["Item"](btIExplorer)["Run"]("http://smartbear.com/");
  var browser = Sys["Browser"]();
  var page = browser.Page("https://smartbear.com/")

  ...
  // Test the page
  ...

  // Close the web browser
  //browser["Close"]();
}

See Also

Common Tasks for Web Testing
Checking if Browser Is Running
Launching Browsers
Running Tests in Multiple Browsers
Checking the Current Browser
Preparing Web Browsers

Highlight search results