java
  • selenium
  • xpath
  • selenium-webdriver
  • 2014-02-28 58 views 2 likes 
    2

    我的時區下午好。不穩定的測試Selenium

    我開始使用Selenium來測試我的Web應用程序。我正在使用WebDriver API和IEDriverServer.exe。操作系統 - > Windows XP 主要問題是測試不穩定。有時運行,有時會拋出異常。例如,這是測試不穩定的常見地點。 我必須打開一個新窗口並開始履行一些領域。

    driver.findElement(By.xpath("//input[@name='"+button+"' and @type='button']")).click();//BUTTON THAT OPENS THE NEW WINDOW 
            long initDate = System.currentTimeMillis(); 
            while(driver.getWindowHandles().size() <= numberPopUps){ 
             Thread.sleep(500); 
             //15 seconds waiting for the pop-up 
             if((System.currentTimeMillis() - initDate) > 15000){ 
              throw new Exception("Timeout to open popup"); 
             } 
            } 
         for(String winHandle : driver.getWindowHandles()){ 
            if(!winHandle.equals(mWindow)){ 
             driver.switchTo().window(winHandle); 
             break; 
            } 
           } 
         driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);//WAIT THAT THE PAGE COMPLETELY LOAD  
    
        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@name='descricaoMov']")));//VERIFY IF THIS INPUT IS ON THE DOM 
    
    `driver.findElement(By.xpath("//input[@name='descricaoMov']")).sendKeys("TESTE SELENIUM");`//This is where sometimes the test throws exception saying that is unable to find this element 
    

    ,我想問這怎麼可能?

    預先感謝 問候

    +2

    有幾件事情映入腦海。你的while循環只是複製'WebDriverWait' /'FluentWait'中的代碼 - 它被設計用於在這種情況下「等待」。其次,什麼版本的IE?第三是什麼版本的IE驅動程序?另外,你會得到什麼錯誤?每次都是一樣的嗎?如果不是,那是什麼?它多久失敗一次?每2次運行?每10次運行?最後,你有沒有正確設置你的保護模式設置? – Arran

    +0

    版本的IE 8 IE驅動程序版本2.39 錯誤時發生的總是相同的「WebElement‘輸入[@名稱=’descricaoMov‘’」沒有找到 我沒有因爲IEDriver頁面配置任何保護模式設置規範沒有說任何與Windows XP相關的任何內容僅針對高級版本 – tt0686

    回答

    2

    你在這裏重複你的努力。 wait.until行完全符合.sendKeys()的異常。試試這個:

    WebElement descriaoMov = new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@name='descricaoMov']"))); 
    descriaoMov.sendKeys("TEST SELENIUM"); 
    

    另外,CSS選擇器比XPath更適合查找元素。我建議改變了XPath的部分上面:

    By.cssSelector("input[name='descriaoMov']") 
    
    +1

    請記住,僅僅因爲某個元素可能位於DOM中,它可能無法始終與之交互。如果您的測試繼續進行之後才能實際將密鑰發送給元素,則也會引發錯誤。您可以添加另一個等待,直到元素可見,而不是隻加載到DOM中。另一種方法是Thread.Sleep(xx),但我會讓它更加動態。 – Ben

    相關問題