2012-10-19 6 views
7

Sto usando i test del selenio 2 (scritti in C#) che scelgono i valori da un controllo "seleziona". La selezione provoca un postback sul server, che aggiorna lo stato della pagina. Sto quindi eseguendo un'attesa manuale (thread.sleep) dopo aver scelto un valore per attendere che la pagina venga modificata. e funziona bene con Thread.Sleep. Tuttavia, Thread.Sleep è una cattiva idea di utilizzare con il numero di buone ragioni per cui quando tiro fuori tutto il mio riga di codice Thread.Sleep poi tutti i miei casi di test cadono a pezzi e mi hanno cercato WebDriverWait, implicitamente ed esplicitamente nessuno lavora e molto frustrazioneWebDriverWait o ImplicitlyWait o ExplictlyWait niente funziona

qui di seguito è il codice di esempio che ho provato ....

// WebDriverWait

public IWebElement WaitForElement(By by) 
{ 
      // Tell webdriver to wait 
      WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30)); 
      wait.PollingInterval = TimeSpan.FromSeconds(2); 
      wait.IgnoreExceptionTypes(typeof(NoSuchElementException), typeof(NoSuchFrameException)); 
      wait.IgnoreExceptionTypes(typeof(StaleElementReferenceException), typeof(StaleElementReferenceException)); 

      IWebElement myWait = wait.Until(x => x.FindElement(by)); 
      return myWait; 
} 

provato anche questo:

WebDriverWait wait = new WebDriverWait(new SystemClock(), driver, TimeSpan.FromSeconds(30), TimeSpan.FromMilliseconds(100)); 

// Implicitamente:

driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 30)); 

// Attendere esplicito:

IWebDriver driver = new FirefoxDriver(); 
driver.Url = "http://somedomain/url_that_delays_loading"; 
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30)); 
IWebElement myDynamicElement = wait.Until<IWebElement>((d) => 
    { 
     return d.FindElement(By.Id("someDynamicElement")); 
    }); 

risposta

0

tenta di utilizzare

new WebDriverWait(driver, 30).until(ExpectedConditions.presenseOfElementLocated(byLocator)); 
+0

sì l'ho fatto provato ma non ha funzionato. –

1

Ecco ciò che funziona per me ->

WebDriverWait wait = new WebDriverWait(browser, new TimeSpan(0, 0, 30)); 

element = wait.Until<IWebElement>((driver) => 
    { 
    return driver.FindElement(By.Name("name_of_element"))); 
    }); 

Puoi anche farlo per ID - >

WebDriverWait wait = new WebDriverWait(browser, new TimeSpan(0, 0, 30)); 

element = wait.Until<IWebElement>((driver) => 
    { 
    return driver.FindElement(By.Id("id_of_element"))); 
    }); 

Senza vedere altro del codice, sarà difficile determinare il motivo per cui non funziona.

+0

fammi sapere quale altro codice desideri, quindi posso incollarlo e fammi provare –

0

trovo una soluzione con StackOverflow :) e questo funziona:

click on partialLinkText("Exit") 
remote.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS) 
remote.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS) 
// Thread.sleep(7000) // for js-work 
(new WebDriverWait(remote, 245)).until(presenceOfElementLocated(By.partialLinkText("""Entry for > technician"""))) 
// Thread.sleep(3000) // for js-works 
Problemi correlati