Switch browser window/tab using Selenide

Tutorial 6​​ ​​ Switch to​​ child​​ window tab, close child and parent​​ windows​​ in​​ Selenide

What you will Learn:

  • Switch to​​ child​​ window tab

  • close current window

  • switch to parent window

  • close all windows (equivalent to driver.quit in selenium)

  • Code snippet

Switch to​​ child​​ window tab

Launch the site

https://sso.teachable.com/secure/673/identity/sign_up/email​​ ​​ 

Note the title of above window.

Next, click ‘Terms of Use’ seen at the page bottom. Notice that a new tab/window gets opened with a different page title​​ 



Inspect ‘Terms of Use’

Copy the relative xpath and paste it in a variable (line#20).

So basically, we have automated the same manual steps and trying to print the page title at 2 places, lines#18 and 22​​ 

Save and execute.​​ 

Notice that the​​ same page​​ title is printed for both the pages. The reason being, we haven’t yet switched the window before printing page title at line#22​​ 

We can simply use​​ switchTo()​​ method and switch to desired window using its index

Save and execute.​​ 

Notice this time, that the page title for child window gets printed​​ 

 

close current window

Let us try to close the current child down and try to print the page title again (line#29)

Save and execute.​​ 

Notice​​ below that child window closes

Notice the expected exception. Since the child window (or target window) is closed, the title cannot be printed and hence an exception

switch to parent window

To switch to parent window

close all windows (equivalent to driver.quit in selenium)​​ 

Comment switching window code snippet (lines 25-37).​​ So we will be left with parent and child window.

We than use​​ closeWebDriver()​​ method to close all windows

Save and execute.​​ 

Code snippet

package​​ w2a.selenide;

 

import​​ org.openqa.selenium.By;

import​​ org.testng.Assert;

import​​ org.testng.annotations.Test;

import​​ com.codeborne.selenide.CollectionCondition;

import​​ com.codeborne.selenide.Condition;

import​​ com.codeborne.selenide.Configuration;

import​​ static​​ com.codeborne.selenide.Selenide.*;

import​​ static​​ com.codeborne.selenide.Condition.*;

 

public​​ class​​ FirstSelenideTest {

@Test

public​​ void​​ searchTest()​​ throws​​ InterruptedException {

//parent window - index 0

open("https://sso.teachable.com/secure/673/identity/sign_up/email");

 System.out.println(title());

 String​​ tou​​ =​​ "//body/main[1]/div[1]/form[1]/div[4]/div[2]/a[1]";

//child window - index 1

$(By.xpath(tou)).click();

/*

 //switch to child window

 switchTo().window(1);

 System.out.println(title());

 //close child window, equivalent to driver.close in​​ selenium

 closeWindow();

 //System.out.println(title());

 //switch to parent window

 switchTo().window(0);

 System.out.println(title());

 */

//close​​ webdriver​​ equivalent to driver.quit in​​ selenium

closeWebDriver();

 Thread.sleep(5000);

 }

}


Thank you for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 10th Nov 2024