From Firefox, record new selenium test, for example for Google search
Export the test to C# web drive
This is how it looks like after export
Open visual studio, create new test project
By default the file UnitTest1.cs is created
Create a new cs file, name it for example GoogleSearch
Copy the code from the cs file generated by selenium and paste it in the file GoogleSearch.cs
Add reference in the project to Selenium .Net WebDriver, WebDriver.dll and WebDriver.Support.dl
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;
Find and replace “TestFixture” to “TestClass”, “SetUp” to “TestInitialize()”, “Test” to “TestMethod”, “TearDown” to “TestCleanup()”
To run with Chrome, download chromedriver.exe from https://code.google.com/p/chromedriver/downloads/list, place it in the bin folder
Change the driver in the code to Chrome then run the test
driver = new OpenQA.Selenium.Chrome.ChromeDriver();
Running the test, chrome will open, and Google search page will be displayed, displaying search results then closed
This was a step by step example on running selenium test from visual studio
Hi Amir,
ReplyDeleteI 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
Make sure you have Microsoft.VisualStudio.QualityTools.UnitTestFramework referenced
DeleteCheck 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
Hi Amir,
ReplyDeleteThanks 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?
Do you have the UnitTesting name space included
Deleteusing Microsoft.VisualStudio.TestTools.UnitTesting;
Hi Amir / Shahid,
ReplyDeleteCan any please help with the following error message?
1. http://screencast.com/t/lleaZpJMAdqf
2. http://screencast.com/t/FjqRo1Y7BPT
Thanks,
Mahesh.
For the first error, remove references for NUnit, comment or remove the below line
Deleteusing 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