Search
 
 

Display results as :
 


Rechercher Advanced Search

Latest topics
» FlexMonkium Download Not available under Gorillalogic site
Thu Dec 08, 2016 5:40 pm by bhanu.praveen

» Selenium Script issue
Tue Dec 06, 2016 9:48 pm by njain213

» Difference between QTP, Selenium and Silk test
Tue Dec 06, 2016 7:58 pm by jessicacyrus1

» Where does selenium script pick browser from
Tue Dec 06, 2016 1:52 pm by janbaskt

» Safari throws an HTTP ERROR: 404
Tue Dec 06, 2016 1:51 pm by janbaskt

» Access Denied Error in Selenium RC
Tue Dec 06, 2016 1:51 pm by janbaskt

» Automating Flex application through selenium webdriver
Fri Dec 02, 2016 2:06 pm by abhinay.krs

» Starting with Selenium, what is Selenium?
Mon Nov 28, 2016 4:00 pm by Foma

» Getting started with Selenium
Mon Nov 28, 2016 3:23 pm by Rebbelx

Navigation
 Portal
 Index
 Memberlist
 Profile
 FAQ
 Search

How to switch window in webdriver?

View previous topic View next topic Go down

How to switch window in webdriver?

Post by sadika on 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

sadika

Posts : 1
Join date : 2012-07-05

Back to top Go down

Re: How to switch window in webdriver?

Post by Sagot on 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


Sagot

Posts : 4
Join date : 2012-07-05

Back to top Go down

Re: How to switch window in webdriver?

Post by popsha on 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

popsha
Amateur
Amateur

Posts : 60
Join date : 2012-04-14

Back to top Go down

Re: How to switch window in webdriver?

Post by Sagot on 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);

Sagot

Posts : 4
Join date : 2012-07-05

Back to top Go down

Re: How to switch window in webdriver?

Post by Sponsored content Today at 5:49 pm


Sponsored content


Back to top Go down

View previous topic View next topic Back to top

- Similar topics

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