Monday, April 8, 2013

Running Selenium tests from Visual Studio – Step by step

From Firefox, record new selenium test, for example for Google search

2013-04-07_13-14-39

Export the test to C# web drive

2013-04-07_13-15-06

This is how it looks like after export

2013-04-07_13-15-50

Open visual studio, create new test project

2013-04-07_13-16-36

By default the file UnitTest1.cs is created

2013-04-07_13-25-47

Create a new cs file, name it for example GoogleSearch

2013-04-07_13-25-54

Copy the code from the cs file generated by selenium and paste it in the file GoogleSearch.cs

2013-04-07_13-26-42

Add reference in the project to Selenium .Net WebDriver, WebDriver.dll and WebDriver.Support.dl

2013-04-07_13-29-322013-04-07_13-29-07

Now the references added, you can see that the editor recognizes the selenium name spaces

using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;

2013-04-07_13-29-40

Find and replace “TestFixture” to “TestClass”, “SetUp” to “TestInitialize()”, “Test” to “TestMethod”, “TearDown” to “TestCleanup()”

2013-04-08_00-31-32

To run with Chrome, download chromedriver.exe from https://code.google.com/p/chromedriver/downloads/list, place it in the bin folder

2013-04-08_09-45-49

Change the driver in the code to Chrome then run the test

driver = new OpenQA.Selenium.Chrome.ChromeDriver();

2013-04-08_09-45-09

Running the test, chrome will open, and Google search page will be displayed, displaying search results then closed

2013-04-08_09-51-36

This was a step by step example on running selenium test from visual studio

6 comments:

  1. Hi Amir,
    I am trying to implement what you have mentioned above but it's throwing the following error when I am trying to build it:
    'The type or namespace name 'TestClass' could not be found (are you missing a using directive or an assembly reference?). Can you please tell me any additional reference which I need to add to the project which I may be missing?

    Thanks,
    Shahid

    ReplyDelete
    Replies
    1. Make sure you have Microsoft.VisualStudio.QualityTools.UnitTestFramework referenced
      Check the references screen shot for the ones that are referenced in the project

      http://lh6.ggpht.com/-yv8_iBB283A/UWJqaw7c6EI/AAAAAAAAAv4/WJsypFo1_Yk/s1600-h/2013-04-07_13-29-40%25255B2%25255D.png

      If the project type is test project, it should be there already

      Delete
  2. Hi Amir,
    Thanks for the reply.
    I tried adding all the possible references as I could probably think of/find. i have added all sorts of references, more than what you have specified in your screenshot but still have the same issue.
    Don't know why it's still happening...Any more thoughts?

    ReplyDelete
    Replies
    1. Do you have the UnitTesting name space included

      using Microsoft.VisualStudio.TestTools.UnitTesting;

      Delete
  3. Hi Amir / Shahid,

    Can any please help with the following error message?

    1. http://screencast.com/t/lleaZpJMAdqf
    2. http://screencast.com/t/FjqRo1Y7BPT

    Thanks,
    Mahesh.

    ReplyDelete
    Replies
    1. For the first error, remove references for NUnit, comment or remove the below line
      using NUnit.Framework;

      For the second error, i think you are trying to run the project, don't run it. Open test view and run the test from there, or run it from the test menu

      Delete