0

When generating NUnit code from selenium ide, wait commands like clickAndWait generate an awkward pattern using a loop.

Wouldn't it be better to use a WebDriverWait.until?

Or am I getting something wrong?

UPDATE: Sorry, wrote from memory, the code i was referring to is on the waitForElementcommand and not clickAndWait.

This is the code i'm referring to:

// 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;
    }
}

Reading various guides and other answers, it seems to me that a better solution would be this one:

// 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;
    }
}
Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
SilverXXX
  • 31
  • 4

1 Answers1

0

Yes, using WebDriverWait is the better approach to wait for element exist, But instead of creating own custom ExpectedConditions you should use selenium provided ExpectedConditions.ElementExists function to wait until element exist as below :-

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

Hope it helps..:)

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
  • Thanks for the tip about ExpectedConditions, didn't know about that. Do you know if you can change the standard exporter? – SilverXXX Jul 20 '16 at 07:07
  • @SilverXXX I'm sorry..i didn't understand what are you asking?? – Saurabh Gaur Jul 20 '16 at 07:49
  • Right now, if we have a test in selenium ide and use "Export test case as... -> c# / NUnit / WebDriver" the first version with a cycle inside the test method get generated. Is there a way to change it so that the second one is generated? (or even adding a new export) – SilverXXX Jul 20 '16 at 07:54