2

I would like to use waitforcondition Selenium extension in a c# project. I saw on the forum people adding: selenium.waitForCondition("some_javascript_boolean_test_as_a_string", "5000")

How can I add it to C# project as 'WaitForCondition' does not exist in the namespace 'Selenium' ?

Edit: I would like to check for two conditions. First both elements are loaded, let us say elements with id=el1 and id=el2. After that one of the elements el2 is removed from the DOM. As soon as el2 is removed, page is loaded.

Different
  • 81
  • 1
  • 2
  • 5

2 Answers2

3

You are using WebDriver (got that from your previous Selenium question), and thus the code you are seeing is incorrect. selenium.waitForCondition is old v1 code. You are using the nice new shiny v2 code.

What you want is specifically the WebDriverWait class that sits in the OpenQA.Selenium.Support namespace.

var driver = new FirefoxDriver();
var waitableDriver = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
var element = waitableDriver.Until(d => d.FindElement(By.Id("something")));

This would wait until it finds a certain element. However, it allows you to specify any Func<T, TResult>, thus it gives some great room for flexibility. You can harness the power of lambdas in C#.

What if you wanted to wait for an element to be visible to the user and have a value inside it? What if there is a message when you login that pops up after 5 seconds? All doable.

Also, there are some handy 'expected conditions' that are already made up for you to use, these are common situations that people might need to "wait" for a particular condition to be true, for instance it's common people need to wait until an element is not only rendered in the page but actually visible to the user. There is a ElementIsVisible expected condition for you to use, so you don't have to make up the logic manually.

This whole concept is known as explicit waits in the Selenium world, and is vital to automating the testing of an AJAX'ified application.

Nick Jones
  • 4,395
  • 6
  • 33
  • 44
Arran
  • 24,648
  • 6
  • 68
  • 78
  • Hi Arran, yes I saw this approach, but the problem is that waitableDriver.Until() expects IWebElement, but I need to evaluate condition instead. What I realy need is to be able to wait untill ajax page is loaded. I could use FindElement(), but the problem is that element already exists even though ajax call is still in the process. I would like to check for two conditions - as soon as one element gets loaded and another disapears. – Different May 28 '13 at 20:36
  • What condition do you need to evaluate? Edit your post to show it. Perfectly possible I'm sure. Is it some javascript? – Arran May 28 '13 at 21:04
  • I have edited post now, the question is as I wrote in the comment: "I would like to check for two conditions - as soon as one element gets loaded and another element disapears." – Different May 29 '13 at 07:20
  • Hi Arran, thanks for your reply. I t helped me to find the answer. – Different May 29 '13 at 11:34
0

I found the way to wait for condition to be true by setting WebDriverWait to wait on a bool value instead of IWebElement:

            waitableDriver.Until<bool>((d) =>
            {
                try
                {
                    IWebElement elementOverlay = d.FindElement(By.Id("overlay"));
                    return !elementOverlay.Displayed;
                }
                catch (NoSuchElementException)
                {
                    IWebElement element = d.FindElement(By.XPath("//li[@id='newBtn']"));
                    return true;
                }
            });
Different
  • 81
  • 1
  • 2
  • 5