Verify Expected Conditions in Selenium C#

In this tutorial, we will talk about one more Dynamic Wait class which is the “ExpectedConditions” class.

ExpectedConditions class in Selenium C#

This class contains a lot of functions that are created on the basis of commonly used dynamic wait conditions. So we can directly use these functions in our wait logic.

Inside our Visual Studio here we will create one more test method, so create TestExpConditions() and make it public also we will use the [TestMethod] attribute with it.

Now, if we go inside the class definition of ExpectedConditions, we can see there are a lot of static functions which are created on the basis of commonly used dynamic wait logic.

For example- ElementExist(), ElementVisible(), UrlContains(), TitleContains() and so on. We can directly use these functions in our wait.

The logic condition:

So the scenario which we are going to automate is like that, First, we will go to the “udemy” website type the string as “HTML” and click on search, after that we are going to select the last element and click on that then click on login and validate that the login box or the dialogue box will appear or not.

[TestMethod]
public void TestExpConditions()
{
NavigationHelper.NavigateToUrl("https://www.udemy.com/"); 
ObjectRepository.Driver.Manage().Timeouts(). ImplicitlyWait(TimeSpan.FromSecon(1)); 
WebDriverWait wait=new WebDriverWait(ObjectRepository.Driver.TimeSpan.FromSecond(50)); 
wait.PollingInterval= TimeSpan.FromMilliseconds(250); 
wait.IgnoreExceptinTypes(typeof(NoSuchElementException),typeof(ElementNotVisibleException)); 
wait.Until(ExpectedConditions.ElementExist(By.XPath("//input[@type='search']"))).SendKeys("HTML");
ButtonHelper.ClickButton(By.CssSelector(".home-search-btn"));
wait.Until(ExpectedConditions.ElementExist(By.XPath("//*[@id='Courses']/li[12]/a/div[2]/div[1]/div/span"))).Click();
Console.WriteLine("Title:{0}",Until(ExpectedConditions.TitleContains("u")));
wait.Until(ExpectedConditions.ElementExist(By.XPath("//a[contains(text(),'Login')]"))).Click();
wait.Until(ExpectedConditions.ElementExist(By.XPath("div[@class='loginbox-v4 js-signin-box']")));
}

Explanation of code:

So here first of all we are copying the piece of code from our TestDynamicWait() where we were navigating to the required position in the page and also setting the implicit wait to 1 second. The block of code is given below

NavigationHelper.NavigateToUrl("https://www.udemy.com/"); ObjectRepository.Driver.Manage().Timeouts(). ImplicitlyWait(TimeSpan.FromSecon(1)); 
WebDriverWait wait=new WebDriverWait(ObjectRepository.Driver.TimeSpan.FromSecond(50)); 
wait.PollingInterval= TimeSpan.FromMilliseconds(250); wait.IgnoreExceptinTypes(typeof(NoSuchElementException),typeof(ElementNotVisibleException));

Now after that, we are going to use wait.Until() so in the first case, we need to supply the string “HTML” inside the Searchbox, so if we do inspect an element so the XPath is $x("//input[@type='search']")

In Until() we will write ExpectedConditions as all functions are static so we can use them with the help of the ClassName.ElementExist and here we will use (By.XPath("//input[@type='search']"));

So if we look at the class definition of ExpectedConditions the function we are using takes (By locator) as an argument. We can also see there the input of this Func Delegate is IWebDriver and the output will be IWebElement.

So we directly call the method and SendKeys (""); specify the text as “HTML”. After that, we need to click on the search button so ButtonHelper.ClickButton(By.CssSelector("") and the XPath of the search button will be with the help of inspect element (". home-search-btn").

After that, we need to wait for the element which is present at the last of this webpage. So we are going to inspect that element and use its absolute XPath. When we will get its XPath so write wait.Until(ExpectedConditions.ElementExist(By.XPath("//*[@id='Courses']/li[12]/a/div[2]/div[1]/div/span"))).Click();

Then as we know this is going to return us the web element so we will call the Click(); we can also check the title so Console.WriteLine("Title: {0}",wait Until(ExpectedConditions.TitleContains("u")));

after that, we want to click on login, if we do the inspect element so the XPath will be $x("//a[contains(text(),'Login')]") so here again we will use wait.Until(ExpectedConditions.ElementExist(By.XPath("//a[contains(text(),'Login')]"))).Click();

So if we click on login the particular dialogue box will appear again we will do the inspect element so here XPath will be $x("//div[@class='loginbox-v4 js-signin-box']") again we will use wait.Until(ExpectedConditions.ElementExist(By.XPath("div[@class='loginbox-v4 js-signin-box']")));

We will put a breakpoint on this line wait.IgnoreExceptinTypes(typeof(NoSuchElementException),typeof(ElementNotVisibleException)); and build the solution and run the script in debug mode.

Output: it has hit the debug point if we do a stepover it is able to locate the element and also able to supply the “HTML” string in it,

then click on the search button and wait for the element to exist

then click on that element and check that the title contains the substring “u”.

 

Then click on login so as we can see the login dialogue box appears and wait for the element to terminate that is this dialogue box.

So as we can see here how easy it is by using ExpectedConditions class and using the inbuilt dynamic projects.

In this manner, we can use ExpectedConditions class with built-in functions with creating our wait logic.