In the last post I have shown you how to record and play back with Selenium IDE.
Now I will show you how Selenium WebDriver code generated from selenium IDE can be run at visual studio 2010 with NUnit Framework.
Selenium IDE has a functionality to convert its recorded script to Selenium WebDriver and C# in NUnit Framework.
So If we know how to record Selenium IDE (see the post : http://bit.ly/pa1TG5) then we can easily convert it to selenium WebDriver code.
Before starting we just define a Test case.
Test case :
- Open Selenium Website (http://seleniumhq.org/) in Firefox browser.
- Click Project tab
- Click Selenium IDE link
- Verify that Selenium IDE Text is present
Step 1: Record the test case in Selenium IDE
- Open Firefox Browser
- Open Selenium IDE window by Clicking Tools->Selenium IDE
- Make Red colored Record button on.
- Enter the URL : http://seleniumhq.org/ at the address bar and click Search button
- Click Project tab
- Click Selenium IDE link
- Verify that Selenium IDE Text is present
The recorded script will be like the following figure
Now make the record button off and Click Run button to run the script.
Step 2: Convert the Selenium IDE recorded script to C# WebDriver code
- Go to Selenium IDE window
- Click Options menu
- Click Format –> C#(WebDriver).
Sometimes in Options –> Format menu, C#(WebDriver) options may not be displayed. The menu options like:
In that case, there is some change needed in selenium IDE option window. Please follow the steps:
- Click Options menu in Selenium IDE window
- Click “Options…” and Selenium IDE options window is displayed.
Click Enable experimental features check box and click OK button. Now go to Format options and driver list.
- A message dialog is displayed after selecting the appropriate format like (C#(WebDriver))
- Click OK button
- Selenium IDE window shows the selenium WebDriver Code in C# format with unit testing Framework NUnit is implemented.
Step 2: Run the Code in visual Studio 2010
- Open Visual Studio 2010 IDE
- Create a New Class Library project by Clicking the options File –>New –>Project and New Project Window is displayed
- Select Visual C# from the template and project type Class Library
- Enter Project name, Location and Solution Name and Select .Net Framework 4.0
- Click OK button
- New Project is created
- Now Copy the whole text from Selenium IDE and paste into Class Library project editor. It is like Copy the whole text like CTRL+A to select the whole text and CTRL+C to copy. Then remove all sample code from VS editor and Paste CTRL+V to all code.
- There are some Reference error show at editor. It could not find Open QA namespace. For this we need some .dll file from Selenium client driver to add as a reference.
- Now go to selenium download page ( http://seleniumhq.org/download/ ) and download the latest version of Selenium Client Driver with C#. Here version 2.6.0 is downloaded.
- Now unzip downloaded zip file and rename the folder as “Selenium”
- Go to Solution explorer of IDE
- Right click Reference and click Add Reference option. Add Reference window is displayed
- Click Browse tab and go to Selenium folder. Inspect that there are 2 sub folder there – net35 and net40. net35 folder is for .Net framework 3.5 project and net40 for .Net framework 4.0 project. If the class library project is created as ,Net framework 3.5 then we select the .dll file of net35 folder. In this project .Net framework 4.0 is selected so we go to net40 folder
- Select all the dlls and click OK button and .dll files are added as reference
- Now NUnit dll’s are needed for NUnit framework
- Now download the latest NUnit form the site (http://www.nunit.org/index.php?p=download)
- Download the latest NUnit with windows verison (2.5.10) and Install
- Now right click on Reference and click Add Reference and go to NUnit folder where it is installed like (C:\Program Files (x86)\NUnit 2.5.10\bin\net-2.0\framework)
- Select (nunit.framework.dll) file and click OK
- Now all the references are added
- Add the following code at the TheUntitledTest() method
- TheUntitledTest() method will be
- public void TheUntitledTest()
- {
- driver.Navigate().GoToUrl(baseURL);
- driver.FindElement(By.LinkText("Projects")).Click();
- driver.FindElement(By.LinkText("Selenium IDE")).Click();
- Assert.AreEqual(driver.FindElement(By.XPath("//div[@id='mainContent']/table/tbody/tr/td/p/b")).Text, "Selenium IDE");
- // ERROR: Caught exception [ERROR: Unsupported command [isTextPresent]]
- }
- Class1.cs code (whole WebDriver code) will be
- using System;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading;
- using NUnit.Framework;
- using OpenQA.Selenium;
- using OpenQA.Selenium.Firefox;
- namespace SeleniumTests
- {
- [TestFixture]
- public class Untitled
- {
- private IWebDriver driver;
- private StringBuilder verificationErrors;
- private string baseURL;
- [SetUp]
- public void SetupTest()
- {
- driver = new FirefoxDriver();
- baseURL = "http://seleniumhq.org/";
- verificationErrors = new StringBuilder();
- }
- [TearDown]
- public void TeardownTest()
- {
- try
- {
- driver.Quit();
- }
- catch (Exception)
- {
- // Ignore errors if unable to close the browser
- }
- Assert.AreEqual("", verificationErrors.ToString());
- }
- [Test]
- public void TheUntitledTest()
- {
- driver.Navigate().GoToUrl(baseURL);
- driver.FindElement(By.LinkText("Projects")).Click();
- driver.FindElement(By.LinkText("Selenium IDE")).Click();
- Assert.AreEqual(driver.FindElement(By.XPath("//div[@id='mainContent']/table/tbody/tr/td/p/b")).Text, "Selenium IDE");
- // ERROR: Caught exception [ERROR: Unsupported command [isTextPresent]]
- }
- private bool IsElementPresent(By by)
- {
- try
- {
- driver.FindElement(by);
- return true;
- }
- catch (NoSuchElementException)
- {
- return false;
- }
- }
- }
- }
- Build the solution by right clicking on the solution name and Click build solution
- Inspect that no Error message is displayed and build is succeeded.
- Now open NUnit application editor by going Start – > Program – > NUnit 2.5.10 –> NUnit
- Now go to File – > Open Project
- Select the Class Library dll file from bin/debug folder of Class Library project
- Click Run button
- Firefox Browser is opened and Script runs and Test is successful.
That’s all. I have described here all the steps in a very easy and details way. If any thing could not be understand, please comment me, I must answer. Please comment me about the whole blog.
In my next blog, I will show how we can run our selenium WebDriver code in Visual Studio Test Framework.
Great post!
ReplyDeleteGood post Indeed Nadim. Keep up the good work..
ReplyDelete"Select the Class Library dll file from bin/debug folder of Class Library project" in my visual c# 2010 express the Class Library dll file is in bin/Release folder, but everything else is ok :), thank you
ReplyDeleteHi Nadim,
ReplyDeleteThanks for the good post. I followed the steps that you have mentioned, but I am not able to run the test. It fails in NUnit. I am getting the following error
SeleniumTests.Untitled.TheUntitledTest:
SetUp : OpenQA.Selenium.WebDriverException : Cannot find firefox binary in PATH. Make sure firefox is installed. OS appears to be: OpenQA.Selenium.Platform
TearDown : System.NullReferenceException : Object reference not set to an instance of an object.
I am using Windows 7 - 64 bit machine.
Your response will be appreciated.
Thanks,
A Tester
This should help others in this situation:
Deletehttp://stackoverflow.com/questions/9907492/how-to-get-the-firefox-working-in-webdriver
To copy/paste that:
Either specify the folder (in which your Firefox binary is) in your PATH system variable - here's how.
WebDriver driver = new FirefoxDriver(new FirefoxBinary(new File("path/to/your/firefox.exe")), profile);
I hope this helps.
Make sure you are using the 32 bit version of nunit. Firefox is a 32 bit browser.
DeleteThis issue happened to me, and I resolved it. I have a 64 bit windows OS, but Firefox is a 32 bit browser. I was trying to use the 64 bit version of nunit, which was giving this "Cannot fine firefox binary in PATH" error. I resolved this by using the 32 bit version of nunit. Basically, there are two exe files in the nunit folder, nunit.exe and nunit-x86.exe. If you are getting this "Cannot fine firefox binary in PATH" error, most likely you need to use the 32 bit version of nunit - the Nunit-x86.exe.
Good one
ReplyDeleteThanks for this post. Exactly what I was looking for.
ReplyDeleteAfter installing NUnit and attempting to open your solution, you may find that attempting to run your tests returns an exception:
ReplyDeleteCould not load file or assembly 'nunit.framework, Version=2.4.8.0, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77' or one of its dependencies. The system cannot find the file specified**
If you see this, open Visual Studio Command Prompt and add the nunit.framework.dll file to your GAC manually with a line like the following:
gacutil -/i "[pathOfYourFile]nunit.framework.dll"
This will manually install the dll to your GAC and allow you to run Nunit tests. I hope this helps someone else who gets stuck on this problem the way I did!
Hi,
ReplyDeletei tried the above code into the visual studio 2010. Its showing visual studio cannot start debugging error
Hi Nadim,
ReplyDeleteNice post. But I face issue where in the Element is still not displayed to act upon. Hence it throws NoSuchElementException.How do i hadle that scenario? I mean the NUnit result file says-the test Passed even though Exception thrown, this is because all NUnit cares about is Assert Pass/Fail.I saw you mention-
bool IsElementPresent(By by). Not sure, how & where its called?
Thank you Chandas.
DeleteI think element is not displayed in this case. you can use Wait for this purpose. Please this post : http://nadimsaker.blogspot.com/2014/08/how-to-use-implicit-and-explicit-wait.html