Web testing automation
C# project.
Automation of tests on the Web.
GitHub link: https://github.com/Apiquet/WebTest_Selenium_VisualStudio
Automation of tasks can be very useful, especially for repetitive ones.
First step: Set up Visual Studio.
Note: Visual Studio is a Microsoft’s IDE (Integrated development environment) to write code.
- Visual Studio Community can be installed for free from the official website.
- Once installed, a new console application project need to be created:
File > New Project > Visual C# > Console App - Then, some package must be installed: NUnit and Selenium.WebDriver
Tool > NuGet Package Manager > Manage NuGet Package for Solution
Selenium will allow to implement a web driver, it will create a browser instance. Once created, actions can be performed: click on actions, write in text zones, read information, take screenshots, etc.
NUnit will allow to put assert. Thanks to it, information on the web page will be verified (if needed).
Second step: Programming
- First, a a browser instance will be created:
RemoteWebDriver firefoxdriver = new FirefoxDriver();
public RemoteWebDriver Driver { get => firefoxdriver; set => firefoxdriver = value; }
To make it works, the geckodriver.exe file should be under the project directory (under the bin > Debug folder): https://github.com/mozilla/geckodriver/releases
- Then, the website url will be specified:
Driver.Url = "https://..";
With these lines, a browser instance has already been programmatically created.
- The next step would be to click on any button. To realize this action, the specific button needs to be found on the web page.
How to find the specific button or link:
As it’s an HTML page, any element can be seen thanks to its XPath. XPath is a path which allows to travel along the html tags. To find an XPath, an extension on the browser can be installed, I’m using XPath Helper.
- Download XPath Helper extension
- Click on the extension on the web page
- Press Shift and move the cursor on the specific element

How to click on the button or link:
IWebElement button = driver.FindElement(By.XPath("XPath"));
button.Click();
- Text can also be added to a text box. After getting the XPath of the Text Box element, a text can be written:
IWebElement username_Textbox = driver.FindElement(By.XPath("XPath"));
username_Textbox.SendKeys("username");
- Read a text:
var text = driver.FindElement(By.XPath(“XPath“)).Text;
In this case, we save the text value of the element.
Third step: Advance features.
In my case, I also set up a task scheduler and an e-mail function. With the e-mail function, I can send myself a screenshot or information that I have retrieved from the website.
I realized it with the SmtpClient feature:
SmtpClient smtpServer = new SmtpClient("smtp.live.com");
var mail = new MailMessage { From = new MailAddress("myEmailAdress@hotmail.com") };
mail.To.Add("recipientEmail@gmail.com");
mail.Subject = subject;
mail.Body = body;
mail.Attachments.Add(new Attachment(Screenshotpath));
smtpServer.Port = 587;
smtpServer.UseDefaultCredentials = false;
smtpServer.Credentials = new System.Net.NetworkCredential("myEmailAdress@hotmail.com", "MyPassword");
smtpServer.EnableSsl = true;
smtpServer.Send(mail);
The task scheduler feature needs a NuGet package: TaskScheduler.
The following lines create a new task:
TaskDefinition td = RegisterTask.NewTask();
Then, a description, a trigger, an action, etc, can be added:
// Create a trigger
td.Triggers.Add(new TimeTrigger
{
StartBoundary =
Convert.ToDateTime(DateTime.Today.Date.ToString("MM/dd/yyyy") + " " + hour + ":" + minute + ":00 AM")
});
// Create an action that will launch Fiddler in quiet mode
td.Actions.Add(new ExecAction("C:/Users/AppData/Local/Programs/Fiddler/Fiddler.exe",
"/quiet", null));
// Register the task in the root folder
RegisterTask.RootFolder.RegisterTaskDefinition(@"CScheduler\FiddlerRunner", td);
All the code is available on my Github profile:
https://github.com/Apiquet/WebTest_Selenium_VisualStudio
Image header from blog.catchpoint
