Showing posts with label selenium. Show all posts
Showing posts with label selenium. Show all posts

Tuesday, 1 November 2011

Writing Selenium Tests in Eclipse


First we download Selenium IDE form http://seleniumhq.org/download/. It is a firefox plugin. Click on install
How to install Selenium IDE into your Browser.
Go to http://seleniumhq.org/docs/02_selenium_ide.html#installing-the-ide.
After Installation Selnium IDE will appear in Tools Menu

Click on Record Button on the Right Side corner OR Go to Action---> Click Record

Goto Url and Navigate some steps . After Navigation complete Stop Record.

Then we will get the script in Table format. If you want to source file click on source button. Default HTML code will appear.

If we want to write tests using java or csharp . Go to Options-->Format--->Select format --->Click OK.

If languages are not appear in the Format menu. Go to Options-->Options-->Select Enable Experimental Features. Now, we got the language options.

Copy the Entire code into your Class  Do the following changes in ur script.
1. Change Package name (i.e where our script appears.).  In the previouse example my script contains in com folde. So include this package like package com;
2. Change Untitled Class name to your Class name.
3. Click on Run. 

We will get the below after screenshot

If test passed Green Bar will appear otherwise Red Bar will appear. Don't forget to run selenium server before your test execution.

If you want ot run selenium tests using TestNG , instead of creating JUnit test case create TestNG Class.
Get the script by using TestNG (Remote Control) Format in Selenium IDE. Don't forget to add TestNG Jar file to your project.

Configuring Selenium RC with Eclipse using JUnit

Open Eclipse IDE
1. Select File-->New-->Java Project. Screen will be appear like below screenshot
Enter Project  Name and Click on Finish. On Left Side Bar we will get SeleniumRC Example in Package Explorer

2. Expand Project --> Right Click on Src--->New-->Folder --> enter folder name -->Click Finish
  Like in below screen shot.
3.In Project Explorer we found one package symbol with given folder name. Right Click on that -->New-->Junit Test Case --->Select JUnit3 or JUnit4 Test -->Enter Class name ---->Click Finish.
Like in below screen shot
Expand Package-->One file name with .java extension will appear and scren will be appear like
Right click on Java File-->Got Build Path--> Configur Build Path-->Select Libriaries-->Click on Add External JARs-->Browse for Selenium Source files-->Select Jar files in seleniu-server folder and select jar files present in Selenium Set-up-->Selenium java-client driver

Click OK.

Now Eclipse is Successsfully Configured with Selenium RC. We can write selenium tests in Eclipse

 If JUnit refernce files are not added to your project download JUnit jar file then Add this Jar file like above.

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.