Is it possible to switch tabs of chrome

1 Answer 13 Views
Chrome
Jun
Top achievements
Rank 1
Jun asked on 11 Apr 2024, 01:54 PM
I searched lot, seems old version not support, not sure if the latest version support, but after I try not good. We also tried use keyboard to switch tab, which could switch successfully, but active browser will not switch.

1 Answer, 1 is accepted

Sort by
0
Elena
Telerik team
answered on 16 Apr 2024, 01:08 PM

Hi Jun, 

Switching between opened tabs is not a straight forward scenario for functional testing and is not supported out-of-the-box. Therefore I wonder what is the specific scenario you work on and isn't it possible to achieve it without switching the tabs. Can you share additional details on this topic? 

Although it's not supported by default, switching between tabs can be accomplished with the help of a coded solution. The idea is that you need to switch the ActiveBrowser manually. When a new tab is opened Test Studio switches the ActiveBrowser automatically to the newly opened one and sets it back to the original tab when the popup is closed. Within a coded step, though, you can change the ActiveBrowser with few lines of code. I have prepared a sample project to demonstrate this (for switching between 2 tabs) - it works against the Test Studio documentation site so you can execute it on your end. 

There's one important thing to mention - if the test is executed in Chrome or Edge in the extensionless mode, the tab will not be visually switched but the actions will still be performed. 

I hope this helps. 

Regards,
Elena
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Jun
Top achievements
Rank 1
commented on 22 Apr 2024, 03:07 AM

Thanks Elena very much for the sharing.

Still want to confirm below things:

About "you can change the ActiveBrowser with few lines of code. I have prepared a sample project to demonstrate this (for switching between 2 tabs) - it works against the Test Studio documentation site so you can execute it on your end. "

Can you please share your code, I didn't find code to change active browser, I just reset the find of the elements to the new browser. Want to know if there's other code I can use. 

 

About "if the test is executed in Chrome or Edge in the extensionless mode, the tab will not be visually switched but the actions will still be performed. 

I can understand it will not be visually. But the function still work right?

 

Thanks again,

Jun

 

Elena
Telerik team
commented on 24 Apr 2024, 04:19 PM

Hi Jun, 

The test from the sample project I sent looks like this:

1. Navigate to the URL
2. Perform any necessary steps. 
3. Click the element which opens the new tab. 
4. Connect to the new tab. 
5. Perform some actions in the new tab.
5. Use a coded step to switch the active browser to the first opened tab. 

First coded step looks like this: 

public void SwitchBrowserTabs_CodedStep()
        {
            // Set the last opened active browser in global variable 
            activeBrowser = Manager.Browsers[Manager.Browsers.Count - 1]; 
            // Set the last opened active browser's URL in a global variable
            activeBrowserURL = Manager.Browsers[Manager.Browsers.Count - 1].Url; 
            
            foreach(var br in Manager.Browsers)
            {
                Log.WriteLine(br.ClientId);
            }
            
            // Remove the currently active browser from browsers collection but don't close it
            Manager.RemoveBrowser(activeBrowser.ClientId); 
            
            //Connect to the previous tab by its URL 
            Manager.WaitForNewBrowserConnect("https://docs.telerik.com/teststudio/automated-tests/elements/find-element", true, 15000);
            Manager.ActiveBrowser.WaitUntilReady();
            System.Threading.Thread.Sleep(1500);
            
            // To be used if running the test with extension (for Edge and Chrome) or Firefox for visually switching the ActiveBrowser
            if (Manager.Settings.Web.UseBrowserExtension || ActiveBrowser.BrowserType == BrowserType.FireFox)
            {
                Manager.Desktop.KeyBoard.KeyDown(Keys.Control);
                Manager.Desktop.KeyBoard.KeyPress(Keys.Tab);
                Manager.Desktop.KeyBoard.KeyUp(Keys.Control);
            }
        }

6. Perform the actions in the first opened tab.
7. Use another coded step to switch the active browser back to the second tab. 

Second coded step looks like this: 

public void SwitchBrowserTabs_CodedStep1()
        {
            //add the previously close pop-up back to the collection
            Manager.AddNewBrowser(activeBrowser); 
            Manager.WaitForNewBrowserConnect(activeBrowserURL, true, 15000);
            Manager.ActiveBrowser.WaitUntilReady();
            System.Threading.Thread.Sleep(1500);
            
            // To be used if running the test with extension (for Edge and Chrome) or Firefox for visually switching the ActiveBrowser
            if (Manager.Settings.Web.UseBrowserExtension || ActiveBrowser.BrowserType == BrowserType.FireFox)
            {
                Manager.Desktop.KeyBoard.KeyDown(Keys.Control);
                Manager.Desktop.KeyBoard.KeyPress(Keys.Tab);
                Manager.Desktop.KeyBoard.KeyUp(Keys.Control);
            }

8. Close the new tab and continue working in the first one.

As to your query about the execution for Chrome and Edge in extensionless mode - the tab will not be visually changed, but the actions will be performed. 

I hope this helps.

Regards,
Elena

 

Tags
Chrome
Asked by
Jun
Top achievements
Rank 1
Answers by
Elena
Telerik team
Share this question
or