In this chapter we will Set Up Project for Selenium Automation Framework in CSharp. To achieve that we need to go through the series of steps.
- Download and Install Visual Studio
- Create a new Project
- Set Up Selenium with Visual Studio in C#
- Create a new Package
- Create a new Test Class
- Set Up NUnit & Test Adapter
- Create First LogIn Test
Step 1: Download and Install Visual Studio
Before going head it is required to have Visual Studio installed in the machine. Please go through the chapter of Download and Install Visual Studio. Make sure that you do not create any Console Application project. Just follow the steps till the installation of the Visual Studio.
Step 2: Create a new Project
1) Create a new Project by browsing through File >> New >> Project.
2) Select Visual C# from the left, then select Class Library from the middle. It will give the option to mention the Project name, location & solution name. Give the sensible name to the project, just for the sake of simplicity, enter OnlineStore and give the preferred location to the project. Click on OK button to proceed.
3) Now you will end up with the below screens on your machine.
Step 3: Set Up Selenium with Visual Studio in C#
1) Go to Tools >> Extensions and Updates..
2) In the Search Box, search for Selenium.WebDriver or Selenium. This will take few seconds to populate the Selenium. Once done, just select Selenium.WebDriver and click on Install to start the installation process.
You can also follow the steps to Set Up Selenium with Visual Studio in CSharp here.
Now that we have the Selenium DLLs referenced in our project it time to test the configuration by running a quick test.
Step 4: Create a new Package or Folder
1) Right click on the project and go to OnlineStore >> Add >> New Folder.
2) Name the folder TestCases.
Step 5: Create a TestCase
1) Right click on the TestCases folder and go to Add >> Item….
2) Name the Class as LogInTest, as the first test we are planning to automate is for the LogIn application.
2) The current state of the Project Solution Explorer should be like this.
Step 6: Set Up NUnit, NUnit Framework & NUnitTestAdapter
I have written a nice tutorial on How to Set Up Selenium Test using NUnit Framework. It is better to install NUnit and UnitTestAdapter before moving to writing Selenium Test.
Note: If you are using NUnit Version 2, then you should install NUnitTestAdapter version 2, but if incase the NUnit Version is above 3 then you must install NUnit3TestAdapter. Otherwise you will not see your test in the Test Explorer window.
Step 7: Write first LogIn Test
1) Enter the below code in the LogIn Test.
public void Test()
IWebDriver driver = new FirefoxDriver();
driver.Url = "http://www.store.demoqa.com";
// Find the element that's ID attribute is 'account'(My Account)
// Find the element that's ID attribute is 'log' (Username)
// Enter Username on the element found by above desc.
// Find the element that's ID attribute is 'pwd' (Password)
// Enter Password on the element found by the above desc.
// Now submit the form.
// Find the element that's ID attribute is 'account_logout' (Log Out)
// Close the driver
2) It is a time to Build the project. Click on Build >> Build Solution.
3) Now Go to Test >> Windows >> Test Explorer. Noticed that the Test is appeared in the Test Explorer window.
4) Right click on the test and click Run Selected Test. The LogIn test will run.
Note: Delete the Class1.cs file, as it was created by default when the project was created. To delete it just right click on the file and select Delete.
OnlineStore Project SnapShot