Start Point: Selenium Webdriver with C#

Selenium-dotnet. This article will be helpful for people who are just starting with selenium with C#. (dot net ). Here it goes,

Shocked to see Taxi on the Selenium WebDriver blog page.

But let’s learn about Selenium WebDriver with an example of Taxi and the Taxi Driver.

Imagine you need to go to the airport from your hotel by Taxi.

When you get into a taxi, you start giving directions to the driver so that you can reach your destination as quickly as possible. You can depend on the driver to listen to your instructions, and follow your directions to your destination according to your instructions.

TAXI is the browser, and the Taxi driver is the driver for the browser. Imagine you (the customer ) are code.

Communication between the Taxi driver and you is two-way. When the driver accelerates, brakes, and turns, the taxi responds back to him. You and the taxi driver discuss the shortest route to take.

So all communication is two-way.

Selenium Webdriver Bi Di

Now, let’s us understand how to initialize a driver, 

Syntax to open a Chrome using the Chrome driver:

IWebDriver driver = new ChromeDriver();

  As soon as you run the syntax, you will have the driver and Chrome open. 

In the same way that the customer gives instructions to the driver and the driver as per instructions gives the command to the taxi and the taxi executes the commands. 

Similarly, we the testers give commands to the browser driver and the browser driver gives commands to the taxi.

Remember all the commands will always go from the driver to the webpage.

WebDriver Commands

Navigation Commands

Back(): To navigate back from the current page to the previous page on a browser;

Below is the Syntax :

driver.Navigate().Back();

Forward (): To navigate forward from the current page to the next page on a browser;

Below is the Syntax :

driver.Navigate().Forward();

Refresh (): Refresh the browser page;

Below is the Syntax :

driver.Navigate().Refresh();

GoToUrl(): To open a URL on a browser we use GoToUrl();

Below is the Syntax :

driver.Navigate().GoToUrl(“https://qualitytestinghub.com/”);

Find Elements Commands

Anything on a web page is a Web element.

One element or multiple elements can be on a webpage with the same HTML tag.

FindElement

When we find a single element on a web page having a unique attribute value of an HTML tag or with a Unique HTML tag, it is called findElement.

      Syntax to findElement : 

driver.FindElement(By.Locator(“value”)).Command();

FindElements

When we find multiple elements on a web page having the same attribute value as an HTML tag. Or multiple values with the same HTML tag. It is called find elements

          Syntax to find elements:

driver.FindElements(By.Locator(“value”)).Command();

WebElements Commands

Anything on a web page is a Web element.

To do any operation like a keyboard event or mouse event we can do it with WebElements Commands.

  • Clear(); It is used to clear data from an enabled text field.

Syntax :   

driver.FindElement(By.Locator(“value”)).Clear();

  • Click(); It is used to Click on a webElement.

Syntax : 

driver.FindElement(By.Locator(“value”)).Click();

  • Sendkeys(); It is used to enter a value in a field.

   Syntax :

driver.FindElement(By.Locator(“value”)).Sendkeys(“valueToEnter”);

  • Text(); It is used to get value from a field that is visible in between the opening tag and the closing tag of HTML.

   Syntax :

driver.FindElement(By.Locator(“value”)).Text;

  • GetAttribute(); It is used to get value from a field which is a self-closing tag.

   Syntax : 

driver.FindElement(By.Locator(“value”)).GetAttribute(“attributename”);

Browser Commands

To get the Url, Title, PageSource of the opened link. We can get it with browser commands.

Below are the syntax to get the details,

 var url = driver.Url;

 var title = driver.Title;

 var pageSource =driver.PageSource;

Locators

Locators are a way to find a path to the web element on a web page.

It can be the same way how you travel to a path to reach your destination.

Also, there can be different ways to go to reach the destination.

Similarly, there are different ways to locate a web element on a webpage.

Types of Locators are :

  • By.Id(“value”)

  • By.ClassName(“value”)

  • By.Name(“value”)

  • By.LinkText(“value”)

  • By.CssSelector(“value”)

  • By.XPath(“value”)

Waits

Explicit wait : 

Explicit waits give your code the capability to halt the execution of your program or to freeze the thread until the condition you pass it resolves. As long as the wait timeout has not expired, the condition is called with a certain frequency. This means that as long as the condition returns a false value, it keeps trying and waiting.

In WebDriver, explicit waits can be used to synchronize the browser’s DOM state and your WebDriver script, since they allow you to wait for a condition to occur.

driver = new ChromeDriver();

driver.Url = “https://qualitytestinghub.com/”; driver.FindElement(By.LinkText(“AboutMe”).Click();

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

IWebElement result = wait.Until(dv => dv.FindElement(By.XPath(“//a/h3”))); Console.WriteLine(result.Text);

Implicit wait :

In order to find any element, WebDriver implicitly polls the DOM for a certain duration. In some cases, this can be useful when the webpage’s content is not available immediately, and some elements need time to load.

IWebDriver driver = new ChromeDriver();

driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);

driver.Url = “https://qualitytestinghub.com/”;

IWebElement element = driver.FindElement(By.Name(“dynamicElement”));

FluentWait :

Using a FluentWait instance, you can specify the maximum waiting time as well as how often the condition will be checked.

using (var driver = new ChromeDriver())

{

WebDriverWait wait = new WebDriverWait(driver, timeout: TimeSpan.FromSeconds(30)) { PollingInterval = TimeSpan.FromSeconds(5), };

wait.IgnoreExceptionTypes(typeof(NoSuchElementException));

var data = wait.Until(dv => dv.FindElement(By.Id(“foo”)));

}

Close and Quit Browser Window

Close() – It closes a single window from a web browser tab that it has focused on.

Syntax : 

driver.Close();

Quit() – It closes a single window from a web browser tab that it has focused on.

Syntax : 

driver.Quit();

Summary

Some references are taken from Selenium’s official website.

Keep Sharing and Learning. !!

Leave a Comment

Your email address will not be published. Required fields are marked *