2017-08-01 82 views
0

我試圖在IE上使用硒web驅動程序切換到彈出窗口。當我到達切換到彈出窗口的行時,代碼就會掛起。我正在尋找可以嘗試切換窗口10次的方法,並在20秒後嘗試另一次嘗試,因爲我試圖在下面做或者更好的方法來確保窗口正確切換。如果我手動關閉彈出窗口,我會得到noSuchWindow異常並且代碼被炸出來。在發佈之前,我已經回顧了其他的stackoverflow文章,但我相信我的問題是獨一無二的。這是我的情景:IE中的Selenium SwitchWindow問題

Scenario: 
    1. Retrieve parent window handle 
    2. Perform action launching popup window 
    3. Get the popup window handles and store them in a string set 
    4. Loop through window handles until there are no more. Retrieve Popup window handle 
    5. Loop until the popup window does not match the parent windows handle and if 20 seconds has passed 
    6. Switch to popup ---Code Hangs Here--- 
    7. Retrieve popup title 
    8. Close popup 
    9. Switch to parent 
    10. Verification of title 

下面是所有相關的代碼出現上述情況:

String popupWindow = ""; 
    String parentWindow = ""; 
    int saveCount = 0; 
    int getPopupCount = 0; 
    int tryCount = 0; 

    // Get Parent window handle 
    parentWindow = driver.getWindowHandle(); 
    System.out.println("parentWindow: " + parentWindow); 
    Thread.sleep(500); 

    //Perform Action launching popup window 

    //Get the popup window handles and store them in a string set 
    Set<String> popups = driver.getWindowHandles(); 
    saveCount = getPopupCount; 
    try { 
     tryCount = 0; 
     //Loop until the popup count does not equal the save count or until 10 tries (20 seconds) have passed 
     while (saveCount == getPopupCount && tryCount++ < 10) { 
      //Wait 2 second 
      Thread.sleep(2000); 

      getPopupCount = popups.size(); 
      System.out.println("getPopupCount: -" + getPopupCount + "-"); 
     }//end while 
     if (tryCount >= 10) { 
      System.out.println("Failed after 10 tries"); 
     }//end if 

     //Loop through window handles until there are no more. Retrieve Popup window handle 
     Iterator<String> myIterator = popups.iterator(); 
     while (myIterator.hasNext()) { 
      popupWindow = myIterator.next(); 
      System.out.println("popupWindow: " + popupWindow); 
      System.out.println("Boolean should be false: " + parentWindow.equalsIgnoreCase(popupWindow)); 
      Thread.sleep(5000); 

      //fetch starting time 
      long startTime = System.currentTimeMillis(); 

      //Loop until the popup window does not match the parent windows handle and if 20 seconds has passed 
      while(!parentWindow.equalsIgnoreCase(popupWindow) && (System.currentTimeMillis()-startTime) < 20000) { 
       try{ 
        Thread.sleep(500); 

        //Switch to the Popup window    
        //TODO - This is where it fails 
        driver.switchTo().window(popupWindow); 
        Thread.sleep(500); 
        System.out.println(driver.getTitle()); 
        popupTitle = driver.getTitle(); 

        //Close the Popup Window 
        driver.close(); 
       } catch (Exception e) { 
        throw new RuntimeException(Thread.currentThread().getStackTrace()[1].getMethodName() 
          + "...Error: " + e.getMessage()); 
       }//end catch 
      }//end if 
     }//end while 
    } catch(Exception e) { 
     throw new RuntimeException(Thread.currentThread().getStackTrace()[1].getMethodName() 
       + "...Error switching to and closing popup: " + e.getMessage()); 
    }//end catch 

    //Switch to parent window. 
    driver.switchTo().window(parentWindow); 
    driver.manage().window().maximize(); 
    Thread.sleep(2000); 

    //Verification of title 
    Assert.assertTrue(popupTitle.contains("MYTITLE")); 

CI信息:

JDK:1.8.0_66

的Java:第8版

IE:11

其他類似的問題是沒有回答我的問題:

switchWindow does not work in IE

How to switch to the new browser window, which opens after click on the button?

How to exit a while loop after a certain time?

任何幫助或反饋是非常感謝!

+0

如果它是公共的,你能找到我們的URL嗎?謝謝 – DebanjanB

+0

這是一個私人網址。我可以去找一個有類似彈出窗口的網站,並在那裏進行測試,如果有幫助,可以回報一下。 –

+0

如果您可以分享類似的網址,那就太好了。謝謝 – DebanjanB

回答

0

使用下面的代碼我測試了我的私人代碼和http://demo.guru99.com/popup.php演示彈出網站。我的代碼對該網站運行良好,但在我的私人網站上失敗。我正在實施等待期,但我不相信這是一個計時問題。我只是相信彈出窗口與我的私人網站上的Selenium在IE上不兼容。將我的代碼發佈在虛擬網站上作爲答案,以防其他人遇到與代碼有效相似的問題。

//Retrieve parent window handle 
    parentWindow = driver.getWindowHandle();   

    //Loop through the window handles until you are on the popup window 
    for(String popupWindow : driver.getWindowHandles()){ 
     if (driver.switchTo().window(popupWindow).getTitle().equals(myTitle)) { 
     break; 
     } 
     else { 
      driver.switchTo().window(parentWindow); 
     } 
    } 

    //Store the title 
    popupTitle = driver.getTitle(); 

    //Close the Popup Window 
    driver.close();  

    //switch to parent window. 
    driver.switchTo().window(parentWindow); 
    driver.manage().window().maximize(); 

    //Verification of title 
    Assert.assertTrue(popupTitle.toUpperCase().contains(myTitle)); 
+0

我們可以至少得到你的私人網站彈出窗口的截圖嗎? – silver