Retrieving an open Browser using a URL pattern

Retrieving an open Browser using a URL pattern

Often during a test execution against a web application a number of browser instances may be opened making it difficult to determine which browser your automation code interacts with. Using the GetUrl( ) method available on the BrowserWindow winclass/class, it is possible to create a simple function that will return a browser based on a particular URL pattern. For Example:

SilkTest Classic:

[ ] //Retrieves a browser application that matches a particular URL
[-] WINDOW GetBrowserByURL(STRING sURL)
     [ ] LIST OF WINDOW lwBrowsers = Desktop.FindAll("//BrowserApplication")
     [ ] WINDOW wBrowser
     [ ]
     [-] for each wBrowser in lwBrowsers
          [-] if (MatchStr(sURL,wBrowser.Find("//BrowserWindow").GetUrl()))
               [ ] return wBrowser
     [ ] LogWarning("No Browser found matching {sURL}")
     [ ] return Null



Silk4J:

public static BrowserApplication getBrowserByURL(String sURL){
     List<BrowserApplication> openBrowsers = desktop.findAll("/BrowserApplication");

     for(int i=0; i<openBrowsers.size(); i++ ){
          if(openBrowsers.get(i).<BrowserWindow>find("/BrowserWindow").getUrl().matches(sURL)){
               return openBrowsers.get(i);
          }
     }


     return null;
}

To use the above functions in your automation scripts, the code would appear similar to:

SilkTest Classic:

[ ] Window wMainBrowser = GetBrowserByURL("*demo.borland.com*")
[ ]
[ ]
[ ] wMainBrowser.SetActive()
[ ] wMainBrowser.FindBrowserWindow("/BrowserWindow").Navigate("www.microfocus.com")



Silk4J:

BrowserApplication wBrowser = getBrowserByURL(".*demo.borland.com.*");
if(wBrowser != null){
     wBrowser.setActive();
     wBrowser.<BrowserWindow>find("/BrowserWindow").navigate("http://www.microfocus.com");
}

DISCLAIMER:

Some content on Community Tips & Information pages is not officially supported by Micro Focus. Please refer to our Terms of Use for more detail.
Top Contributors
Version history
Revision #:
1 of 1
Last update:
‎2013-10-30 19:30
Updated by:
 
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.