Blog We love to talk!

Feb Thu

Selenium: Waiting and Validating

  •  2-2-2017
selenium

In this post we will cover 2 different techniques to make your Selenium tests more useful and robust. If you are not already familiar with the Selenium basics you might want to read our introduction guide and the basics about elements first.

Waiting For Conditions

Sooner or later you will have to wait on a specific condition in your Selenium tests. Maybe you need to wait for an element to appear or a page to finish loading. Luckily Selenium has a great many different conditions you can wait for without writing any manual sleeps or pauses in your tests.

Static Waits

A static wait is simply a set time period Selenium should wait for something to occur. You could, for example, tell Selenium to keep searching for an element up to 10 seconds. This would look something like this in code:

IWebDriver driver = new ChromeDriver();

driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));

driver.Navigate().GoToUrl("https://www.browseemall.com");

IWebElement element = driver.FindElement(By.Id("locator"));

driver.Quit();

While this works rather well it is often times not the best possible option. For one you could to use different timeout settings for different actions like a longer running task. For this Selenium gives you more explicit waits on dynamic conditions.

Dynamic Waits

With a dynamic wait you are not limited to locating specific elements. You can use a wide collection of different conditions like waiting for visibility of an element or waiting until a element is removed from the DOM. Lets take a look at a simple example:

IWebDriver driver = new ChromeDriver();

driver.Navigate().GoToUrl("https://www.browseemall.com");

WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0, 0, 5));
wait.Until(ExpectedConditions.ElementIsVisible(By.Id("logout")));

driver.Quit();

This code will wait until an element is actually visible to the user regardless of the fact that it is always attached to the DOM. The full list of possible wait conditions is this:


ExpectedConditions.AlertIsPresent();
ExpectedConditions.AlertState(true);
ExpectedConditions.ElementExists(By.Id("button"));
ExpectedConditions.ElementIsVisible(By.Id("logout"));
ExpectedConditions.ElementSelectionStateToBe(By.Id("checkbox"), true);
ExpectedConditions.ElementToBeClickable(By.Id("button"));
ExpectedConditions.ElementToBeSelected(By.Id("checkbox"));
ExpectedConditions.FrameToBeAvailableAndSwitchToIt("frame");
ExpectedConditions.InvisibilityOfElementLocated(By.Id("notthere"));
ExpectedConditions.InvisibilityOfElementWithText(By.Id("notthere"), "text");
ExpectedConditions.PresenceOfAllElementsLocatedBy(By.CssSelector(".button"));
ExpectedConditions.StalenessOf(element);
ExpectedConditions.TextToBePresentInElement(element, "text");
ExpectedConditions.TextToBePresentInElementValue(element, "value");
ExpectedConditions.TitleContains("title");
ExpectedConditions.TitleIs("the title");
ExpectedConditions.UrlContains("browseemall.com");
ExpectedConditions.UrlMatches("regex");
ExpectedConditions.UrlToBe("https://www.browseemall.com");
ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.Id("button"));

Validation

At one point or another you will need to check if the web application under test is actually performing the task you are testing. For this Selenium gives us the possibility to assert different properties of elements. Lets say you want to assert that an element contains a specific text as the end result of a calculation:

IWebDriver driver = new ChromeDriver();

driver.Navigate().GoToUrl("https://www.browseemall.com");

IWebElement element = driver.FindElement(By.Id("locator"));

Assert.IsTrue(element.Text.Equals("100"));

driver.Quit();

Quite easy right? The important thing to note here is that the assert will fail your whole test case if it is not met. So a real world test case should always contain at least one assert otherwise the test can succeed even though the application does not actually work.

You can assert quite a lot of different properties using this technique. A few more examples:

IWebDriver driver = new ChromeDriver();

driver.Navigate().GoToUrl("https://www.browseemall.com");

IWebElement element = driver.FindElement(By.Id("locator"));

Assert.IsTrue(element.Displayed);
Assert.IsTrue(element.Enabled);
Assert.IsTrue(element.Selected);
Assert.IsTrue(element.Text.Equals("test"));

Assert.IsTrue(element.Displayed);

driver.Quit();

And this is really all there is to it. Now you are able to create complex waiting scenarios and validate the state of the application under test in an easy way.

No Comments

Leave a Reply

Your email address will not be published.