5

I am using Selenium 2.x WebDriver for the automation of a java project. While the automation proceeds it reaches a page in which, when a submit button is clicked a "pop up window" appears and the automation cannot be proceeded.

Code

public void writeSite(WebDriver driver, ZoneTest zone) throws BiffException, InterruptedException, IOException

    //Creates a new zone for testing

    General.elementClick_xpath(driver, Locators.siteMenuDropBoxXpath);
    General.waitToLoad(General.WAIT_MIN);
    General.elementClick_xpath(driver, Locators.viewSitesButtonXpath);
    General.elementClick_xpath(driver, Locators.viewDataPointDetailsXpath);
    General.waitToLoad(General.WAIT_AVG);

    General.elementClick_xpath(driver, Locators.addZoneXpath);

    General.waitToLoad(General.WAIT_AVG);

    General.inputTextFieldEnter_Id(driver, "name", zone.zoneName);
    General.inputTextFieldEnter_Id(driver, "description",zone.zoneDescription );
    General.inputTextFieldEnter_Id(driver, "urlExtension", zone.urlExtension);
    General.inputTextFieldEnter_Id(driver, "timeSpentThreshold", zone.thresholdTime);
    General.inputTextFieldEnter_Id(driver, "tuningNumber", zone.tuningNumber);

   **General.elementClick_xpath(driver, Locators.createZoneSubmitXpath);**

   //Here a new pop up window apppears. And the following codes 3 lines doesnt work.

    General.inputTextFieldEnter_Id(driver, "active", zone.act);
    General.inputTextFieldEnter_Id(driver, "userid", zone.uid);
    General.elementClick_xpath(driver, Locators.SubmitXpath)
}


public class General 
{
  public static final long WAIT_MICRO = 500;
  public static final long WAIT_MIN = 2000;
  public static final long WAIT_AVG = 5000;
  public static final long WAIT_MAX = 5500;
  public static String baseUrl ="";


  //Method to wait  
  public static void waitToLoad(long milliSeconds) throws InterruptedException {
     Thread.sleep(milliSeconds);
  }

     // Method to load the Url taken from the config.property file

  public static void loadBaseUrl(WebDriver driver){
     baseUrl = PropertyUtility.getProperty("Baseurl");
     driver.get(baseUrl);
     driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
  }

  public static void inputTextFieldEnter_xpath(WebDriver driver, String LocatorId, String ValueToBeEntered) throws InterruptedException {       
     System.out.println("Log in user name_inputTextField_outside if: "+ ValueToBeEntered);
     WebElement inputTextField = driver.findElement(By.xpath(LocatorId));
     General.waitUntilElementVisible(driver, LocatorId);
     inputFieldClear_xpath(driver,LocatorId);
     inputTextField.sendKeys(ValueToBeEntered);     
}

How can I switch the view to new pop up window?

Krishnalal P
  • 487
  • 4
  • 20
Akhil Suresh
  • 376
  • 3
  • 15

3 Answers3

8

If you want to do any operations in pop-up window, here is WebDriver logic to select Pop-up window.

driver.switchTo().window("<window name>");
Krishnalal P
  • 487
  • 4
  • 20
1

If any pop-up Window arises,then you've to switch to that window.Because it will not recognize new window automatically unless you mention that window name.

syntax: set ws=driver.get windowshandles();

    Iterator itr=ws.iterate();

    String W1=(String)itr.next();

    String W2=(String)itr.next();

driver.switchTo().window(W1);

(or)

driver.switchTo().window(W2);

(or) ......

dollar
  • 3
  • 1
  • 1
  • 5
0

You need to switch that window first then do operation what ever you want. You have to use below command for switch window.

driver.switchTo().window("Window ID");

You can use below command for getting all opened windows unique ID.

Set<String> windows = driver.getWindowHandles();

In your case, Please use below code for switch window.

String vBaseWindowHandle = driver.getWindowHandle();
    Set<String> windows = driver.getWindowHandles();

    for(String temp : windows)
    {
        driver.switchTo().window(temp);
    }

    // Do code for new window

    driver.close();     
    driver.switchTo().window(vBaseWindowHandle);

Fore more information related to how to handle windows and popup please go to below URL https://trickyautomationworld.blogspot.in/2018/03/how-to-handle-windows-in-selenium.html https://trickyautomationworld.blogspot.in/2018/03/how-to-handle-popup-in-selenium.html