Comprehensive Selenium integration in Hudson with Selunit

This example tells you how to integrate Hudson with Selunit. Selunit can be used to execute Selenium tests directly from Selenium IDE in Hudson builds and using Hudson reporting capabilities provided for JUnit, where Selunit transforms Selenium reports to. The goal is to point out a comprehensive solution beginning with a convenient way creating and maintaining tests using Sel IDE, running them concerning multiple browsers/environments and finally monitoring and analyzing reports in Hudson.

All resources used in this tutorial are focused on examples for testing a travel community portal reiselichter.de (“travel lights” in English).

For more information go to http://www.selunit.org

This Article is Submitted by Michael Blank

WebDriver 5 Minute Getting Started Guide

WebDriver is a tool for automating testing web applications, and in particular to verify that they work as expected. It aims to provide a friendly API that’s easy to explore and understand, which will help make your tests easier to read and maintain. It’s not tied to any particular test framework, so it can be used equally well with JUnit, TestNG or from a plain old “main” method. This “Getting Started” guide introduces you to WebDriver’s Java API and helps get you started becoming familiar with it.

Start by Downloading the latest binaries and unpack them into a directory. From now on, we’ll refer to that as $WEBDRIVER_HOME. Now, open your favourite IDE and:
• Start a new Java project in your favourite IDE
• Add all the JAR files under $WEBDRIVER_HOME to the CLASSPATH
You can see that WebDriver acts just as a normal Java library does: it’s entirely self-contained, and you don’t need to remember to start any additional processes or run any installers before using it. You’re now ready to write some code. An easy way to get started is this example, which searches for the term “Cheese” on Google and then outputs the result page’s title to the console. You’ll start by using the HtmlUnit Driver. This is a pure Java driver that runs entirely in-memory. Because of this, you won’t see a new browser window open.

package org.openqa.selenium.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class Example {
public static void main(String[] args) {
// Create a new instance of the html unit driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new HtmlUnitDriver();
// And now use this to visit Google
driver.get( "http://www.google.com" );
106 Chapter 10. Selenium 2.0 and WebDriver
Selenium Documentation, Release 1.0
// Find the text input element by its name
WebElement element = driver.findElement(By.name( "q" ));
// Enter something to search for
element.sendKeys( "Cheese!" );
// Now submit the form. WebDriver will find the form for us from the element
element.submit();
// Check the title of the page
System.out.println( "Page title is: " + driver.getTitle());
}
}

Compile and run this. You should see a line with the title of the Google search results as output on the console.

Congratulations, you’ve managed to get started with WebDriver!
In this next example, you shall use a page that requires Javascript to work properly, such as Google Suggest. You will also be using the Firefox Driver. Make sure that Firefox is installed on your machine and is in the normal location for your OS.
Once that’s done, create a new class called GoogleSuggest, which looks like:

package org.openqa.selenium.example;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.RenderedWebElement;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class GoogleSuggest {
public static void main(String[] args) throws Exception {
// The Firefox driver supports javascript
WebDriver driver = new FirefoxDriver();
// Go to the Google Suggest home page
driver.get( "http://www.google.com/webhp?complete=1&hl=en" );
// Enter the query string "Cheese"
WebElement query = driver.findElement(By.name( "q" ));
query.sendKeys( "Cheese" );
// Sleep until the div we want is visible or 5 seconds is over
long end = System.currentTimeMillis() + 5000;
while (System.currentTimeMillis() < end) { // Browsers which render content (such as Firefox and IE) // return "RenderedWebElements" RenderedWebElement resultsDiv = (RenderedWebElement) driver.findElement(By.className( // If results have been returned, // the results are displayed in a drop down. if (resultsDiv.isDisplayed()) { break; 10.3. The 5 Minute Getting Started Guide 107 Selenium Documentation, Release 1.0 } } // And now list the suggestions List allSuggestions = driver.findElements(By.xpath( "//td[@class=’gac_for (WebElement suggestion : allSuggestions) {
System.out.println(suggestion.getText());
}
}
}

When you run this program, you’ll see the list of suggestions being printed to the console. That’s all there is to using WebDriver!

Hopefully, this will have whet your appetite for more. In the Next Steps section you will learn more about how to use WebDriver for things such as navigating forward and backward in your browser’s history, and how to use frames and windows. It also provides a more complete discussion of the examples than The 5 Minute Getting Started Guide. If you’re ready, let’s take the Next Steps!

How did this solution work for you? Do you have a better alternative? Feel free to share your thoughts in comments section below.

Selenium RC with Java and JUnit

Here is an example of selenium script in Java and executed with NUnit. The results are saved in a text file.
[java]
package com.testscripts;
import java.io.*;
import com.thoughtworks.selenium.*;
public class NewTest extends SeleneseTestCase  {
// We create our Selenium test case
public void result(String results){
try {
FileWriter fstream = new FileWriter(“C:\\Results.txt”,true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(results+”\n”);
//Close the output stream
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void setUp() throws Exception {
setUp(“http://www.google.com/”, “*firefox”);
// We instantiate and start the browser
}
public void testNew() throws Exception {
selenium.open(“/”);
selenium.type(“q”, “selenium rc”);
selenium.click(“btnG”);
selenium.waitForPageToLoad(“30000”);
if (selenium.isElementPresent(“link=Selenium Remote-Control”))
{
verifyEquals(“Selenium Remote-Control”, selenium.getText(“link=Selenium Remote-Control”));
// These are the real test steps
result(“Selenium Remote-Control is Present”);
}
else
{ result(“Selenium Remote-Control is not Present”);}
}
}
[/java]
Submitted by Pavandeep Puddupakkam

Create a Java Selenium RC test script and executing the script

Hello All,

In this article I will explain you step-by-step process of how to run Selenium RC application in Java. Before we start writing the script you need to make you have the following installed on you computer.

1. Java JDK – you can download the latest from http://java.sun.com/javase/downloads/index.jsp

2. Selenium RC – you can download this from https://sourceforge.net/projects/selenium-rc/

3. Selenium IDE – it is a firefox plugin and you can download this from http://seleniumhq.org/download/

4. Junit – you can download this from https://sourceforge.net/projects/junit/files/junit/ – download junit4.7.zip

Install JDK in the following location.

C:\Program Files\Java\

Selenium RC unzip the file and save the files is the following location: C:\selenium-remote-control-1.0-SNAPSHOT

In the folder C:\selenium-remote-control-1.0-SNAPSHOT you will have the following folders:

1. selenium-dotnet-client-driver-1.0-SNAPSHOT

2. selenium-java-client-driver-1.0-SNAPSHOT

3. selenium-perl-client-driver-1.0-SNAPSHOT

4. selenium-php-client-driver-1.0-SNAPSHOT

5. selenium-python-client-driver-1.0-SNAPSHOT

6. selenium-ruby-client-driver-1.0-SNAPSHOT

7. selenium-server-1.0-SNAPSHOT

We will need the selenium-java-client-driver-1.0-SNAPSHOT and selenium-server-1.0-SNAPSHOT for running Selenium RC application in Java

Unzip Junit and save the files in the following location: C:\junit

In the folder c:\junit will have the folder junit4.7

Create a folder c:\selenium\tests to save all the scripts

Now that you have all the required software open selenium ide on your firefox browser and record a simple google search.

Your Selenium IDE screen should look this http://clearspace.openqa.org/servlet/JiveServlet/previewBody/1431-102-1-1174/GoogleSearch.jpg

[html]
<tr>
<td>open</td>
<td>/firefox?client=firefox-a&amp;rls=org.mozilla:en-US:official</td>
<td></td>
</tr>
<tr>
<td>type</td>
<td>sf</td>
<td>Selenium IDE</td>
</tr>
<tr>
<td>clickAndWait</td>
<td>btnG</td>
<td></td>
</tr>
<tr>
<td>verifyTextPresent</td>
<td>Selenium IDE</td>
<td></td>
</tr>
[/html]

Now export test case save as Java(Junit) – Selenium RC
File-> Export Test Case as..-> Java (Junit) – Selenium RC
Check the link File-> Export Test Case as..-> Java (Junit) – Selenium RC
Save the file as GoogleSearch.java in the folder c:\selenium\tests
Go to c:\selenium\tests folder
You will see GoogleSearch.java
Your GoogleSearch.java should look some thing
[java]
package com.example.tests;
import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;
public class simplesearch extends SeleneseTestCase {
public void setUp() throws Exception {
setUp(“http://www.google.com.au/”, “*chrome”);
}
public void testSimplesearch() throws Exception {
selenium.open(“/firefox?client=firefox-a&rls=org.mozilla:en-US:official”);
selenium.type(“sf”, “Selenium IDE”);
selenium.click(“btnG”);
selenium.waitForPageToLoad(“30000”);
verifyTrue(selenium.isTextPresent(“Selenium IDE”));
}
}

[/java]

Now go to command prompt

Go to the folder c:\selenium\tests

Type the following command and press enter
[bash]
set
CLASSPATH=%CLASSPATH%;C:\junit\junit4.7\junit4.7\junit-4.7.jar;C:\selenium-remote-control-1.0-SNAPSHOT\selenium-java-client-driver-1.0-SNAPSHOT\selenium-java-client-driver.jar;C:\selenium-remote-control-1.0-SNAPSHOT\selenium-server-1.0-SNAPSHOT\selenium-server.jar;C:\Program
Files\Java\jdk1.5.0_16\bin\javac.jar;C:\Program
Files\Java\jdk1.5.0_16\lib\tools.jar;
[/bash]

You are have sucessfully set the classpath
Now type the following command and press enter
path=C:\Program Files\Java\jdk1.5.0_16\bin\

You will have to now change the GoogleSearch.java file for it to get complied

[java]
//package com.example.tests;

import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;
import junit.framework.*; //added
public class GoogleSearch extends SeleneseTestCase {
public void setUp() throws Exception {
setUp(“http://www.google.com.au/”, “*chrome”);

}
public void testSimplesearch() throws Exception {
selenium.setTimeout(“1000000”);
// this is used when you get the error message
thoughtworks.selenium.SeleniumException: Timed out after 30000ms

selenium.open(“/”);
selenium.type(“sf”, “Selenium IDE”);
selenium.click(“btnG”);
selenium.waitForPageToLoad(“30000”);
verifyTrue(selenium.isTextPresent(“Selenium IDE”));
}
public static Test suite() {
//method added
return new TestSuite(GoogleSearch.class);
}
public void tearDown(){
//Added . Will be called when the test will complete
selenium.stop();
}
public static void main(String args[]) {
// Added. Execution will started from here.
junit.textui.TestRunner.run(suite());
}
}

[/java]

After making changes to the GoogleSearch.java save the file.
Now lets compile the java file
In your cmd prompt execute the following command
javac GoogleSreach.java
c:\selenium\tests\javac GoogleSreach.java
Your class file will be saved in c:\selenium\tests\
Run the TestCase
Now before you run the test cases you need to start the Selenium server. To start the server go to the command prompt and run the following command
java -jar C:\selenium-remote-control-1.0-SNAPSHOT\selenium-server-1.0-SNAPSHOT\selenium-server.jar -interactive
In the other command prompt (c:\selenium\tests\) and type the following command
java GoogleSearch

If the test passes you will get the following result.
Time: 102.172
OK (1 test)

It lot of cases you will have more than one test script to run; here is an example of how to run more than one script using a batch file and saving the Test Results

To create a batch file open notepad and type the following text:
[bash]
cd\
set
CLASSPATH=%CLASSPATH%;C:\junit\junit4.7\junit4.7\junit-4.7.jar;C:\selenium-remote-control-1.0-SNAPSHOT\selenium-java-client-driver-1.0-SNAPSHOT\selenium-java-client-driver.jar;C:\selenium-remote-control-1.0-SNAPSHOT\selenium-server-1.0-SNAPSHOT\selenium-server.jar;C:\Program
Files\Java\jdk1.5.0_16\bin;C:\Program
Files\Java\jdk1.5.0_16\lib\tools.jar;
path=C:\Program Files\Java\jdk1.5.0_16\bin\
cd C:\selenium\Tests
java GoogleSearch >c:\TestResults.txt
java GoogleSrearch1 >>c:\TestResults.txt
[/bash]

In this there are two scripts that I am running GoogleSearch and GoogleSearch1. I am saving the results in  TestResults.txt. >> will append the TestResults.txt file and you will not lose the old test results. Once executed the TestResults.txt will have all the test results.

The most common errors that you will encounter are:

java:3: package com.thoughtworks.selenium does not exist

import com.thoughtworks.selenium.*;

package junit.framework does not exist

import junit.framework.*; //added

GoogleSearch.java:14: cannot find symbol

symbol  : variable selenium

location: class GoogleSearch
selenium.type(“q”, “selenium IDE”);

The solution for the errors are
[bash]

set
CLASSPATH=%CLASSPATH%;C:\junit\junit4.7\junit4.7\junit-4.7.jar;C:\selenium-remote-control-1.0-SNAPSHOT\selenium-java-client-driver-1.0-SNAPSHOT\selenium-java-client-driver.jar;C:\selenium-remote-control-1.0-SNAPSHOT\selenium-server-1.0-SNAPSHOT\selenium-server.jar;C:\Program
Files\Java\jdk1.5.0_16\bin;C:\Program
Files\Java\jdk1.5.0_16\lib\tools.jar;

[/bash]

C:\selenium\Tests>java googlesearch
Exception in thread “main” java.lang.NoClassDefFoundError: googlesearch
The issue can be resolved by typing java GoogleSearch
Always make sure you have set the JDK path correctly path=C:\Program Files\Java\jdk1.5.0_16\bin\

Cheers

Pavandeep Puddupakkam