Thursday 27 October 2011

Writing tests using Selenium WebDriver

using System;
using System.Text;
using System.Threading;
using OpenQA.Selenium;
using NUnit.Framework;
using OpenQA.Selenium.IE;     // For IE include this name space
using OpenQA.Selenium.Support.UI;   //To setup execution speed include this name space
namespace SeleniumWebdriverExample
{
[TestFixture]
public class WebDriverExample
{
IWebDriver driver;

[SetUp]
public void SetupTest()
{
driver = new InternetExplorerDriver();
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));  //Time in Seconds

}
[TearDown]
public void TeardownTest()
{
driver.Quit();
}
[Test]
public void TheUntitledTest()
{

driver.Navigate().GoToUrl("http://www.google.co.in/");
IWebElement query = driver.FindElement(By.Name("q"));    // Identifying by name
query.SendKeys("Cheese");                            // Type string into Textbox
IWebElement btnclick = driver.FindElement(By.Name("btnG"));
btnclick.Click();                                              // Button Click
}

}
}

Tuesday 25 October 2011

Different types for identifying elements in Selenium RC


selenium.Click("link=Images");                 // Identify  link by using label
selenium.WaitForPageToLoad("30000");       // wait for page to load
Assert.AreEqual("Google Images", selenium.GetTitle());
selenium.Click("css=#gb_8 > span.gbts");          //Identifying by using css selector
selenium.WaitForPageToLoad("30000");
Assert.AreEqual("Google Maps", selenium.GetTitle());      // Verifying by using page title
selenium.Click("link=News");
selenium.WaitForPageToLoad("30000");
Assert.AreEqual("Google",selenium.GetTitle());
selenium.Click("xpath=//div[@id='als']//font[@id='addlang']/a[2]");  //  Using XPath

Run selenium tests in NUnit

Download latest version of NUnit from http://www.nunit.org/index.php?p=download

After NUnit installation Go to -->Start--->Programs-->NUnit-->Click on NUnit

NUnit GUI will open.

Go to File--> Open Project--> Browse for .dll for the above project-->click on open

On Left Side bar we will the Name Space , Class name of the project and Methods present in the project.
Click on Run button 

                         
We will get the Result like below screenshot. If all the steps executed succesfully then we will get Green color bar other wise Red color with the error messages. 


If we want to see any output messages Goto-->Text Output tab at the bottom.

Example to verify Search Results

using System;
using System.Text;
using NUnit.Framework;
using Selenium;

namespace GoogleExample
{
    [TestFixture]
    public class homepage
    {
       ISelenium selenium;
       [SetUp]
        //Steps to be executed before execution of all steps
       public void setUpTest()
       {
           selenium = new DefaultSelenium("localhost",4444,"iexplore","http://google.co.in");
               //Here 4444 means port no of selenium server, iexplore for IE 
           selenium.Start();                                   // To start Selenium server  
           selenium.SetSpeed("2000");                 //To Control the execution speed
          selenium.WindowMaximize();                //To Maximize Browser window
          
       }

        [TearDown]
        //Steps to be executed after execution of all steps
        public void tearDownTest()
        {
         selenium.Stop();

        }

        [Test]
        // Steps to be written to verify the Result Status
        public void ResultsStatus()
        {
            selenium.Open("/"); // open url
            selenium.Type("name=q", "Testing"); // Type text in Text Field
            selenium.Click("name=btnK");    //click on button with name


            //Verifying results status

       
            if ((selenium.IsElementPresent("id=resultStats"))) 
             {
                 string a = selenium.GetText("id=resultStats"); //Get the text Results output
                 Console.WriteLine(a);     //To display output in the console of NUnit GUI
             }
             else
            {
                Console.WriteLine("Results not found");    //To display output in the console of NUnit GUI
            }
         }
      }
}

We need to install NUnit to run the above example.

Monday 24 October 2011

Configuring Selenium RC with .Net Client Driver


.NET client Driver can be used with Microsoft Visual Studio. To configure it with Visual do as Following.
·         Launch Visual Studio and navigate to File > New > Project.

·         Select Visual C# > Class Library > Name your project > Click on OK button.

·         A Class (.cs) is created. Rename it as appropriate.

·         Under right hand pane of Solution Explorer right click on References > Add References.

·         Select following dll files - nmock.dll, nunit.core.dll, nunit.framework.dll,ThoughtWorks. Selenium.Core.dll, ThoughtWorks.Selenium.IntegrationTests.dll, ThoughtWorks.Selenium.UnitTests.dll and click on Ok button


Now Visual Studio is ready for writing Selenium Test Cases.

Wednesday 19 October 2011

Running Selenium RC server from Microsoft Visual studio

1)      Tools ---->External Tools
2)      External Tools Window opened
Fill the following values
Title: Selenium Server (Name appear in Tools Menu)
Command: C:\Program Files\Java\jdk1.5.0_22\bin\java.exe (java installed directory)
Arguments: -jar selenium-server.jar (To execute a jar file)
Initial directory: D:\My Files\Selenium\selenium-remote-control-1.0.3\selenium-server-1.0.3
(Path of the selenium-server.jar file)
Now Selenium Server appears in Tools menu. We can directly run the selenium server by click on it. We can watch the status of the selenium server details in output window.

Tuesday 18 October 2011

View Cookie in Internet Explorer 7 & 8

To view cookie information in Internet Explorer 7 and 8 versions.

Follow the below steps

1) Open the site(i.e Cookie information for which site you are searching)
2) Click F12
3) Click Cache Tab
4) Select View Cookie Information

Then you get the cookie information about the particular site or domain name like below format.

Cookie Information - http://book.theautomatedtester.co.uk/chapter8


NAME visitorCount
VALUE 2
DOMAIN book.theautomatedtester.co.uk
PATH /
EXPIRES 11/9/2011 6:31:48 PM