| Cogitek RIATest 3 Documentation | Copyright © Cogitek Inc. |
By default, RIATest opens a browser and loads your application at the beginning of project execution and closes 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.
Now let's see how to control browser opening and closing from the test scripts. You can use the openBrowser function to open a browser instance and the closeBrowser function to close it. The openBrowser function accepts two parameters, the first is the URL to open in the browser, the second controls 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 the openBrowser function will not wait for the Agent to connect hence the second line to do this. The BR_IE option requests that Internet Explorer browser is opened. The following is the list of possible options for the 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 the Agent connection is accepted your script can execute as usual and interact with your application components. You can close the browser at any time by calling the closeBrowser function:
closeBrowser();
By default when RIATest is told to launch a browser at the beginning of project execution (when the "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 the "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"));
You can call the openBrowser function more than once without calling the closeBrowser function. In this 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 capabilities that involve operations that must be done via both applications.
When you want to close a previously open browser and you have several open you need to specify which browser you want to close. This is made possible by the openBrowser function returning an instance of Process object corresponding to the opened browser process. You can close the browser by simply calling the kill function or alternatively pass a Process instance to the 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 the closeBrowser function is called without parameters it closes the last browser opened. Note that you need to enable the "Allow multiple Agent connections" project option to allow RIATest to accept connections from multiple Agents when you open multiple applications each in their own browser.
Important: openBrowser() function creates a Process object and stores it in an internal variable. If a second call to openBrowser() is made the value in the internal variable is overwritten causing the Process object destruction which results in process termination. When you assign the result of openBrowser() to a variable as in the above example you keep live reference to the Process object and second call to openBrowser no longer destroys the process.
RIATest uses application ID to distinguish between applications that are currently connected. RIATest does not allow applications with the same ID to connect simultaneously. However sometimes you need to run tests against more than one instance of the same application. One way to automate two instances of the same application is to have two copies of HTML wrapper file which will refer to the same SWF file but will use different ID. make a copy of your application html (or RIATestLoader.html if using RIATest Loader) and change the ID manually in HTML wrapper to something else. In default HTML wrapper generated by Flex Builder the ID parameter is usually passed to AC_FL_RunContent() JavaScript function:
AC_FL_RunContent(
"flashvars", "params="+window.location.search,
"src", "RIATestLoader",
"width", "100%",
"height", "100%",
"align", "middle",
"id", "RIATestLoader2", <--------- NOTE THE CHANGE
"quality", "high",
"bgcolor", "#869ca7",
"name", "RIATestLoader",
"allowScriptAccess","sameDomain",
"type", "application/x-shockwave-flash",
"pluginspage", "http://www.adobe.com/go/getflashplayer"
);
You will need to make sure you are using appropriate url when calling openBrowser() to open the second instance.
By default after all tests are run and project execution is finished RIATest closes the browsers opened via openBrowser() call. If you would like the browser to remain open (for example to perform further testing using another testing tool) you need to detach the browser process, e.g.:
var browserProc = openBrowser("someurl.html");
browserProc.detach();
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, leads to better work separation in large testing teams and allows for faster 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.
If you have a running Firefox instance by default RIATest will open your application in a new tab in the current instance of Firefox. When this happens RIATest will not be able to close the tab when the tests finish (or when closeBrowser() function is called).
The solution is to use custom profile for Firefox:
openBrowser("file://C:/MyApp.html",BR_CUSTOM);
Now whenever you attempt to launch your application from RIATest it will use custom profile and will launch separate Firefox process which will allow it to close the process when needed.