This tutorial is the the next step in Creating a test automation framework with C#, Selenium 3 and Nunit; after creating the reporting module in the previous tutorial, we can finally start writing an automated functional test with the test automation framework we’ve just created.
Tutorial Overview:
Class 1 – Creating an Automated Test Using Selenium WebDriver 3 and C#
- Setting Up the Development Environment for Your Selenium Automation Framework
- How to Inspect Web Elements & Methods to Locate Them with Chrome Devtools
- How to Find Web Elements with Selenium WebDriver
Class 2 – How to Create a Test Automation Framework Architecture with Selenium WebDriver 3
- How to Create a Cross Browser Compatible Testing Framework
- Steps to Develop a Report Module in a Testing Framework
- Implementing a Report Module in an Automated Framework
- You’re here→How to Write a Functional Test with a Basic Selenium Automation Framework
Class 3 – Utilizing Test Automation Framework with Advanced Capabilities
- Page Object Pattern: Advantages & Implementation
- Read Data From CSV File in C#
- How to Create a Test Suite in Selenium WebDriver and C#
- How to Use Data Driven with Selenium Test Suite
Before we begin creating a functional test, we’ll need to create a new project in the Solution, in order to have a separation between the framework’s project to the automated tests themselves.
There are two main reasons for separating the framework’s project and the tests’ project:
- Modularity of the code
- Easier to maintain a project that is dedicated to the tests
The Project will be called ‘AutomatedTestCases’. Next, change the name of the class while creating the project in Sanity.cs
It should appear as follows in the screenshot:
The automated functional tests will be executed on the blog’s page in the following url: https://blog.testproject.io
Open the file Sanity.cs and paste the following piece of code:
using NUnit.Framework; using OpenQA.Selenium; using Test; namespace AutomatedTestCases { [TestFixture] public class Sanity : AutomationCore { [Test] public void ClickContactUs() { Browsers.getDriver.FindElement(By.Id("menu-item-1296")).Click(); } } }
Lets get into more detail about the steps of the code above:
The first step is to include the project of the automation framework into the automated tests project. This is performed by adding ‘using Test;’
Afterwards, we’ll need to inherit from the AutomationCore, in order to make the framework and browser launch before executing any tests. Next, we’ll be starting with the automated tests.
Browsers.getDriver.FindElement(By.Id(“menu-item-1296”)).Click();
We are receiving the WebDriver from our Browser’s class and then in order to locate the element we’ll be using the FindElement method while passing the element’s Id. After we have the element we can click on it.
When the functional test is complete, the results will appear in the ‘Test Explorer’ window.
Obviously, the report we created has to appear in the path we’ve configured for it.
You have now completed the last step in designing your selenium testing framework, continue on to the Next tutorial where we’ll be covering the implementation of advanced capabilities when creating a test automation framework, and specifically: Page Object Pattern: Advantages & Implementation.
I would be happy to see your questions/ ideas / stories in the comments below!
I have created two projects as mentioned in the tutorials(1
Hi Asya,
Thanks for sharing such nice tutorials. There is minor refinement required as per my point of view. Your tutorials lack the complete steps defining like currently i am facing an issue which is…..I have configured two separate projects as mentioned in the tutorial and i am not able to start them because i don’t know what Output type required for each project, currently i have configured the output type of both projects as Class library.
While i tried to start the project i am facing an error i.e. there should be one executable project. Please help me, although i have marked the Automatedtestcases project as startup project.
After that i have marked the Output type of Automatedtestcases project as Windows Application and other one as Class library but system is doing nothing upon start.
Regards,
Muhammad Islam
+92-345-4057636
Hi Islam,
You don’t need to change the Output type of project.
You don’t need to run the projects with F5 (Run) like a regular project.
You should be running the tests from the Visual studio built in TestRunner.
AutomatedTestCases -> ClickContactUs right click and Run
Hi Asya,
Thanks for this tutorial. It was very helpful! I have a question for you though. Pagefactory is going to be deprecated very soon. Can you please show us how to modify the existing code in your tutorial based on that? I am asking about these specific lines:
private static T getPages() where T : new ()
{
var page = new T();
PageFactory.InitElements(Browsers.getDriver, page);
return page;
}
Hi,
Thanks for the tutorial. I tried creating test cases in AutomatedTestCases project.
Browsers.getDriver.FindElement(By.Id(“ist-ib”)) this line throws me an exception “System.NullReferenceException: ‘Object reference not set to an instance of an object”.Could you please help me to sort this problem.