Skip to main content

How to automate testing using Selenium and C#

Hi All,

today I’m going to explain about Selenium which is a open source automation tool and how to utilize it with C#. As you know automation tools save our time in terms of testing a software product. If we are doing some regression testings automation tools doing a great job in terms of saving the time.
So I’m going to explain a very simple example for using Selenium from C#.
We can divide selenium as two entities. One is Selenium IDE as a web browser extension. And the other one is Selenium Web Driver. So I’m going to talk about the selenium web driver which is using for web based testings from C# code. Basically Selenium Web driver will open the browser and doing the actions as we instructing in the C#code.
Firstly you need to install visual studio. For my case I have installed visual studio community version which is a free version of Microsoft.
Then you need to create a project by navigating to File -> New -> Project. And give a name for the project and click ok.
1
Now go to the Solution Explorer and right click on References and click on “Manage Nuget Packages…” as below.
2
Next step will be adding Selenium references in to the project. So what you need to do is search “Selenium” in the Browse tab as below.
3
Here you need to install below packages. I will focus for Chrome browser for this example but you can choose Firefox if you need to open your application in Firefox.
  • Selenium.WebDriver
  • Selenium.Support
  • Selenium.Chrome.WebDriver
After you installed above packages you can see below screen.
4
Now you have setup all the Selenium web driver in to your project. So what you need to do now is add some code to automate your browser based application. Here I will show you how to open the chrome web browser and search some content on it.
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.Chrome;
namespace SeleniumAutomation
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
using (var driver = new ChromeDriver())
{
driver.Manage().Window.Maximize();
driver.Navigate().GoToUrl("http://www.google.com");
var searchBox = driver.FindElementById("lst-ib");
searchBox.SendKeys("Automation using selenium");
var searchButton = driver.FindElementByName("btnK");
searchButton.Submit();
var searchResults = driver.FindElementById("resultStats");
}
}
}
}
Initially I’m maximizing the browser by using below code block.
driver.Manage().Window.Maximize();
Then I’m navigating to the “Google” homepage
driver.Navigate().GoToUrl(“http://www.google.com”);
Find the search text box by using the id on the homepage.
var searchBox = driver.FindElementById(“lst-ib”);
Enter the text as “Automation using selenium” in the textbox
searchBox.SendKeys(“Automation using selenium”);
Find the search button by using the name on the homepage
var searchButton = driver.FindElementByName(“btnK”);
Click “Submit” to start the search
searchButton.Submit();
This is a very basic example of doing automation from Selenium Web Driver with C#. You can explore more on this official site.
Thanks,
Prasad

Comments

Popular posts from this blog

Exploring the Marvels of Salesforce Marketing Cloud: Unleashing the Power of Marketing Automations

Salesforce Marketing Cloud is a comprehensive marketing automation platform offered by Salesforce, a leading customer relationship management (CRM) company. It is designed to help businesses manage and optimize their marketing efforts across various channels and touchpoints. The platform enables organizations to create, execute, and analyze marketing campaigns to engage with their target audiences more effectively and drive better results. Key features and capabilities of Salesforce Marketing Cloud include: Email Marketing: Users can create and send personalized email campaigns to segmented audiences, track email performance metrics, and automate email workflows to nurture leads and build customer relationships. Journey Builder: This tool allows marketers to design and automate customer journeys across multiple channels such as email, mobile, social media, and advertising. It helps create personalized experiences based on customer behavior and interactions. Social Media Marketing: Sale

Integrate .NET console application with Salesforce Rest API

According to the integration perspective in Salesforce there are several APIs to integrate with external systems. As an example there are REST API, SOAP API, Bulk API, Tooling API and Streaming API. Here I will focusing for Rest API. I have created a different post which will describing the integration with Salesforce Soap API. You can find it from  here . And for the Tooling API you can find it from  here . 1st of all you need to create a connected app in Salesforce in order to make the bridge to integrate with console app.  A connected app integrates an external application with Salesforce using APIs. Connected apps use standard SAML and OAuth protocols to authenticate, provide single sign-on, and provide tokens for use with Salesforce APIs. So I have created a separate post which will describe how to create a connected app to get consumer id and consumer secret. Please follow  this   post to get consumer id and consumer secret. After you have those details you need to create

Salesforce REST API

This post describe about the salesforce rest api and using salesforce rest api . Salesforce implements the OAuth to authenticate soap and rest calls with the client. So I will describe with the simple steps, how to create salesforce app that expose outside to authenticate and call rest methods. First you need to create a salesforce developer account if you don't have an developer account or sandbox account. If you don't have an developer account go with the following link and create an account. It's free.   https://developer.salesforce.com/signup . Once you create the developer account go to https://login.salesforce.com/ and login with your credentials.     Navigate to setup and type app and you will be able to find Apps section and click the link. So now you will be able to find Connected Apps section and click New. What we doing here is creating a connected app and that app will exposing to outside to authenticate and call rest api calls. So you can insert C