RIATest 2 Documentation Copyright © RIATest.com

Controlling browser opening and closing

RIATest has a default behavior of opening a browser and loading your application at the beginning of the project execution and closing the browser when the execution finishes. For many applications this is all that is needed.

However there are cases when you want finer control over when the browser should be opened and closed or you need to open multiple browser simultaneously. There are a few project options that control browser opening/closing behavior and also openBrowser/closeBrowser functions which you can call from test scripts.

Let's have a look at project options first.

The first options is "Launch application". This option controls whether a browser (or standalone Flex application) is launched at the beginning of project execution. You may want to disable this option if you want to open the browser manually from the script using openBrowser function (see below).

The second option is "Close browser on test complete". This option controls whether the browser is closed when execution of the project finishes. You can disable this option to leave the browser open after the completion of RIATest script for example if you need to interact with the browser using some other automation tool.

openBrowser/closeBrowser functions

Now let's see how to control browser opening and closing from the test scripts. You can use openBrowser function to open a browser instance and closeBrowser function to close it. openBrowser function accepts two parameters, the first is the URL to open in the browser, the second is various opening options. For example the following code opens Internet Explorer browser using specified URL and waits for the Agent connection:

openBrowser("../Components/app/bin/App.html",BR_IE);
waitfor(isAppConnected("App"));

Note that openBrowser function will not wait for the Agent to connect hence the second line to do it. BR_IE option requests that Internet Explorer browser is opened. The following is the list of possible options for openBrowser function:

BR_RIATESTOPTION Open the browser specified in RIATest Preferences - this is default option
BR_SYSDEFAULT Open default system browser
BR_IE Open Internet Explorer
BR_FIREFOX Open Mozilla Firefox
BR_CUSTOM Open custom browser set in RIATest Preferences

The following options control loading type:

BR_PROJECTLOADTYPE Use loading type specified in project options - this is default option
BR_STANDALONE In browser - standalone application or using remote loader
BR_USELOADER In browser, using local RIATest Runtime loader

You can combine browser and loading options using bitwise OR operator, e.g.:

openBrowser("../UsingLoader/app/bin/App.swf",BR_USELOADER|BR_IE);

Once the browser is opened and Agent connection is accepted your script can execute as usual and interact with your application components. You can close the browser any time by calling closeBrowser function:

closeBrowser();

Changing initial Agent connection timeout

By default when RIATest is told to launch a browser at the beginning of project execution (when "Launch application" project option is enabled) it will launch the browser and wait for 30 seconds for the Agent to connect. If for some reason this is not enough for your application to load you can increase this timeout by disabling "Launch application" option and opening the browser from the first script in your project using the following code:

openBrowser("my-app-url.html"); 
setWaitTimeout(120000); // wait for up to 2 minutes
waitfor(isAppConnected("app-id-here"));

Opening multiple browsers

You can call openBrowser function more than once without calling closeBrowser function. In that case multiple browsers will be opened and you can have several different applications loaded each in its own browser. This can be useful if for example you have two applications: one for users, another for system administrators and want to test certain capability that involves operations that must be done via both applications.

When you want to close previously open browser and you have several opened you need to specify which browser you want to close. This is made possible by openBrowser function returning an instance of Process object corresponding to the opened browser process. You can close the browser by simply calling kill function or alternatively pass Process instance to closeBrowser function, e.g.:

var firstBrowser = openBrowser("../Components/app/bin/App.html",BR_IE);
var secondBrowser = openBrowser("../UsingLoader/app/bin/App.swf",BR_USELOADER|BR_IE);

waitfor(isAppConnected("App"));
waitfor(isAppConnected("RIATestLoader"));

selectApp("App");
include "../Components/tests/SimpleComponents.rts";

selectApp("RIATestLoader");
include "../Components/tests/SimpleComponents.rts";

closeBrowser(firstBrowser);
closeBrowser(secondBrowser);

When closeBrowser function is called without parameters it closes the last browser opened. Note that you need to enable "Allow multiple Agent connections" project option to allow RIATest to accept connections from multiple Agents when you open multiple applications each in its browser.

Using manual browser control to design independent test cases

When designing your testing scenario it is usually advisable to break your entire testing project into test cases which can be independent of each other. This allows you to design each test case independently without relying on the knowledge of post-effects of other test cases and leads to better work separation in large testing teams as well as allows quicker troubleshooting of test cases by running them individually when needed.

One of the ways to ensure your test cases are independent is to open a new browser at the beginning of each test case and close it when the test case is finished. It is up to you to decide how exactly to break your testing project into test cases however it is advisable that each test case is composed of one RIATest script which possibly includes other script files and calls functions from those files. To ensure that each test case uses a fresh browser add the following lines to the beginning of each test case script:

closeBrowser(); // close previously open browser (if any)
openBrowser("my-app-url.html"); // open a new browser for this test case
waitfor(isAppConnected("app-id-here"));

These lines of code will ensure that every test case starts with a fresh browser.

Alternatively create a new script file called for example CaseStartup.rts and put those lines in that file (and additionally anything that you may need to perform at the beginning of each test case) and in each test case simply include CaseStartup.rts:

include "CaseStartup.rts";

Placing browser re-opening code at the beginning of each test case also ensures that should a previous test case fail with an error, execution of that test case is stopped and the next script is started which will re-open the browser and perform next test case.

The advice of making each case a single RIATest script does not mean you are restricted to one script file per test case and have to make huge script files for complex test cases. You can break the test case into multiple script files each implementing various functions and have one 'main' test case script file including other script files and calling functions in order to implement the test case. Add this 'main' test case script file to RIATest project tree and let the rest simply reside in your project directory.