2012-09-28

Recording in Selenium IDE and Generating WebDriver Code

In this article, we are going to learn how to record steps in selenium IDE. We are going to see some basic path as well as some basic command descriptions.
Step 1 : Please Install IDE & Firefox (for reference you may see one of my previous posts)
Step 2 : Start Firefox
Step 3 : From menu, click tools, and then select Selenium IDE
clip_image002
Step 4 : By default, when IDE opened, it is recording mode( like , it is recording). In the example I am using www.kaz.com.bd this site for practice.
clip_image004
Step 4 : now, From browser go to www.kaz.com.bd
Step 5 : You get a static HTML site with 4 tabs “Home”, “Talents”,” Culture”,” Contact”.
clip_image006
Step 6: click on “Home”, then “Talents”,” Culture”,” Contact” accordingly and from stat menu bar, open selenium IDE UI. You will get those 4 steps listed
clip_image008
Step 7: Press stop button from Selenium IDE (right Upper corner Red Round Button)
Now, you have finished recording where steps are simple clicking tabs.
From the IDE, you can work on those steps like re-run.
Now, We will see how to get web driver Unit test codes (Selenium IDE supported some experimental codes)
Step 1 : To get the code, go to Option –> Options… and check the “Enable experimental features” and press ok
 clip_image010
Step 2: Now, After recording from IDE, click Options -> Format and get the list below
 clip_image012
Step 3: Select c#/NUnit /webDriver ( later on we will also get the JUnit 4 code also) and press OK on warning message( as this is experimental code, so it may be not very accurate).
Now you get the code
clip_image014
In C# Code we have
   1: using System;

   2: using System.Text;

   3: using System.Text.RegularExpressions;

   4: using System.Threading;

   5: using NUnit.Framework;

   6: using OpenQA.Selenium;

   7: using OpenQA.Selenium.Firefox;

   8: using OpenQA.Selenium.Support.UI;

   9:  

  10: namespace SeleniumTests

  11: {

  12:     [TestFixture]

  13:     public class Untitled

  14:     {

  15:         private IWebDriver driver;

  16:         private StringBuilder verificationErrors;

  17:         private string baseURL;

  18:         

  19:         [SetUp]

  20:         public void SetupTest()

  21:         {

  22:             driver = new FirefoxDriver();

  23:             baseURL = "http://www.kaz.com.bd/";

  24:             verificationErrors = new StringBuilder();

  25:         }

  26:         

  27:         [TearDown]

  28:         public void TeardownTest()

  29:         {

  30:             try

  31:             {

  32:                 driver.Quit();

  33:             }

  34:             catch (Exception)

  35:             {

  36:                 // Ignore errors if unable to close the browser

  37:             }

  38:             Assert.AreEqual("", verificationErrors.ToString());

  39:         }

  40:         

  41:         [Test]

  42:         public void TheUntitledTest()

  43:         {

  44:             driver.Navigate().GoToUrl(baseURL + "/");

  45:             driver.FindElement(By.LinkText("Home")).Click();

  46:             driver.FindElement(By.LinkText("Talents")).Click();

  47:             driver.FindElement(By.LinkText("Culture")).Click();

  48:             driver.FindElement(By.LinkText("Contact")).Click();

  49:         }

  50:         private bool IsElementPresent(By by)

  51:         {

  52:             try

  53:             {

  54:                 driver.FindElement(by);

  55:                 return true;

  56:             }

  57:             catch (NoSuchElementException)

  58:             {

  59:                 return false;

  60:             }

  61:         }

  62:     }

  63: }
Step 4 : So, After recording we are getting NUnit code here.
If we make an empty class type project in vs2010(see my previous post on how to create webdriver environment setup) and copy to class to a new empty class, we can run the webDriver Unit test case using NUnit GUI(after building Project)

If we choose the JAVA/JUnit4 webDriver then , we will get the following code
image

In JAVA code we get
   1: package com.example.tests;

   2: 

   3: import com.thoughtworks.selenium.Selenium;

   4: import org.openqa.selenium.*;

   5: import org.openqa.selenium.htmlunit.*;

   6: import org.openqa.selenium.firefox.*;

   7: import org.openqa.selenium.chrome.*;

   8: import org.openqa.selenium.ie.*;

   9: import org.junit.*;

  10: import static org.junit.Assert.*;

  11: 

  12: public class Untitled {

  13:  

  14:     WebDriver driver;

  15:     Selenium selenium;

  16:  

  17:     @Before

  18:     public void startSelenium() {

  19:         driver = new FirefoxDriver();

  20:         selenium = new WebDriverBackedSelenium(driver, "http://www.kaz.com.bd/");

  21:     }

  22:  

  23:     @After

  24:     public void stopSelenium() {

  25:         driver.close();

  26:     }

  27:  

  28:     @Test

  29:     public void testUntitled() {

  30:         selenium.open("/");

  31:         selenium.click("link=Home");

  32:         selenium.waitForPageToLoad("30000");

  33:         selenium.click("link=Talents");

  34:         selenium.waitForPageToLoad("30000");

  35:         selenium.click("link=Culture");

  36:         selenium.waitForPageToLoad("30000");

  37:         selenium.click("link=Contact");

  38:         selenium.waitForPageToLoad("30000");

  39:     }

  40:  

  41: }
If we make a JAVA project in Eclipse(see my previous post on how to create webdriver environment setup) and copy to class to a new empty class, we can run the webDriver Unit test case code Running as JUnit 4.0 test project. 

For detail about Selenium IDE, you can visit the link
Thanks for reading this….:)

This is shantonu's personal blog.Mail[shantonu_oxford@yahoo.com] to contact.

6 comments:

  1. Hey, nice site you have here! Keep up the excellent work!

    Selenium


    ReplyDelete
  2. Yes, I have started updating gradually

    ReplyDelete
  3. Hi, this is Yasmin from Chennai. I have read your blog. Its very informative and useful blog. You have done really great job. Keep update your blog. Thanks..
    Regards.
    Selenium Training in Chennai | Selenium Training in Chennai

    ReplyDelete
  4. Hi,
    Your website really useful for beginners. I am new to selenium and i want to learn basic to advanced concepts with real time examples. Can you please help me on this.

    Looking forward for your positive response...!!!

    Thanks

    ReplyDelete
  5. Hi,
    I have read your blog and it really useful for beginners and i am new to selenium and please help me to teach selenium with real time examples .

    Looking forward for your positive response...!!

    Thanks

    ReplyDelete
    Replies
    1. This was real example when it was created. try to follow same technique for modern sites.

      Delete