8 Ağustos 2016 Pazartesi

selenium popup

In Selenium 2(WebDriver), testing popup windows involve switching the driver to the popup window and then running the corresponding actions. Twist recorder records actions in popup windows as commented code.
For eg.
      //Write your logic to locate the appropriate popup before using commented actions.
      //Look at - 'How do I handle popup in WebDriver' section for more details.
      //action in popup "Google". popup.findElement(By.name("q")).sendKeys("Thoughtworks");
      //action in popup "Google". popup.findElement(By.name("btnG")).submit();"
      
The above code is the result of recording in the popup window having google search page, searching for "Thoughtworks".
To get the above code working:

1) Identify the popup,
The following code identifies the popup window with title "Google". Note that the actions tells the title of the popup window. Add the code just before the first popup action being commented.
      String parentWindowHandle = browser.getWindowHandle(); // save the current window handle.
      WebDriver popup = null;
      Iterator<String> windowIterator = browser.getWindowHandles();
      while(windowIterator.hasNext()) { 
        String windowHandle = windowIterator.next(); 
        popup = browser.switchTo().window(windowHandle);
        if (popup.getTitle().equals("Google") {
          break;
        }
      }
      
2) Uncomment code for the same popup,
      //action in popup "Google". 
      popup.findElement(By.name("q")).sendKeys("Thoughtworks");
      //action in popup "Google". 
      popup.findElement(By.name("btnG")).submit();"
      
3) After the popup actions, switch the driver back to the parent window,
      browser.close(); // close the popup.
      browser.switchTo().window(parentWindowHandle);