Selenium Forum: Functional And Regression Testing Tool.
Would you like to react to this message? Create an account in a few clicks or log in to continue.
Search
Display results as :
Advanced Search
Latest topics
AEM Training | Free Online DemoWed Apr 21, 2021 5:45 pmazharuddin
c# PageFactory - issue initializing elementsFri Nov 01, 2019 8:40 pmthegoatboy
Selenium making automatic connection to random urlsMon Jul 08, 2019 12:58 pmrepairtechsolutions1
How can we design the custom framework in Selenium RCMon Jun 24, 2019 2:26 pmrandybonnettes
What are the new features in Selenium 3.0Tue Jun 18, 2019 5:37 pmpappyvicky
What are you using Selenium for? Fri Apr 12, 2019 3:52 amzhl
LIMITATIONS OF SELENIUMWed Apr 10, 2019 11:23 amswara
Navigation
 Portal
 Index
 Memberlist
 Profile
 FAQ
 Search

Go down
avatar
sadika
Posts : 1
Join date : 2012-07-05

How to switch window in webdriver? Empty How to switch window in webdriver?

Thu Jul 05, 2012 3:49 pm
Can any one elaborate how to switch window in webdriver using C#. we
have tried driver.SwitchTo().Window("titleName") but my window Title is
same as parent window. so it switches to parent window besides another
window.

Thanks
Sadik Ali
avatar
Sagot
Posts : 4
Join date : 2012-07-05

How to switch window in webdriver? Empty Re: How to switch window in webdriver?

Thu Jul 05, 2012 9:52 pm
Hi, a few days ago I also faced that problem, and here I'll tell you how to handle it:

Issue:
The PoP Up window occurs or simply we get a new window (or a tab) after some action (click a link for example) and we want to focus our WebDriver driver to keep on testing. I'm using Selenium 2.0 WebDriver with Java - but C# is very similar especialy Selenium.

What we gonna do:
We use method driver.getWindowHandles() to get all id's of all windows' that are currently opened by WebDriver (if you have opened some other browser windows it's ok, it see only opened by driver).
We save list of id's of opened windows BEFORE some action (click on a link for example) and AFTER and then we compare this lists. If after we click there will be one more window id - then we understand that a new window opened and so we want to focus on it.

Here is the code how to implement it:


Code:
...
public clickTheLinkAndFocusOnANewWindow(WebDriver driver) {

  saveOldHandles(driver); // here we use method to save all window id before click (the code I'll show further)
 
  driver.findElement(By.id("idofbutton")).click(); // here we find our button and click on it

  saveNewHandles(driver); // here we save all window id after click (the code I'll show further)

ifNewWindowOccursFocusOnIt(driver); // this method selects and focuses on a new window if it really occurs, and if not it won't do anything (because sometimes we expect opening in new window but it opens in current window) code I'll show further
   
}

Here is the code for our helper methods, if you will use this many times I suggest you to put these helper methods in super class (parent class) so you can use it in any classes.

Code:


//here we make a fields where id's will store
public static Set setOfOldHandles = null;
public static Set setOfNewHandles = null;

// first we clear (just in case) our set. Empty set can't be cleared so we put an "if" statement.
public static void saveOldHandles(WebDriver driver) {
        if (setOfOldHandles != null) {
            setOfOldHandles.clear();
        }
        setOfOldHandles = driver.getWindowHandles(); // here we save id's of windows
    }


// just the same for new set of id's

public static void saveNewHandles(WebDriver driver) {
        if (setOfNewHandles != null) {
            setOfNewHandles.clear();
        }
        setOfNewHandles = driver.getWindowHandles();
    }

// here is the code of method which decides whether to focus on a new window or not

public static void ifNewWindowOccursFocusOnIt(WebDriver driver){
      if (setOfNewHandles != null) {
          setOfNewHandles.removeAll(setOfOldHandles); // this method removeAll() take one set and puts it in another set and if there are same positions it will erase them and leaves only that are not equals
        }
        else {

      System.out.println("setOfNewHandles is null. Can't compare old
and new handles. New handle may have not enough time to load and
save. Maybe you should add some time to load new window by adding Thread.Sleep(3000); - wait for 3 second ");
      }

      if (!setOfNewHandles.isEmpty()) {
            String newWindowHandle = setOfNewHandles.iterator().next(); // here IF we have new window it will shift on it
          driver.switchTo().window(newWindowHandle);
        }       
    }

I hope it will help you. It works on IE and FireFox perfectly

avatar
popsha
Amateur
Amateur
Posts : 60
Join date : 2012-04-14

How to switch window in webdriver? Empty Re: How to switch window in webdriver?

Mon Jul 09, 2012 1:11 pm
hi Sagot,

your code works perfectly.
but cannot find a way to switch to main window since we implement remove all function.
can you suggest a solution for this
avatar
Sagot
Posts : 4
Join date : 2012-07-05

How to switch window in webdriver? Empty Re: How to switch window in webdriver?

Mon Jul 30, 2012 5:19 pm
popsha wrote:hi Sagot,

your code works perfectly.
but cannot find a way to switch to main window since we implement remove all function.
can you suggest a solution for this

To return focus to the main window there are 2 ways:

Complicated:

The idea is to make a set of all windows' and to get an id of the current window (current window now is our poped out window)
so our main window is the set of all windows' minus current id.

as you already know: setOfNewHandles = driver.getWindowHandles(); - we get a set of all windows' opened now
currWin = driver.getWindowHandle(); - is to get only the current window.

And then we use
setOfNewHandles.remove(currWin); - to remove one object from the set, what will remain - would be the main window.

Simpler way:
just before some actions save id of the main window
String mainWinId = driver.getWindowHandle();

and then to return to our main window use: driver.switchTo().window(mainWinId);
Sponsored content

How to switch window in webdriver? Empty Re: How to switch window in webdriver?

Back to top
Permissions in this forum:
You cannot reply to topics in this forum