Friday 31 October 2014

Multiple browser tabs implementation using CodedUI


While i was trying to open multiple sites in same browser in different tabs, it was bit tricky.

I was trying it the way as shown below and was successful in opening a new tab, however i was lauching the site in a new browser.

                  //launching Bing.com
BrowserWindow myBrowser = BrowserWindow.Launch();
myBrowser.NavigateToUrl(new System.Uri("http://bing.com/"));

                 //Clicking on new Tab button

WinTabList tabList = new WinTabList(myBrowser);
WinButton newTabButton = new WinButton(tabList);
newTabButton.SearchProperties[WinButton.PropertyNames.Name] = "New Tab (Ctrl+T)";
Mouse.Click(newTabButton);

               //launching yahoo.com

myBrowser.NavigateToUrl(new System.Uri("http://yahoo.com/"));

              //Clicking on new Tab button
Mouse.Click(newTabButton);


             //launching google.com

myBrowser.NavigateToUrl(new System.Uri("http://google.com/"));


By using the above code, there is a browser opened and two tabs opened, but all three sites are opened in the first tab itself as shown below





the code is successful to open the new tabs but was unable to the to launch the url's in the tabs which were open.


i modified the code and tried finding the new tab and launch the url and even it resulted the same

BrowserWindow myb = BrowserWindow.Launch();
myb.NavigateToUrl(new System.Uri("http://bing.com/"));

WinTabList tabList = new WinTabList(myb);

WinButton newTabButton = new WinButton(tabList);
newTabButton.SearchProperties[WinButton.PropertyNames.Name] = "New Tab (Ctrl+T)";
Mouse.Click(newTabButton);

                 // Finding the new tab opening

myb.SearchProperties[UITestControl.PropertyNames.Name] = "New Tab";
myb.SearchProperties[UITestControl.PropertyNames.ClassName] = "IEFrame";


myb.NavigateToUrl(new System.Uri("http://yahoo.com/"));


Mouse.Click(newTabButton);


myb.SearchProperties[UITestControl.PropertyNames.Name] = "New Tab";

myb.SearchProperties[UITestControl.PropertyNames.ClassName] = "IEFrame";
myb.NavigateToUrl(new System.Uri("http://google.com/"));


Finally the code was modified as below to make it working.

BrowserWindow myBrowser = BrowserWindow.Launch();
myBrowser.NavigateToUrl(new System.Uri("http://bing.com/"));


proc = myBrowser.Process;

WinTabList tabList = new WinTabList(myBrowser);
WinButton newTabButton = new WinButton(tabList);
newTabButton.SearchProperties[WinButton.PropertyNames.Name] = "New Tab (Ctrl+T)";
Mouse.Click(newTabButton);


myBrowser = BrowserWindow.FromProcess(proc);

myBrowser.SearchProperties[UITestControl.PropertyNames.Name] = "New Tab";
myBrowser.SearchProperties[UITestControl.PropertyNames.ClassName] = "IEFrame";
myBrowser.NavigateToUrl(new System.Uri("http://yahoo.com/"));

Mouse.Click(newTabButton);

myBrowser = BrowserWindow.FromProcess(proc);
myBrowser.SearchProperties[UITestControl.PropertyNames.Name] = "New Tab";
myBrowser.SearchProperties[UITestControl.PropertyNames.ClassName] = "IEFrame";
myBrowser.NavigateToUrl(new System.Uri("http://google.com/"));



happy coding :)

Check this How to close a particular tab in a browser with multiple tabs

No comments:

Post a Comment