Why we only using Set collection to store no of windows

864 views
Skip to first unread message

Balaji Singh .Y

unread,
Jun 23, 2016, 12:07:56 PM6/23/16
to seleniu...@googlegroups.com
Hi All,

Why we are using the Set Collection for storing number of child  windows ? And why  don`t  we use any other collection ?


public class Chilld_Window_Close_Using_Set {

/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
driver.get("http://rediffmail.com");

//get the parent window handle name 
String parentHandle = driver.getWindowHandle();
System.out.println("Parent Window :" + parentHandle);
// get all the window handles and assign it to a set
// It will include child window and parentwindow
Set<String> windowHandles = driver.getWindowHandles();
System.out.println("No of Window Handles  or currently opened browsers: " + windowHandles.size());
     
// remove the parent handle from the set
windowHandles.remove(parentHandle);

//Because we removed only the parent handle now only child handles will be thier in 
// the set
// Now iterate through the child window and close the child window
for (String winHandle : windowHandles) {
driver.switchTo().window(winHandle);
System.out.println("Child Handle : " + winHandle);
driver.close();
}
        
// switching back to parent window
driver.switchTo().window(parentHandle);
//closing the parent browser
System.out.println("Closing the parent Browser :");
driver.close();

}

}

With Regards, 
BALAJI SINGH .Y

Shameem Akhtar

unread,
Jun 23, 2016, 12:41:27 PM6/23/16
to seleniu...@googlegroups.com
Hi Balaji,

We use Set collection for storing window ids because Set doesn't allow duplicate objects. Since window ids are always unique for each window, we use Set to store them.

Hope it helps..

Regards,
Shameem
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/CAC1Dipf3d78hv4bqrWK5hx5T%2BSH_UpBQVZnMYUKPP4jAoLwUyg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


--
Best Regards,

Shameem Akhtar

Mobile # +917666339589

Balaji Singh .Y

unread,
Jun 23, 2016, 1:00:09 PM6/23/16
to seleniu...@googlegroups.com
Hi Shameem,

As we all know that Windows ID`s will be unique then why we have to use set, we can go with List only right ?

I am still not aware why everybody uses set .

With Regards,
Balaji Singh


For more options, visit https://groups.google.com/d/optout.



--
BALAJI SINGH .Y

⇜Krishnan Mahadevan⇝

unread,
Jun 27, 2016, 1:38:12 AM6/27/16
to Selenium Users
Balaji,

As we all know that Windows ID`s will be unique then why we have to use set, we can go with List only right ?
I am still not aware why everybody uses set .

What do you mean by this ? Its not a choice that the Selenium library consumers make. The WebDriver API contract itself has been written such that it returns back a Set of Window Handles. So you adhere to the contract and consume the window handles as a set.


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

Balaji Singh .Y

unread,
Jun 27, 2016, 11:04:16 AM6/27/16
to seleniu...@googlegroups.com
Got it Krishnan. Thanks a lot for your advice on this.


For more options, visit https://groups.google.com/d/optout.



--
BALAJI SINGH .Y
Reply all
Reply to author
Forward
0 new messages