TestComplete provides a possibility to perform Web-applications testing in all of the popular browsers (Internet Explorer, Mozilla Firefox, Google Chrome, Apple Safari, and Opera), easily switching in-between.
In this recipe we will consider launching one script in several browsers.
With the help of recording means let's make a record of any test in any of the available browser at hand. In our example, Internet Explorer is used to carry out the following actions:
function Test1() { var form; Browsers.Item(btIExplorer).Run("http://smartbear.com/"); browser = Aliases.browser; page = browser.pageAutomationTestingWebMonitori; page.formAspnetform.linkLookingForAFreeTrial.Click(); browser.pageSoftwareTestingWebMonitoring.Wait(); }
To launch the recorded script in several browsers, go about the following actions:
function Test2()
{
var browsers = ["iexplore", "firefox"];
for(var i = 0; i < browsers.length; i++)
{
//TODO: place code here
}
}
//TODO
comment and place the contents of the earlier written Test1
function instead.Browsers.Item(btIExplorer)
with the following: Browsers.Item(browsers[i])
. In the result, we will have the next function ready:function Test2()
{
var browsers = ["iexplore", "firefox"];
for(var i = 0; i < browsers.length; i++)
{
var form;
Browsers.Item(browsers[i]).Run("http://smartbear.com/");
browser = Aliases.browser;
page = browser.pageAutomationTestingWebMonitori;
page.formAspnetform.linkLookingForAFreeTrial.Click();
browser.pageSoftwareTestingWebMonitoring.Wait();
}
}
Test2
function, we will see that the same actions were initially executed in the Internet Explorer browser, and then in Mozilla Firefox.The object Browsers
provides access to any installed browser via the Item
property. This property takes the Index
parameter, with the help of which we are able to assign the particular browser we would like to work in. The Index
parameter can be any number (of a named constant value, just like the btIExplorer
in our case), or the name of a process.
Since working with control elements in all the browsers is executed in a similar fashion, we can record a script in one browser to execute in another or in several others (in our example, we have used the for
loop).
In a real project, it is more convenient to create tests that accept a parameter (the name of browser process), and then launch the tests by passing the name of the browser as a parameter. The resulting function will assume the following shape and form:
function Test3(browserName) { var form; Browsers.Item(browserName).Run("http://smartbear.com/"); }
The launch of this from the test items will appear as follows:
3.147.84.157