2 Mart 2016 Çarşamba

selenium popup

package com.webdriver.test;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.*;
public class HandlePopUP {
                   
     private WebDriver driver;
     private String baseUrl;
     @BeforeSuite
     public void setUp() throws Exception {
           driver = new FirefoxDriver();
           baseUrl = “http://rip747.github.io/”;
          driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
      }
     @Test
     public void testHandlePopUp() throws Exception {
          driver.get(baseUrl + “/popupwindow/”);
          driver.findElement(By.linkText(“Example 1”)).click();
          //get main window id before switch to popup
          String currentWindowId = driver.getWindowHandle();
                                        
          //retrive all windows id
          Set allWindows = driver.getWindowHandles();
                   
          //switch all windows on by one
                    for (String windowId : allWindows) {
                          driver.switchTo().window(windowId);
                          String text = “”;
                          try {
                                //get text of popup window on which we want to switch
                                text = driver.findElement(By.xpath(“//a[@title=’Contact Me’]”)).getText();
                             }
                             catch(Exception ex){}
                                                            
                             //check if you get text
                               if(text.equals(“Contact”)){                
                                         //put here your code what you want to perform on popup
                                                            
                                         //close popup
                                         driver.close();
                                         break;
                                }
                               //switch to parent windwo again
                              driver.switchTo().window(currentWindowId);
                    }
      }
     @AfterSuite
      public void tearDown() throws Exception {
                    driver.quit();                                                      
      }
}