Applies to TestComplete 14.10, last modified on June 5, 2019

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 open a web page in a web browser and simulate user actions on the page. If you are running your test using multiple browsers, you must close the browser when the test iteration is over. When the next iteration starts, it will launch another browser. If the previous browser is still running, it can cause ambiguous object identification and your tests will fail. To avoid the problem, configure your tests to close the current web browser automatically at the end of each run.

In Keyword Tests

To close a browser window

To close a web browser, you can call the Close method of the BrowserWindow object that corresponds to the main window of the current web browser. The method has the WaitTimeout parameter that pauses the test run for the specified time or until the window closes. If any of your browsers closes slowly, to avoid starting a new test before the browser closes, set the parameter value accordingly.

To close a browser process

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.

Add closing operations

When recording a test, close the web browser after all the desired actions on the browser are recorded. TestComplete records closing the browser as the On-Screen Action operation that calls the Close method of the browser window.

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

If you recorded a test without closing the browser, you can add the closing operation manually:

To learn how to call object methods from keyword tests, see Calling Object Methods.

In 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