Archive for December, 2009

Selenium/Webdriver + iPhone webdriver in action

Well I’ve gotten the webdriver+iPhone webdriver working. It’s fairly straight forward (and drives the same way as driving a browser).

The only caveat is that you need to pay $99 a year for the iPhone Developers Program. But $99 is chump change compared to any other test tool licensing.

Here’s what I’ve done (JAVA Only).

First of all I’ve added all the selenium + the compiled iphone webdriver jar files to a directory.

Next, I have compiled the following code which basically does a simple google search (for demonstration purposes).

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.iphone.IPhoneDriver;
import java.util.Properties;

public class Test {
    public static void main(String[] args) throws Exception {
        Properties p = new java.util.Properties();
        p.load(new java.io.FileInputStream("url.properties"));
        String url = p.getProperty("com.bt.url");
        System.out.println("Connecting to iWebDriver on: " + url);
        WebDriver driver = new IPhoneDriver(url);
        driver.get(p.getProperty("com.bt.auturl"));
        WebElement element = driver.findElement(By.name("q"));
        element.sendKeys("Selenium");
        element.submit();
        // Simple output
        System.out.println("Page title is: " + driver.getTitle() + "\n");
        System.out.println("Page URL is: " + driver.getCurrentUrl() + "\n");
    }
}

Next, create a file called “url.properties” and add the following values.
com.bt.url
com.bt.auturl

The first key defines where iWebDriver sits. The second key defines the google URL.

Finally run the following commands (UNIX Only. I guess you can use cygwin32)

# UNIX based systems is a hell of a lot nicer for java because you don't need to type
# in all the jar files or update classpaths. But then again its my preference.

# Compiling it
javac -cp `find . -name "*.jar" | xargs | sed 's/ /:/g;s/.\///g;'`:. ClassSource.java
# Running it
java -cp `find . -name "*.jar" | xargs | sed 's/ /:/g;s/.\///g;'`:. ClassName

It will then run and produce some output telling you about the page. You will also see it navigate through the page on the iWebDriver application (It’s open source so you can even hack away at the code there to customize it to your liking).

Tags: , , , , ,

Automated Testing on the iPhone for web apps

Have been taking a look at the iPhoneDriver component for WebDriver/Selenium 2.0 (which connects to a UIWebView instance on the iPhone).

Some of the observations I’ve noted if you wish to go down that path:

  • The framework is pretty easy to set up
  • You will need the iPhone SDK and to be a registered iPhone developer (so you can create provisioning profiles for on device testing)
  • You need an iPhone for on device testing (duh). Haven’t tried on the simulator but the documentation says it works.
  • The framework is Java based so you can data drive your tests (i.e. Using JDBC to connect to a database)
  • You could use JUnit and probably cruise control and set up some sort of continuous build integration
  • WebDriver is loosely based on selenium by the looks of the API. The script appears not to need too much modification in order for it to work

Tags: , ,