<<Previous Post << Complete Tutorial>> Next Post>>
WebDriverIO – Switching To Tabs in WebDriverIO
switchWindow() is the command of WebDriverIO by which we can switch focus to a particular tab/window.
Let’s go through this article to understand it in better way through a demonstration with a small exercise.
Pre-requisite: Have ‘Visual Studio Code’ IDE installed in your machine (Click here to install if not installed in your machine)
Here is the small Exercise to Switch Tabs using switchWindow() command of WebDriverIO.
Using SwitchWindow() Command
First, I will demonstrate the exercise where we need to click on a link to open a new tab and then switch our focus to newly opened tab by switchWindow() command of WebDriverIO, followed by practically demonstrating the WebDriverIO Automation code for achieving it.
Exercise:-
1) Navigate to http://omayo.blogspot.com/
2) Find Xpath of the hyperlink which will open the URL in New tab.
3) Switch focus to newly opened tab i.e., on Selenium143.blogspot.com
Practical Demonstration:
Following are the Steps to Perform above Exercise (i.e. to click on a link to open a new tab and then switch our focus to newly opened tab by switchWindow() command of WebDriverIO) using WebDriverIO Automation code in ‘Visual Studio Code’ IDE:
Pre-requisite: Download the Project from here (Click here to Download) and Unzip it.
1) Open Visual Studio Code on your system
2) Click on File and Select Open Folder. Select the project folder on which your workspace is present.
3) Expand ‘test’ from the Explorer, right click on the displayed ‘specs’ and select ‘New File’ option as shown below:
4) Give any name of the newly created file say “Switching_To_Tabs.test.js” and copy the below given code into the newly created file as shown below and save the file:
The following is the WebDriverIO sample code for switch our focus to newly opened tab by using switchWindow() command of WebDriverIO on ‘Omayo.blogspot.com’ application:
You can understand this code, by reading the comments provided in the below code:
Copy the code from here :-
describe('Switching to tabs in WebDriverIO',() => { it('should Switch tabs using switchWindow Command ',() => { browser.url('https://omayo.blogspot.com') $("//*[@id='selenium143']").click() //xpath of the hyperlink on webpage browser.pause(3000); browser.switchWindow('http://selenium143.blogspot.com/') }); });
5) Click on ‘wdio.conf.js’ to open in the IDE, and make sure the Test file name is updated properly in this file as shown below and save it:
6) Click on Terminal and Open New Terminal.
7) By Default, Powershell Terminal will open as shown below:
8) Run geckodriver.exe in the Powershell terminal as shown below to run Geckodriver in background.
9) Open Another Terminal in the same way we did in the step 6 and run below command in terminal
.\node_modules\.bin\wdio .\wdio.conf.js
Observe that the ‘Switching_To_Tabs.test.js’ will get executed successfully, the Firefox Browser will launch and then Iframe was searched and respective action will be taken by WebDriverIO Automation code.
And also, the result will be displayed as ‘Passed’ in the terminal as shown below:
This is how, we can take help of switchWindow() Command of WebDriverIO to switch focus to particular tab/window. I will explain other other commands to perform different operations on WebPages using WebDriverIO in the upcoming Articles .
Next Steps:
> To learn more about WebDriverIO, continue to the next post (Click on Next Post link below)
> Check complete WebDriverIO Tutorial Contents here (Click here)
Please leave your questions/comments/feedback below.
Happy Learning ?
Connect to me on LinkedIn (Click here)
On a mission to contribute to the Software Testing Community in all possible ways.
<<Previous Post << Complete Tutorial>> Next Post>>
working with multiple windows like in selenium we had switchtodefault or context window?
Any link to the question please.
Hi Parul,
Please find Article :- https://www.qafox.com/webdriverio-handling-multiple-windows-in-webdriverio/
I believe this article should resolved your asked query.
Thanks & Regards,
Ajay Lunia
thanks Ajay.
that looks like an alternative to the direct solution which is provided by switchtoDefaultwindow or context window which directly takes you to the parent window, here we are using loops to match to the parent window using locator right?
Yes Parul. This is one way of achieving the feat. But i am not able to find if there is any parent Window command or if there is any other function which is making us to reach directly to parent Window. for the time being we can use it.
Hope you liked this alternate solution.
Thanks & Regards,
Ajay Lunia
Hi, could you please help that I tried but it didn’t work? The parent link likes: https://abc.com/start/ and the new tab link likes https://abc.com/. Thanks in advance.
@Thuytien :- Hope i was able to resolve your query dedicatedly and on time 🙂
Yes, thank you so much. You’re the best supported I’ve ever known. I deeply appreciate you help.
Thanks a lot Thuytien for your Kind words and compliments 🙂