Selenium Webdriver C Tutorial

Selenium Webdriver C Tutorial Rating: 3,8/5 8633 votes

The only C# tutorial on the web designed specifically for test automation engineers using Selenium WebDriver. Master everything that you need for phenomenal test automation. Don't waste your time on lessons that you don't need to learn. Other course features. Utilizing Selenium WebDriver 3.0+ Utilizing the latest automation testing frameworks. In this video we will discuss Selenium WebDriver with C#. Selenium is one of the most popular automation tool in market. Selenium with C# also has high demand in market so let's discuss Step.

Selenium Overview:

Selenium is an open-source, web Automation Testing tool that supports multiple browsers and multiple operating systems. It allows testers to use multiple programming languages such as Java, C#, Python, .Net, Ruby, PHP, and Perl for coding automated tests.

C # Overview:

C# is an object-oriented programming language derived from C++ and Java. C# allows developers to build applications using Visual Studio on .Net platform. The following are the key features of C#.

  1. It is an Object-Oriented programming language
  2. It supports the development of console, windows and web-based applications
  3. It provides features such as Encapsulation, Inheritance, and Polymorphism.
What You Will Learn:[hide]

Basic Syntax of C #:

A program in C # need to contain the following sections

  1. Namespace declaration
  2. Classes
  3. Class Attributes and Methods
  4. Main method
  5. Program statements

Example-

Below is a sample C# program to print the text 'Guru99' on the system console.

Explanation:

  • A namespace in C# is a collection of multiple classes. Each namespace must be declared using the keyword 'using'.
  • The first statement of the code includes the namespace 'System' into our program. System namespace defines the fundamental classes and events used in C#. The namespace to be used depends on the program requirement.
  • The second statement is declaring a namespace 'FirstProgram' for the class 'DemoPrint.' A C# file can contain multiple classes within the same namespace.
  • The third statement includes the class declaration. A class may contain multiple attributes and multiple methods.
  • The fourth statement includes a declaration of the Main method. The main method is the entry point of execution for each class.
  • The last statement is the C# syntax used for printing a statement to console. WriteLine is a method of the class 'Console.

Set Up Visual Studio with Selenium WebDriver:

Visual Studio is an Integrated Development Environment (IDE) that is used by developers to build applications across multiple platforms such as Windows, Android, iOS and Cloud-based applications.

Step 1) Navigate to the URL https://www.visualstudio.com/downloads/ and Click on the 'Free download' button displayed on Visual Studio Community 2017 tab

Step 2) Open the exe downloaded. Click on 'Yes' if asked for Admin Rights.

Step 3) The below popup will appear. Click on 'Continue' button.

Files will be downloaded as shown in the popup below.

Step 4) In the next screen,

Select the checkboxes for

  • Universal Windows Platform development
  • Net desktop development

Click on 'Install.'

Wait for installation of each component to complete. Files are 16GB in size and will take time.

Step 5) The below pop up will be displayed. Click on 'Restart' button.

Step 6) Once the machine is restarted, search for 'Visual Studio 2017' on the start menu and click on the search result. The following popup will appear. Click on 'Not now, maybe later' link if you do not have an existing account.

Step 7) In the next screen,

  • Select color theme of your liking
  • Click the button 'Start Visual Studio'

Step 8) Visual Studio 'Get Started' screen will appear.

Create a new project in Visual Studio:

Step 1) In the File Menu, Click New > Project

Step 2) In the next screen,

  1. Select the option 'Visual C#'
  2. Click on Console App (.Net Framework)
  3. Enter name as 'Guru99'
  4. Click OK

Step 3) The below screen will be displayed once the project is successfully created.

Set up Visual Studio with Selenium WebDriver:

Selenium Webdriver C Tutorial

Step 1) Navigate to Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution

Step 2) In the next screen

  1. Search for Selenium on the resultant screen
  2. Select the first search result
  3. Check the project checkbox
  4. Click on 'Install'

Step 3) Click on 'OK' button in the pop-up screen

Step 4) The below message will be displayed once the package is successfully installed.

NUnit Framework: Overview

NUnit is the Unit Testing framework supported by Visual Studio and Selenium WebDriver. NUnit is the most widely used Unit Testing framework for .Net applications. NUnit presents the test results in a readable format and allows a tester to debug the automated tests.

We need to install NUnit Framework and NUnit Test Adapter onto Visual Studio inorder to use it.

Steps to install NUnit Framework:

1. Navigate to Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution

Step 2) In the next window

  1. Search for NUnit
  2. Select the search result
  3. Select Project
  4. Click Install

Step 3) The below popup will appear. Click on 'Ok' button.

Step 4) The below message will appear once the installation is complete.

Steps to download NUnit Test Adapter

Please note that the below steps work only for 32-bit machines. For 64-bit machines, you need to download the 'NUnit3 Test Adapter' by following the same process as mentioned below.

Step 1) Navigate to Tools ->NuGet Package Manager -> Manage NuGet Packages for Solution. In that screen,

  1. Search NUnitTestAdapter
  2. Click Search Result
  3. Select Project
  4. Click Install

Step 2) Click OK on the confirmation pop-up. Once install is done you will see the following message-

Selenium and NUnit framework:

Integration of selenium with NUnit framework allows a tester to differentiate between various test classes. NUnit also allows testers to use annotations such as SetUp, Test, and TearDown to perform actions before and after running the test.

NUnit framework can be integrated with Selenium by creating a NUnit test class and running the test class using NUnit framework.

The below are the steps needed to create and run a test class using NUnit framework.

Steps to create a NUnit Test class in Selenium:

Step 1) In the Solution Explorer, Right click on project > Add > Class

Step 2) Class creation window will appear.

  1. Provide a name to the class
  2. Click on Add button

Step 3) The below screen will appear.

Step 4) Add the following code to the created class. Please note that you need to specify the location of 'chromdriver.exe' file during chrome driver initialization.

Download SUPERAntiSpyware All purchases have a 30-day, unconditional 100% money-back guarantee. If you have already purchased SUPERAntiSpyware, click the Download Free Trial - Pro button to install the Professional Edition of the program. You will enter your registration code to convert the Free Trial to the paid Professional Edition during. Free download spyware software for mac. Top 10 Best Free Spyware Removal Software 2019 Get full antivirus protection and remove all spyware today! Spyware is malicious software which finds its way onto your computer and will track data about you and your computer usage and use this to obtain sensitive information about users or intercept advertising on your computer. Spyware is often bundled with other software or downloads on file-sharing sites (e.g., sites where you download free music or movies), or is installed when you open an email attachment. Because of the secretive nature of spyware, most people are not aware when spyware is on a computer that’s unprotected by anti-spyware. Jun 26, 2019  Spyware is nasty business, threatening your security by collecting information about your online activity without your knowledge or consent. SuperAntiSpyware Free Edition is a comprehensive.

Step 4) Click on 'Build' -> 'Build Solution'

NOTE: You may get an error like 'Does not contain a static 'main' method suitable for an entry point' when you build

To resolve this Got to Project > Properties and change Output Type to 'Class Library.' The default is 'Console Application.'

Step 5) Once the build is successful, we need to open the Test Explorer window. Click on Test -> Windows -> Test Explorer

Step 6) Test Explorer window opens with the list of available tests. Right-click on Test Explorer and select Run Selected Tests

Step 7) Selenium must open the browser with specified URL and close the browser. Test case status will be changed to 'Pass' on the Test Explorer window.

Selenium WebDriver Commands in C#:

C# uses the interface 'IWebDriver' for browser interactions. The following are the category of commands available in C#.

  1. Browser commands
  2. Web Element commands
  3. Dropdown commands

Let's study them one by one

Browser commands:

The following are the list of browser commands available in C#.

Command Name

Description

Syntax

Url Command

This command is used to open a specified URL in the browser.

Title Command

This command is used to retrieve the page title of the web page that is currently open

PageSource Command

This command is used to retrieve the source code of web page that is currently open.

Close Command

This command is used to close the recently opened browser instance.

Quit Command

This command is used to close all open browser instances

Back Command

This command is used to navigate to the previous page of browser history.

Forward Command

This command is used to navigate to the next page of browser history.

Refresh Command

This command is used to perform browser refresh.

Webelement Commands:

A Webelement represents all the elements on a web page. They are represented by HTML tags. Each of the buttons, textboxes, links, images, tables, and frames fall under Webelements. Operations on web elements can be triggered using the IWebelement interface. To interact with a Webelement, we need to find the element on the webpage and then perform operations on it. Tools like Firebug and Firepath can be used to identify the Xpath of Webelement.

The following are the list of Webelement commands available in C#.

Command Name

Description

Syntax

Click command

This command is used to click on a Webelement. For the element to be clickable, the element must be visible on the webpage. This command is used for checkbox and radio button operations as well.

Clear command

This command is specifically used for clearing the existing contents of textboxes.

SendKeys command

This command is used to input a value onto text boxes. The value to be entered must be passed as a parameter to

Displayed command

This command is used to identify if a specific element is displayed on the webpage. This command returns a Boolean value; true or false depending on the visibility of web element.

Enabled command

This command is used to identify if a particular web element is enabled on the web page. This command returns a Boolean value; true or false as a result.

Selected command

This command is used to identify if a particular web element is selected. This command is used for checkboxes,radio buttons, and select operations.

Submit command:

This command is similar to click command, The difference lies in whether the HTML form has a button with the type Submit. While the click command clicks on any button, submit command clicks on the only the buttons with type submit.

Text command

This command returns the innertext of a Webelement. This command returns a string value as a result.

TagName command

This command returns the HTML tag of a web element. It returns a string value as the result.

GetCSSValue Command:

This method is used to return the color of a web element on the form of a rgba string (Red,Green,Blue, and Alpha).

Output- If the color of element is red, output would be rgba(255,0,0,1)

Dropdown Commands:

Dropdown operations in C# can be achieved using the SelectElement class.

The following are the various dropdown operations available in C#.

Command Name

Description

Syntax

SelectByText Command

This command selects an option of a dropdown based on the text of the option.

SelectByIndex Command

This command is used to select an option based on its index. Index of dropdown starts at 0.

SelectByValue Command

This command is used to select an option based on its option value.

Options Command

This command is used to retrieve the list of options displayed in a dropdown.

The above code prints all the options onto console within a dropdown.

IsMultiple command

This command is used to identify if a dropdown is a multi select dropdown; A multi select dropdown enables user to select more than one option in a dropdown at a time. This command returns a Boolean value.

DeSelectAll command

This command is used in multi select dropdowns. It clears the options that have already been selected.

DeSelectByIndex command

This command deselects an already selected value using its index.

DeSelectByValue command

This command deselects an already selected value using its value.

DeSelectByText command

This command deselects an already selected value using its text.

Code Samples

Example 1: Click on a link using XPATH Locator:

Test Scenario:

  1. Navigate to Demo Guru99 web page - http://demo.guru99.com/test/guru99home/
  2. Maximize the window
  3. Click on the 'Testing' menu
  4. Close the browser

Example 2: Entering data into TextBox and Click on a button using XPATH locator:

Test Scenario:

  1. Navigate to Guru 99 demo page - http://demo.guru99.com/test/guru99home/
  2. Enter data into email text box
  3. Click on sign up button

Example 3: Entering data into TextBox and Click on a button using CSS locator:

Selenium Webdriver Tutorial Python

Test Scenario:

  1. Navigate to Guru 99 demo page - http://demo.guru99.com/test/guru99home/
  2. Enter data into email text box
  3. Click on sign up button

Example 4: Select a value in the dropdown:

Test Scenario:

  1. Navigate to Guru 99 demo page - http://demo.guru99.com/test/guru99home/
  2. Click on SAP link
  3. Enter data onto name and email text boxes
  4. Select a value from the Course dropdown
  5. Close the browser

Summary:

  • In order to use Selenium with C#, you need to install Visual Studio.
  • NUnit is the Unit Testing framework supported by Visual Studio and Selenium WebDriver
  • We need to install NUnit Framework and NUnit Test Adapter onto Visual Studio inorder to use it.
  • NUnit framework can be integrated with Selenium by creating a NUnit test class and running the test class using NUnit framework.
  • NUnit also allows testers to use annotations such as SetUp, Test, and TearDown to perform actions before and after running the test.
  • Selenium WebDriver Commands can be categorized into Browser commands, Webelement Commands and Dropdown Commands.

This article is contributed by Sneha Gudapati

Selenium WebDriver is the most important component of Selenium Tool's Suite. The latest release 'Selenium 2.0' is integrated with WebDriver API which provides a simpler and more concise programming interface.

The following image will give you a fair understanding of Selenium components and the Test Automation Tools.


Selenium WebDriver was first introduced as a part of Selenium v2.0. The initial version of Selenium i.e Selenium v1 consisted of only IDE, RC and Grid. However, with the release of Selenium v3, RC has been deprecated and moved to legacy package.

In WebDriver, test scripts can be developed using any of the supported programming languages and can be run directly in most modern web browsers. Languages supported by WebDriver include C#, Java, Perl, PHP, Python and Ruby.

Before learning the concepts of Selenium WebDriver, you should be well versed with any of the supported programming languages. Currently, Selenium Web driver is most popular with Java and C#. For this tutorial, we are using Selenium with java. You can refer to the links given below to learn basic as well as advance concepts of Java and C#:

Selenium Webdriver Example Java

Java Tutorial: https://www.javatpoint.com/java-tutorial

C# Tutorial: https://www.javatpoint.com/c-sharp-tutorial

Selenium WebDriver performs much faster as compared to Selenium RC because it makes direct calls to the web browsers. RC on the other hand needs an RC server to interact with the browser.

WebDriver has a built-in implementation of Firefox driver (Gecko Driver). For other browsers, you need to plug-in their browser specific drivers to communicate and run the test. Most commonly used WebDriver's include:

  • Google Chrome Driver
  • Internet Explorer Driver
  • Opera Driver
  • Safari Driver
  • HTML Unit Driver (a special headless driver)

Selenium WebDriver- Architecture

Selenium WebDriver API provides communication facility between languages and browsers.

The following image shows the architectural representation of Selenium WebDriver.


There are four basic components of WebDriver Architecture:

  • Selenium Language Bindings
  • JSON Wire Protocol
  • Browser Drivers
  • Real Browsers

Selenium Language Bindings / Selenium Client Libraries

Selenium developers have built language bindings/Selenium Client Libraries in order to support multiple languages. For instance, if you want to use the browser driver in java, use the java bindings. All the supported language bindings can be downloaded from the official website (https://www.seleniumhq.org/download/#client-drivers) of Selenium.

JSON Wire Protocol

JSON (JavaScript Object Notation) is an open standard for exchanging data on web. It supports data structures like object and array. So, it is easy to write and read data from JSON. To learn more about JSON, visit https://www.javatpoint.com/json-tutorial

JSON Wire Protocol provides a transport mechanism to transfer data between a server and a client. JSON Wire Protocol serves as an industry standard for various REST web services. To learn more about Web Services, visit https://www.javatpoint.com/web-services-tutorial

Browser Drivers

Selenium uses drivers, specific to each browser in order to establish a secure connection with the browser without revealing the internal logic of browser's functionality. The browser driver is also specific to the language used for automation such as Java, C#, etc.

When we execute a test script using WebDriver, the following operations are performed internally.

  • HTTP request is generated and sent to the browser driver for each Selenium command.
  • The driver receives the HTTP request through HTTP server.
  • HTTP Server decides all the steps to perform instructions which are executed on browser.
  • Execution status is sent back to HTTP Server which is subsequently sent back to automation script.

Browsers

Browsers supported by Selenium WebDriver:

  • Internet Explorer
  • Mozilla Firefox
  • Google Chrome
  • Safari

Selenium WebDriver- Features

Some of the most important features of Selenium WebDriver are:

  • Multiple Browser Support: Selenium WebDriver supports a diverse range of web browsers such as Firefox, Chrome, Internet Explorer, Opera and many more. It also supports some of the non-conventional or rare browsers like HTMLUnit.

  • Multiple Languages Support: WebDriver also supports most of the commonly used programming languages like Java, C#, JavaScript, PHP, Ruby, Pearl and Python. Thus, the user can choose any one of the supported programming language based on his/her competency and start building the test scripts.
  • Speed: WebDriver performs faster as compared to other tools of Selenium Suite. Unlike RC, it doesn't require any intermediate server to communicate with the browser; rather the tool directly communicates with the browser.

Selenium Webdriver Tutorial W3school

  • Simple Commands: Most of the commands used in Selenium WebDriver are easy to implement. For instance, to launch a browser in WebDriver following commands are used:
    WebDriver driver = new FirefoxDriver(); (Firefox browser )
    WebDriver driver = new ChromeDriver(); (Chrome browser)
    WebDriver driver = new InternetExplorerDriver(); (Internet Explorer browser)
  • WebDriver- Methods and Classes: WebDriver provides multiple solutions to cope with some potential challenges in automation testing.
    WebDriver also allows testers to deal with complex types of web elements such as checkboxes, dropdowns and alerts through dynamic finders.

Selenium WebDriver Tutorial Index

Selenium Webdriver Tutorial For Beginners

Selenium WebDriver Tutorial

C Tutorial Point