Blog

Setup Selenium with C#

Share:

Open Visual Studio

Click on File >> New >> Project

In New Project template,

    • Click on Visual C# in the left panel.
    • Click on Console Application.
    • Enter Name as

Click on OK.

After successful creation of project, below screen would be displayed:

Setting up Selenium WebDriver in Visual Studio

To install the Selenium WebDriver, follow the below steps:

Click on Tools >> NuGet Package Manager >> Manage NuGet Package for Solution…

In the next screen,

    • Click on Browse.
    • Search for Selenium in the Search bar.
    • Select the first result “Selenium.WebDriver”
    • Check the project checkbox.
    • Click on Install.

Review the changes and click on OK in the Preview pop-up.

After successful install of Selenium WebDriver, below message will be displayed in Output window.

Downloading Chrome Driver

We would be executing our Selenium scripts as well as automating any web application in Chrome browser. Hence, to launch the Chrome browser, we need to have ChromeDriver downloaded in the system.

To download ChromeDriver, follow the below steps:

  • Check the version of your Chrome browser.
    • Open Chrome browser
    • Click on three vertical dots on top most right hand side in the browser.

Click on Help >> About Google Chrome.

In the next screen, Chrome version is displayed in About Chrome section.

Open below link to download ChromeDriver:
https://chromedriver.chromium.org/downloads

Based on the Chrome browser version, click on the ChromeDriver download link:

On click of the download link, a window will open specifying the chromeDriver for various platforms. Download the one according to your system’s configuration.

On click of the link, ChromeDriver zip file will get downloaded. Unzip the file and place it in some folder.

Writing and executing first Selenium test

Right click on Project and click on Add >> New Item…

In Add New Item template, click on Class under Visual C# items and provide the Name as DLADemo.cs and click on Add button.

New class DLADemo.cs will get added to the project.

Write the following code in DLADemo.cs file:

Open the Program.cs and write the following code in Main method.

Click on Start to run the application.

Browser will open and run the scripts written and browser will get closed.

Creating NUnit test class in Selenium

To install the NUnit Framework, follow the below steps:

Click on Tools >> NuGet Package Manager >> Manage NuGet Package for Solution…

In the next screen,

    • Click on Browse.
    • Search for NUnit in the Search bar.
    • Select the first result “NUnit”.
    • Check the project checkbox.
    • Click on Install.

Review the changes and click on OK in the Preview pop-up.

Review the changes and click on OK in the Preview pop-up.

Installing NUnit Test Adapter

 Click on Tools >> NuGet Package Manager >> Manage NuGet Package for Solution…

In the next screen,

    • Click on Browse.
    • Search for NUnitTestAdapter in the Search bar.
    • Select the first result “NUnit3TestAdapter”.
    • Check the project checkbox.
    • Click on Install.

Review the changes and click on OK in the Preview pop-up.

After successful install of NUnit Test Adapter, below message will be displayed in Output window.

Writing and executing first Selenium test with NUnit annotations

Write the below code in DLADemo.cs file.

Click on Build >> Build Solution.

Once the build is successful, click on Test >> Windows >> Test Explorer.

Test Explorer window will be opened and the test script with annotation Test(“OpenUrl”) will be displayed in the Test Explorer window.

Select the test and right click on it. Click on Run Selected Tests.

Selenium will open the browser, then execute the test script and will close the browser and test case will get passed and will be displayed under Passed Tests in Test Explorer window.

Related Articles
Categories
Latest Articles

Popular Courses

Google Reviews
  • review rating 4  It was good session, I left my training room with basic knowledge on all the tools. Tutors have great knowledge and experience on the tools. But three days is not enough time.

    thumb sairam shyamala
    6/30/2019
  • review rating 5  Excellent Training program. Training courses and trainers are very good. Trainer will explain you as many time you ask the question. Best thing is hands on based training. You learn by doing. Looking for more courses from here.

    thumb Vijay Kumar
    6/30/2019
  • review rating 4  Training provided by the trainers in DevLabs Alliance is completely awesome. Trainers are highly qualified in their areas with practical experience. Since it is 3 days training we have completely got the opportunity to have hands on.

    thumb Salaja K
    6/30/2019
  • review rating 5  Training provided by the trainers here is completely awesome. Trainer is highly qualified in their areas with practical experience. Also amenities provided during training by institute is great

    thumb Abhishek Tripathi
    6/02/2019
  • review rating 4  Both trainers (Ravi and Naresh) were excellent in their approach. Session was less of a theory and more of a practical. And even the theory part was quite interesting. A bit issue with the volume of information which I had received in just a span of 2-3 days - it seemed a bit over-loaded. But overall, it has given me a great platform to take it forward from here!

    thumb Varenya Varshney
    6/02/2019
$('.enrollbtn').on('click', function (){ $( "#announcement" ).tabs( "option", "active", 0 ); $(' a[href="#announcement"]').trigger('click'); });