Which collection should be preferred to handle, store and iterate over a large number of window popups using selenium webdriver?

我只是一个虾纸丫 提交于 2019-12-13 02:25:27

问题


I am aware of the fact that we can handle window popups using driver.getWindowHandles() and then store it in a Set and then iterate over it. But what if there are more than 100 window popups?

Consider a scenario where user navigates to a webpage with a link on it. It clicks on this link a new window popup appears and then user click on link on that popup and again another window popup appears and so on..upto 100 or more popups..

Which collection should be preferred in such case? or what approach should be applied in such a case?


回答1:


you can iterate with winHandles.size();:

for(int i = 0; i<= winHandles.size(); i++) {
...
}



回答2:


getWindowHandles()

As per the documentation getWindowHandles() is defined as:

java.util.Set<java.lang.String> getWindowHandles()

Return a set of window handles which can be used to iterate over all open windows of this WebDriver instance by passing them to switchTo().WebDriver.Options.window()

Returns:
    A set of window handles which can be used to iterate over all open windows.

So even dealing with 100 or more popups also you are safe.

A generic example:

  • Code Block:

    import java.util.Set;
    
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class WINDOW_HANDLE_ITERATE_Set_demo 
    {
        public static void main(String[] args) throws Exception 
        {
            System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
            WebDriver driver =  new FirefoxDriver();
            driver.get("http://www.google.com");
            String parent_window = driver.getWindowHandle();
            System.out.println("Page Title is: "+driver.getTitle());
            ((JavascriptExecutor) driver).executeScript("window.open('http://facebook.com/');");
            new WebDriverWait(driver,10).until(ExpectedConditions.numberOfWindowsToBe(2));
            Set<String> allWindows_1 = driver.getWindowHandles();
            for(String hand1:allWindows_1)
            if(!parent_window.equals(hand1))
            {
                driver.switchTo().window(hand1);
                new WebDriverWait(driver,10).until(ExpectedConditions.titleContains("Face"));
                System.out.println("Page Title is: "+driver.getTitle());
                driver.close();
            }
            driver.switchTo().window(parent_window);
            System.out.println("Page Title is: "+driver.getTitle());
            driver.quit();
        }
    }
    
  • Console Output:

    Page Title is : Google
    Page Title is : Facebook – log in or sign up
    Page Title is : Google
    

Alternative

However to navigate through 100 or more popups you can also cast the Set<String> object into a ArrayList<String> object as follows:

  • Code Block:

    import java.util.ArrayList;
    import java.util.Set;
    
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class WINDOW_HANDLE_ITERATE_ArrayList_demo
    {
        public static void main(String[] args) throws Exception 
        {
            System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
            WebDriver driver =  new FirefoxDriver();
            driver.get("http://www.google.com");
            System.out.println("Page Title is : "+driver.getTitle());
            String parent_window = driver.getWindowHandle();
            ((JavascriptExecutor) driver).executeScript("window.open('http://facebook.com/');");
            WebDriverWait wait = new WebDriverWait(driver,5);
            wait.until(ExpectedConditions.numberOfWindowsToBe(2));
            Set<String> allWindows_1 = driver.getWindowHandles(); // this is the casting step
            ArrayList<String> tabs = new  ArrayList<>(allWindows_1);
            driver.switchTo().window(tabs.get(1));
            wait.until(ExpectedConditions.titleContains("Facebook"));
            System.out.println("Page Title is : "+driver.getTitle());
            driver.close();
            driver.switchTo().window(parent_window);
            System.out.println("Page Title is : "+driver.getTitle());
            driver.quit();
        }
    }
    
  • Console Output:

    Page Title is : Google
    Page Title is : Facebook – log in or sign up
    Page Title is : Google
    

trivia (List vs Set)

List and Set are the two among several Collection classes in Java. Both of them are used to store objects and provides convenient APIs to insert, remove and retrieve elements to support Iteration over Collection.

  • List in Java allows duplicates while Set doesn't allow any duplicate. If you insert duplicate in Set it will replace the older value. Any implementation of Set in Java will only contains unique elements.
  • List is an Ordered Collection while Set is an unordered Collection. List maintains insertion order of elements, means any element which is inserted before will go on lower index than any element which is inserted after. Set in Java doesn't maintain any order. Though Set provide another alternative called SortedSet which can store Set elements in specific Sorting order defined by comparable and comparator methods of Objects stored in Set.
  • Popular implementation of List interface in Java includes ArrayList, Vector and LinkedList. While popular implementation of Set interface includes HashSet, TreeSet and LinkedHashSet.

tl; dr

Here you can find a reference about Difference between List and Set in Java Collection



来源:https://stackoverflow.com/questions/52291219/which-collection-should-be-preferred-to-handle-store-and-iterate-over-a-large-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!