Pages

Wednesday, November 27, 2013

How to switch and Handle the pop-up window ?





If you want to do any operations in pop-up window you need to switch the control to pop-up window then do all your operations in that and finally close the pop-up window and again select the default (main ) window.

here is WebDriver logic to select Pop-up window

1 . Pop-up window has name/id

     driver.switchTo().window("<window name>");




2. Pop-up window doesn't have name / you don't want to hard code the window name then go for below logic.


Method-1

  • before opening pop-up get the main window handle.
             String mainWindowHandle=driver.getWindowHandle();
  • open the pop-up (click on element which causes open a new window)
             webElement.click();
  • try to get all available open window handles with below command. (the below command returns all window handles as Set)
            Set s = driver.getWindowHandles();
  • from that above set try get newly opened window and switch the control to that (pop-up window handle), as we already know the mainWindowHandle.
          Set s = driver.getWindowHandles();
Iterator ite = s.iterator();
while(ite.hasNext())
            {
String popupHandle=ite.next().toString();
if(!popupHandle.contains(mainWindowHandle))
{
driver.switchTo().window(popupHandle);
}
}
  • Now control is in pop-up window, do all your operations in pop-up and close the pop-up window.
  • Select the default window again.
                 driver.switchTo().window( mainWindowHandle );


Here is an example to switch to the new window:

          String handle = driver.getWindowHandles().toArray()[1];

Method-2

// get  the last window handles before the popup window appears.Which was opened as the last window

driver.switchTo().window(driver.getWindowHandles().iterator().next());


Method-3

// Switch to window by using Page URL

public static void switchToWindow(String objTitle) 
{
 
 String currentWindow=driver.getWindowHandle();
 
 try
 {
 
  Set<String> AllHandle = driver.getWindowHandles();
        for (String han : AllHandle)
        {
              driver.switchTo().window(han);
              try 
              {
                    Thread.sleep(2000);
              } 
              catch (InterruptedException e) 
              {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
              }
              String getTitle = driver.getCurrentUrl();
              if (getTitle.contains(objTitle))
              {
                 System.out.println("Switched to window..."+objTitle);
                 break;
              }
               
        }
  
 }
 catch (Exception e) {
  // TODO: handle exception
 }
  
}



Method-4

// get all the window handles before the popup window appears
 Set beforePopup = driver.getWindowHandles();
     
// click the link which creates the popup window
driver.findElement(by).click();
  
// get all the window handles after the popup window appears
Set afterPopup = driver.getWindowHandles();
     
// remove all the handles from before the popup window appears
afterPopup.removeAll(beforePopup);

// there should be only one window handle left
if(afterPopup.size() == 1) {
          driver.switchTo().window((String)afterPopup.toArray()[0]);
 }


Methods for Handle

// Using JavaScript how to handle the pop with ok and Cancel

( ( JavascriptExecutor ) _driver )
            .executeScript( "window.onbeforeunload = function(e){};" );

You might try using keyboard events. So once the window pops up:

Tab onto the "Ok" button.
//Using Keyboard controls .

driver.Keyboard.PressKey(Keys.Tab);
You'll have to play around to see how many tab presses are required.

Then hit space.

driver.Keyboard.PressKey(Keys.Space);

Yeah it's sort of a hack, but WebDriver is still pretty immature.

//Using Alert

IAlert alert = driver.SwitchTo().Alert();

        alert.Accept(); //for two buttons, choose the affirmative one
        // or
        alert.Dismiss();// to cancel the affirmative decision, i.e., pop up will be dismissed and no action will take place

  

5 comments:

  1. denizen by levi’s men’s dresses,denizen levi’s men’s dresses,donate,dresses collection for summer,man dresses,men’s dresses collection,men’s dresses collection fashion,khussa,khussa indian shoes,khussa mahal,khussa shoe,khussa shoes,khussa trends collection,khussa wedding shoes,khussas for fashionable women,ladies khussa,mens khussa,mens khussa shoes,pakistani khussa,collection dress,dresses in fashion,glitter designs,londan men fashion,lookbook,men dresses style collection,paris fashion dress,street look,stylish men wear,trendy ideas dress,uk trendy men wear,watches fashion,14 august fabulous nails art designs,14 august nails,all colour nails,attractive,beautify nails,chocolate,embellish nails,fresh design,graceful,green,independance day,nails art designs collection,paint nails,stones nails,ankle band tattoos,ankle bracelet tattoos,ankle tattoos designs newest collection,ankle tattoos for women,butterfly ankle tattoos,butterfly tattoos on ankle,cool ankle tattoos,cute ankle tattoos,flower ankle tattoos,tattoos designs,tattoos on ankle,girls wear collection,gul ahmed midsummer arrivals,jashn-e-azaadi wear dresses,latest collection by gul ahmed,latest jashn-e-azaadi dresses,long shirt dresses,midsummer arrivals,newest collection by gul ahmed,seasonal dresses,styo dresses,women stylish jashn-e-azaadi wear dresses,women wear collection,autumn polyvore,best clothing combos,events polyvore combos,formal wear polyvore,how to dress up for office,polyvore combos autumn collection to try on events,polyvore combos autumn fashion,try on events polyvore combos,amazing nail art,awesome nail art designs,beautiful nail art,floral nail art for ladies,insurance,nail art,nails,stylish floral nail art,bridal shower,bridal shower cakes,bridal shower decorations,bridal shower favors,bridal shower games,bridal shower gift ideas,bridal shower gifts,bridal shower ideas,bridal shower ideas collection,bridal shower invitations,bridal shower invites,bridal shower themes,bnb trendy and stylish handbag,fashion of handbags,girls handbags,handbag,handbag designs fashion,handbag designs fashion for womenattorney,claim,classy pakistani,credit,degree,donate,dresses,hosting,insurance,lawyer,lehenga choli,loans,mortgage,salwar kameez,suits design,unstitched pattern,women dresses,women wear,beautiful jewellary collection 2015,rings collection 2015,rings collection 2015 for men & women,wedding flowers and rings,wedding flowers and rings collection,wedding flowers and rings collection 2015 jewellary,wedding rings collection 2015,motifz,motifz dresses 2015,spring collection 2015,spring dresses,elegant lehenga cholimlehenga choli,lehenga,lehenga choli fashion 2015,party wear elegant lehenga choli,tagged bridal wear elegant lehenga choli

    ReplyDelete