How to work with multiple tabs in a single browser window
You would have observed that nowadays, the modern web browsers support tabbed browsing. Tabbed browsing is a feature using which you can load multiple web pages in different tabs within the same browser window. Using this feature, it becomes very easy & convenient to switch between different tabs and view the web pages. In this article, we would see how to work with a tabbed browser in Internet Explorer using QTP.
Support for omnipresent tabbed browsing was introduced in QTP with version 9.5. QTP treats each tab in the browser as a separate browser window. For example, 2 tabs in a single browser window will be treated as 2 separate browser windows, each having their distinct set of properties (including browser CreationTime). So, you don’t need to do anything extra than the usual stuff to work with multiple tabs. The code for handling multiple browsers would work perfectly fine for multiple tabs within sigle browser window. QTP also provides few built-in methods to work with multi-tabs. Let’s see some examples of working with multiple tabs in QTP.
Sample Script 1: Opening different web paged in multiple tabs. This example uses QTP’s in-built methods to load multiple web pages in different tabs within a single browser window.
'Open a new Browser SystemUtil.Run "iexplore.exe", "http://www.google.com" 'Browser Sync Browser("CreationTime:=0").Sync 'Find total number of tabs in the browser window iTabs = Browser("CreationTime:=0").GetROProperty("number of tabs") msgbox iTabs 'Displays the value 1 'Open a new tab within the same browser Browser("CreationTime:=0").OpenNewTab() 'Sync for new tab Browser("CreationTime:=1").Sync 'Load some web page in the new tab Browser("CreationTime:=1").Navigate "http://www.yahoo.com" 'Find total number of tabs in the browser window iTabs = Browser("CreationTime:=0").GetROProperty("number of tabs") msgbox iTabs 'Displays the value 2 'Close all the tabs in the browser window Browser("CreationTime:=0").CloseAllTabs() 'OR -> Browser("CreationTime:=1").CloseAllTabs()
Another important point to be noted here is that if you are working on multiple web pages, you should really not be bothered about whether the web pages are opened in multiple tabs or in multiple browsers. This is because the same code would work in both the situations. Let’s see an example for the same.
Sample Script 2 This example searches for a string in both Google and Yahoo! web pages. As a pre-requisite, open Google and Yahoo! in separate browsers and then run the code. After this, open Google and Yahoo! in multiple tabs within the same browser and then run the code. The code should run successfully in both the cases.
'Search something in Google Browser("title:=.*Google.*").Page("title:=.*Google.*").WebEdit("name:=q").Set "google" Browser("title:=.*Google.*").Page("title:=.*Google.*").WebButton("name:=btnG").Click Browser("title:=.*Google.*").Page("title:=.*Google.*").Sync 'Search something in Yahoo! Browser("title:=.*Yahoo!.*").Page("title:=.*Yahoo!.*").WebEdit("name:=p","class:=input-query input-long med-large ").Set "yahoo" Browser("title:=.*Yahoo!.*").Page("title:=.*Yahoo!.*").WebButton("name:=Search","class:=searchsubmit med-large y-fp-pg-grad").Click Browser("title:=.*Yahoo!.*").Page("title:=.*Yahoo!.*").Sync 'Close both the browsers Browser("title:=.*Google.*").Close() Browser("title:=.*Yahoo!.*").Close()
Hope you’ll find this information useful. Please do share your comments about this article.
If you enjoyed this article, you can join our blog to get free email updates directly in your inbox.