2016-07-19 24 views
0

當從selenium ide生成NUnit代碼時,等待命令(如clickAndWait)使用循環生成一個尷尬模式。Selenium IDE生成的代碼不使用WebDriverWait.Ontil

使用WebDriverWait.until不是更好嗎?

還是我錯了?

更新: 對不起,從內存中寫道,我所指的代碼是waitForElement命令,而不是clickAndWait

這是我指的是代碼:

// waitForElementPresent | id=id |    
for (int second = 0; ; second++) 
{ 
    if (second >= 60) Assert.Fail("timeout"); 
    try 
    { 
     if (IsElementPresent(By.Id("id"))) break; 
    } 
    catch (Exception) 
    { } 
    Thread.Sleep(1000); 
}  

private bool IsElementPresent(By by) 
{ 
    try 
    { 
     driver.FindElement(by); 
     return true; 
    } 
    catch (NoSuchElementException) 
    { 
     return false; 
    } 
} 

閱讀各種指南和其他的答案,在我看來,一個更好的解決辦法是這樣:

// waitForElementPresent | id=id |    
if (!WaitForElementPresent(By.Id("id"))) { Assert.Fail(); } 

private bool WaitForElementPresent(By by) 
{ 
    var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(60)); 
    try 
    { 
     wait.Until(drv => drv.FindElement(by)); 
     return true; 
    } 
    catch (Exception) 
    { 
     return false; 
    } 
} 
+0

你是什麼意思的東西錯誤?你可以expalin更多的示例代碼? –

+0

澄清了這個問題,還有一個錯誤,我指的是clickAndWait類型的命令,而它在waitForElement命令 – SilverXXX

+0

並且是這個問題?我的意思是有任何錯誤? –

回答

0

是,使用WebDriverWait是更好的方法來等待元素存在,但不是創建自己的自定義ExpectedConditions你應該使用硒提供的ExpectedConditions.ElementExists函數來等待,直到元素存在如下: -

var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(60)); 
IWebElement el = wait.Until(ExpectedConditions.ElementExists(by)); 

希望它有幫助.. :)

+0

感謝您提供有關ExpectedConditions的提示,不知道這一點。你知道你是否可以改變標準出口商? – SilverXXX

+0

@SilverXXX對不起..我不明白你在問什麼? –

+0

現在,如果我們在selenium IDE中進行測試,並使用「導出測試用例... - > c#/ NUnit/WebDriver」,則會生成測試方法中具有循環的第一個版本。有沒有辦法改變它,以便第二個生成? (甚至添加新的導出) – SilverXXX