Write a Selenium test script using C#

Last updated on Nov 24 2021
Swaminathan M

Table of Contents

Write a Selenium test script using C#

To write a Selenium test script using C# programing language, follow the below steps:

Steps Actions Input Expected Result
1. Create reference for the browser The reference for the browser should be created.
2. Navigate to the Google home page. https://www.google.com/ The Google home page must be displayed.
3. Identify the Google search text box and pass the worth Tecklearn tutorials The Google search box should be identified, and the value should be entered.
4. Identify and Click on the Google search button. The Google search button should be identified, and clicked.
5. Close the Browser. The Browser should be closed.

We are creating our test scripts step by step to offer you an entire understanding of every component intimately.
Step1
To create a reference for the browser, we’ll follow the below process:
Note: To declare the interface: prefix “I” before the interface name. Like as we will see within the below table that in C#, we put “I “before interface (IWebDriver), whereas, in java, there’s no got to put “I “before the interface (WebDriver) name.
C#                           Java
IWebDriver          WebDriver
IWebElement      WebElement
Here the sample code:
1. IWebDriver driver = new ChromeDriver();
• While scripting this above line of code, we encounter the below error:

Page 2 Image 1
error

• To overcome this error, we’ll use the below statement, which helps us to feature the Selenium reference.
1. using OpenQA.Selenium;
• While adding the reference of the chrome driver, it’ll give the below error:

Page 3 Image 2
error

 

• And to resolve this error, we’ll use the below statement in our code.
1. using OpenQA.Selenium.Chrome;
Step2
In this step, we are getting to navigate https://www.google.com/ URL within the browser.
And the sample code is as below:
1. driver.Navigate().GoToUrl(“https://www.google.com/”);
Note: GoToUrl() method is employed to navigate the URL.
Step3
To identify the Google search box, follow the below process:
In this step, we try to locate the Google search text box with the assistance of its Name attribute.
• Right-click on the Google search text box, and choose the Inspect option within the pop-up menu as we will see within the below image:

Page 4 Image 3
Inspect

• The developer tool window are going to be launched with all the precise codes utilized in the event of the Google search text box where we’ll copy the worth of its Name attribute that’s “q”.

Page 4 Image 4
developer tool

• And the sample code is as below:
1. IWebElement ele = driver.FindElement(By.Name(“q”));
Step4
In this step, we are passing the worth within the Google search box.
Here the sample code:
1. ele.SendKeys(“tecklearn tutorials”);
Step5
To identify the Google search button, follow the below process:
• Right-click on the Google search button, and choose the Inspect option from the given pop-up menu as we will see within the below image:

Page 5 Image 5
Inspect

• The developer tool window are going to be launched with having all the precise codes utilized in the event of the Google search button.
• Then, copy the worth of its name attribute that’s “btnK” as we will see within the below image:

Page 6 Image 6
attribute btnK

• And, the sample code is as following:

1. IWebElement ele1 = driver.FindElement(By.Name("btnK"));
2. //click on the search button
3. ele1.Click();
Step6
In the last step, we'll close the browser after the completion of all the actions performed on the browser.
Here the sample code:
1. driver.Close();
Our final test script will appear as if this after completing all the test scenarios.
1. using OpenQA.Selenium;
2. using OpenQA.Selenium.Chrome;
3. using System;
4. using System.Collections.Generic;
5. using System.Linq;
6. using System.Text;
7. using System.Threading;
8. using System.Threading.Tasks;
9. namespace SeleniumTest
10. {
11. class Program
12. {
13. static void Main(string[] args)
14. {
15. Console.Write("test case started ");
16. //create the reference for the browser
17. IWebDriver driver = new ChromeDriver();
18. // navigate to URL
19. driver.Navigate().GoToUrl("https://www.google.com/");
20. Thread.Sleep(2000);
21. // identify the Google search text box
22. IWebElement ele = driver.FindElement(By.Name("q"));
23. //enter the worth within the google search text box
24. ele.SendKeys("tecklearn tutorials");
25. Thread.Sleep(2000);
26. //identify the google search button
27. IWebElement ele1 = driver.FindElement(By.Name("btnK"));
28. // click on the Google search button
29. ele1.Click();
30. Thread.Sleep(3000);
31. //close the browser
32. driver.Close();
33. Console.Write("test case ended ");
34. }
35. }
36. }

Run the Test Script

Once we are through with writing the Selenium test script, we’ll run our test script.
Follow the below process, to run the test script:
• Click on the beginning button to execute the Programcs file as we will see within the below image:

Page 8 Image 7
execute the Program

• The execution process are going to be started, after clicking on the beginning button as we will see within the below image:

Page 9 Image 8
execution process

• The above test script will launch the Google Chrome browser and automate all the test scenarios.

So, this brings us to the end of blog. This Tecklearn ‘Write a Selenium test script using C #’ blog helps you with commonly asked questions if you are looking out for a job in Selenium and Automation Testing. If you wish to learn Selenium and build a career in Automation Testing domain, then check out our interactive, Selenium Certification Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

Selenium Certification Training

Selenium Certification Training

About the Course

Tecklearn’s Selenium Certification Training enables you to master the complete Selenium suite. The Selenium Training is designed to train developers and manual testers to learn how to automate web applications with a robust framework, and integrate it within the DevOps processes of an organization. This Selenium Certification Training will also help you master important concepts such as TestNG, Selenium IDE, Selenium Grid, Selenium WebDriver, etc. Get hands-on experience on widely used automation frameworks such as Data-Driven Framework, Keyword-Driven Framework, Hybrid Framework, and Behavior Driven Development (BDD) Framework. Throughout this online Instructor-led Selenium Certification Training, you will be working on real-life industry use cases.

Why Should you take Selenium Certification Training?

• The average salary of a Selenium Test Automation Engineer is $94k per year – Indeed.com.
• Automation Testing Market is expected to grow at a Compound Annual Growth Rate (CAGR) of 18.0% in the next three years.
• Global software testing market to reach $50 billion by 2020 – NASSCOM. Selenium tool supports more browsers and languages than any other testing tool.

What you will Learn in this Course?

Getting started with Selenium

• Introduction to Selenium testing
• Significance of automation testing
• Comparison of Manual and Automation Testing
• Installation of Java JDK, JRE and Eclipse

Setting the environment in Eclipse for Selenium

• Java Introduction
• Creating a Java function and executing
• Concepts of Java
• Properties File
• Reading Data from Excel File
• Database Connection
• Hands On

Advantages of Selenium automation testing

• Selenium Features
• Concept of Selenium Integrated Development Environment
• Understanding of the Selenium IDE features
• Addition of Script assertions and general commands
• Deploying the first Selenium Script
• Sample project IDE
• Recording Selenium test case
• Hands On

Selenium Web driver Automation

• Architecture of Selenium Web Driver
• Download and installation
• Creating a Java function using Selenium and execution
• Hands On

Deploying Web Drivers for scripting

• Getting the HTML source of Web Element
• Table and Form Elements
• Firebug extension and Fire Path installation
• Advance User Interactions and Cross Browser Testing
• Hands On

Deep dive into Selenium Web Driver

• Action Commands
• Web Table / Date Picker
• How to Implement Switching Commands in WebDriver
• Alerts
• Frames
• Hands On

Switching Operations in WebDriver using Window

• Selenium Webdriver Wait
• Implicit wait, Explicit wait
• Deploying searching elements using the link text, name, using XPath
• Calendar
• Hands On

Introduction to TestNG Framework

• Introduction to TestNG
• Advantages of TestNG
• Installing TestNG on Eclipse
• Rules to write TestNG
• TestNG Features
• Annotations
• Grouping
• Sequencing: Prioritization and Dependency
• Enable/Disable a test case
• Parameterization: Using Xml file and DataProvider
• Parallel Testing & Cross Browser Testing
• TestNG Report: HTML Report, Console Report, XML Report

JUnit Operations and Test Framework

• Annotations, Methods in JUnit
• Junit Test Suites, ANT Build and JUNIT reporting
• Types of Test Automation Framework
• Module Based Testing Framework
• Data Driven Testing Framework
• Keyword Driven Testing Framework
• Hybrid Driven Testing Framework
• How to implement Testing Framework in Project

Object Repository

• Understanding of Object Repository• Learning sample scripts using object repository
• Page Object Modelling
• Page Factory

JavaScript Functions

• Autosuggestion
• Headless Browser
• Sikuli
• XPath

Got a question for us? Please mention it in the comments section and we will get back to you.

 

0 responses on "Write a Selenium test script using C#"

Leave a Message

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