Saturday, September 8, 2012

How to handle popup window and run test suite in Selenium webdriver.

Hi There are three .java files and one build.xml file.

package com.org;

import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;

import junit.framework.TestCase;

public class TypeGoogle extends TestCase{
    
    private WebDriver driver;
    private String baseUrl;
    private StringBuffer verificationErrors = new StringBuffer();
    
    @Before
    public void setUp() throws Exception {
        driver = new FirefoxDriver();
        baseUrl = "http://www.google.co.in";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test
    public void testFileTypeGoogle() throws Exception {
        
        driver.get(baseUrl + "/");
        driver.findElement(By.xpath("//div[@id='gs_lc0']/input")).clear();
        driver.findElement(By.xpath("//div[@id='gs_lc0']/input")).sendKeys("google");
         
            WebElement google = driver.findElement(By.className("gssb_m"));
            List<WebElement> googleBox = google.findElements(By.tagName("span"));
            Iterator<WebElement> i = googleBox.iterator();
            while(i.hasNext()) {
                    WebElement sel = i.next();
                    System.out.println(sel.getText());
            }
      }

    @After
    public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
      }
    }

=====================================================================


package com.org;

import java.util.concurrent.TimeUnit;

import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import junit.framework.TestCase;

public class AddGadget extends TestCase{
    
    private WebDriver driver;
    private String baseUrl;
    private StringBuffer verificationErrors = new StringBuffer();
    @Before
    public void setUp() throws Exception {
        driver = new FirefoxDriver();
        baseUrl = "http://www.google.com/";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

        @Test
        public void testFileAddGadget() throws Exception {
            driver.get(baseUrl + "ig");
            
            driver.findElement(By.xpath("//div[@class='popupContent']/div/a[@id='popup_dialog_close']")).click();
            driver.findElement(By.cssSelector("img.G-BJ.G-AJ")).click();
            driver.findElement(By.id("search_module_query_box")).clear();
            driver.findElement(By.id("search_module_query_box")).sendKeys("news");
            driver.findElement(By.id("search_module_submit_button")).click();
            driver.findElement(By.id("featured_gadget_link")).click();
            driver.findElement(By.cssSelector("#addbtn > input.kd-button")).click();
            driver.findElement(By.id("back_to_gallery_link")).click();
            driver.findElement(By.cssSelector("input.kd-button")).click();
            driver.findElement(By.cssSelector("span.G-PI")).click();
            driver.findElement(By.id("tdd_edit")).click();
            driver.findElement(By.linkText("Delete")).click();
            driver.switchTo().alert().accept();
            Thread.sleep(5000);
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        }

        @After
        public void tearDown() throws Exception {
            driver.quit();
            String verificationErrorString = verificationErrors.toString();
            if (!"".equals(verificationErrorString)) {
                fail(verificationErrorString);
            }
        }
    }

=====================================================================

package com.org;

    import junit.framework.Test;
    import junit.framework.TestSuite;
    import org.junit.runner.RunWith;
    import org.junit.runners.Suite;
    import org.junit.runners.Suite.SuiteClasses;

    @RunWith(Suite.class)
    @SuiteClasses(value = {AddGadget.class, TypeGoogle.class})

    public class IGTestSuite {

        public static Test suite() {
            
            TestSuite suite = new TestSuite();
            suite.addTestSuite(AddGadget.class);
            suite.addTestSuite(TypeGoogle.class);
           
            return suite;
        }

        public static void main(String[] args) {
            junit.textui.TestRunner.run(suite());
        }
    }

=====================================================================

<?xml version="1.0" encoding="UTF-8"?>

<project name="IGoogle" default="all" basedir=".">
    <description>
        Demo UI testing, using Selenium and JUnit
    </description>
  <!-- set global properties for this build -->
  <property name="project_name" value="Results"/>
  <property name="src" location="src"/>
  <property name="build" location="bin"/>
  <property name="dist"  location="dist"/>
  <property name="lib"  location="lib"/>
  <property name="res"  location="res"/>
  <property name="output" location="out"/>
  
    <!-- Set the Classpath -->
    <path id="classpath">
        <fileset dir="./lib/">
            <include name="*.jar"/>
        </fileset> 
        <pathelement path="${build}"/>
    </path>
    
    <!-- top level targets -->
      <target name="compile" depends="init" description="compile the source code " >
        <javac srcdir="${src}" destdir="${build}" includeantruntime="false">  
        <classpath refid="classpath"></classpath>
        </javac>
      </target>

      <target name="clean"
            description="clean up" >
        <!-- Delete the ${build} and ${dist} directory trees -->
        <delete dir="${build}"/>
        <delete dir="${output}"/>
      </target>

        <!-- Selenium Test Case Start -->
        <target name="Start_Selenium_Server">
            <echo message="Start Selenium Server. Please wait.."/>
            <java jar="./lib/selenium-server.jar"
                    fork="true" spawn="true" >
                <classpath refid="classpath"></classpath>
                <!--  Set the proxy
                <arg value="-Dhttp.proxyHost=172.16.18.3"/>
                <arg value="-Dhttp.proxyPort=8080"/
                <arg line="-singleWindow"  />-->
                <arg line="-log ${output}/selenium.log"/>
            </java>
            <echo message="Started Selenium Server. "/>
        </target>    
    
        <target name="Stop-Selenium-Server">
                <echo message="Stop Selenium Server. Please wait.."/>
                <get taskname="selenium-shutdown"
                     src="http://localhost:4444/selenium-server/driver/?cmd=shutDownSeleniumServer
                     dest="${output}/server.stop.status.txt" 
                     ignoreerrors="true"/>
        </target>
    
        <target name="run-alltests" depends="compile" description="run all test suite" >
              <echo message="Test Run. Please wait.."/>
              <antcall target="Start_Selenium_Server"/>
            <junit printsummary="yes" haltonfailure="no" showoutput="yes" >
                <classpath refid="classpath"/>
               <batchtest fork="yes" todir="${output}/raw/">
                  <formatter type="xml"/>
                <fileset dir="${build}">
                 <filename name="com/org/IGTestSuite.class"/>
                </fileset>
              </batchtest>
            </junit>    
              <antcall target="Stop-Selenium-Server"/>
        </target>
    
    
      <target name ="test" depends="run-alltests">
            <java classname="com/org/IGTestSuite.class"
                  fork="true">
                    <arg line="${output}/raw/ ${output}/SeleniumReport.xml"/>
                <classpath refid="classpath"/>
            </java>
              <junitreport todir="${output}">
              <fileset dir="${output}/raw/">
                <include name="TEST-*.xml"/>
              </fileset>
              <report format="frames" todir="${output}/html/"/>
            </junitreport>
      </target>

    <!-- supporting targets -->
    
     <target name="init" description="initialize the build environment" >
        <!-- Create the time stamp -->
        <tstamp/>
        <!-- Create directory structures -->
        <mkdir dir="${build}"/>
        <mkdir dir="${output}"/>
        <mkdir dir="${output}/raw/"/>
        <mkdir dir="${output}/html/"/>
      </target>
  
      <target name="all" depends="clean, test">
      </target>
  
</project>

=====================================================================

Tuesday, June 12, 2012

Selenium RC Server can be started with the mentioned options.



The procedure followed to run the Selenium RC Server is:

1. Open the command prompt.
2. Change the folder path to Selenium RC Server.
3. Issue the command "java -jar selenium-server.jar".
4. For more options on how to start selenium server.


Now in brief description:


-port the port number the selenium server should use (default 4444)
-timeout an integer number of seconds before we should give up
-interactive: puts you into interactive mode. See the tutorial for more details
-singleWindow: puts you into a mode where the test web site executes in a frame. This mode should only be selected if the applicationunder test does not use frames.
-profilesLocation: Specifies the directory that holds the profiles that java clients can use to start up selenium. Currently supported for Firefox only.
-forcedBrowserMode sets the browser mode to a single argument (e.g. "*iexplore") for all sessionsno matter what is passed to getNewBrowserSession
-forcedBrowserModeRestOfLine sets the browser mode to all the remaining tokens on the line (e.g. "*custom /some/random/place/iexplore.exe") for all sessions, no matter what is passed to getNewBrowserSession
-userExtensions indicates a JavaScript file that will be loaded into selenium
-browserSessionReuse: stops re-initialization and spawning of the browser between tests
-avoidProxy: By default, we proxy every browser request; set this flag to make the browser use our proxy only for URLs containing '/selenium-server'
-firefoxProfileTemplate <dir>: normally, we generate a fresh empty Firefox profile every time we launch. You can specify a directory to make us copy your profile directory instead.
-debug: puts you into debug mode, with more trace information and diagnostics on the console
-browserSideLog: enables logging on the browser side; logging messages will be transmitted to the server. This can affect performance.
-ensureCleanSession: If the browser does not have user profiles, make sure every new session has no artifacts from previous sessions. For example, enabling this option will cause all user cookies to be archived before launching IE, and restored after IE is closed.
-trustAllSSLCertificates: Forces the Selenium proxy to trust all SSL certificates. This doesn't work in browsers that don't use the Selenium proxy.
-log writes lots of debug information out to a log file
-htmlSuite Run a single HTML Selenese (Selenium Core) suite and then exit immediately, using the specified browser (e.g. "*firefox") on the specified URL (e.g. http://www.google.com"). You need to specify the absolute path to the HTML test suite as well as the path to the HTML results file we'll generate.
-proxyInjectionMode: puts you into proxy injection mode, a mode where the selenium server acts as a proxy server for all content going to the test application. Under this mode, multiple domains can be visited, and the following additional flags are supported:
-dontInjectRegex an optional regular expression that proxy injection mode can use to know when to bypss injection
-userJsInjection specifies a JavaScript file which will then be injected into all pages
-userContentTransformation a regular expression which is matched against all test HTML content; the second is a string which will replace matches. These flags can be used any number of times. A example of how this could be useful: if you add "-userContentTransformation https http" then all "https" strings in the HTML of the test application will be changed to be "http".

Thursday, February 16, 2012

Integrate Selenium tool in AutoIt.

Testing Tool - BlueDuck SDA

Description:
BlueDuck SDA is an Autoit Driver for Selenium. Combining automation for Win32 and Web in a single script. The perfect combination for developing automated cases, AutoIt and Selenium together. Features: -> Supports your web app no matter what it is developed in. -> Easy to learn BASIC-like syntax -> Interacts with all standard windows controls -> Integration with Testlink 1.9.1 -> DataProvider -> Object Repository -> and more...
Requirement:
Windows XP - Vista - Seven

Problem

Hi Guys,

I am having some problem regarding behavior driven development to desktop based application. If you have any framework for the BDD to desktop application then please send me the information ...

Thanks in advance :
Prashant Chauhan

Saturday, January 21, 2012

Selenium 2.0 or Selenium Web Driver

Selenium Web Driver Features:
The primary new feature is the integration of the WebDriver API. This addresses a number of limitations along with providing an alternative, and simpler, programming interface. The goal is to develop an object-oriented API that provides additional support for a larger number of browsers along with improved support for modern advanced web-app testing problems.
  • WebDriver API
  • Selenium Server not needed
  • WebDriver is the name of the key interface against which tests should be written, but there are several implementations.

The Selenium Server – When to Use It:

You may, or may not, need the Selenium Server, depending on how you intend to use Selenium. If you will be strictly using the WebDriver API you do not necessarily need the Selenium Server. Selenium-WebDriver makes direct calls to the browser using each browser’s native support for automation. How these direct calls are made, depends on the browser you are using. See the information for each browser implementation below.
Some reason why you will need the Selenium-Server:
  • You are using Selenium-Grid to distribute your tests over many machines / VMs.
  • You want to connect to a remote machine that has a particular browser version that is not on your current machine.
  • You are not using the Java bindings and would like to use HtmlUnit Driver 
Required Things:
  • FireFox advance version Ex. 9.0.1
  • Selenium IDE Ex. 1.5.0
  • Firebug Ex. 1.9.0
  • Some JAR files:
  1. selenium-server-standalone-2.15.0.jar
  2. selenium-java-2.15.0.jar
  • Eclipse IDE
  • JUnit 4 instead of JUnit 3
  • ANT and build.xml file  

Getting Started With Selenium-WebDriver:

WebDriver is a tool for automating web application testing, and in particular to verify that they work as expected. It aims to provide a friendly API that’s easy to explore and understand, easier to use than the Selenium-RC (1.0) API, which will help to make your tests easier to read and maintain. It’s not tied to any particular test framework, so it can be used equally well in a unit testing or from a plain old “main” method.