Using different datasets for testing CoreData

I found that making use of compiler pre-process directives and bundling some test databases into your application (and using NSFileManager to copy the files across) makes testing with real data a breeze.

What I’ve done first is I have taken a copy of the iPhone database (Do a find . -name “ApplicationName” in the backup directory for your application OR if jailbroken do it in /var/mobile/Applications). Then you look for any sqlite files generated and now you can use this as a baseline test.

Next, create a folder in the xcode project (better organization!) called “QA” and then set up a target called “Test”, under the pre-processor directives add _TEST to this.

And finally the code

#if _TEST
	NSLog(@"Application in Test Mode %@", [[NSBundle mainBundle] pathForResource:@"AppDB" ofType:@"sqlite" inDirectory:@""]);
	NSLog(@"DEST: %@", [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"AppDB_Test.sqlite"]);
	if ([[NSFileManager defaultManager] copyItemAtPath:[[NSBundle mainBundle] pathForResource:@"AppDB" ofType:@"sqlite" inDirectory:@""] toPath:[[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"AppDB_Test.sqlite"] error:nil]) {
		NSLog(@"Copied DATABASE");
	} else {
		NSLog(@"Could not copy database");
	}
    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"AppDB_Test.sqlite"]];
#else
    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"AppDB.sqlite"]];
#endif
Bookmark and Share

Tags: ,

WebDriver Framework in Ruby

Well as a little experiment, I decided to write a simple navigational automated test that takes the JAVA WebDriver libraries but manipulates the objects in Ruby (which goes through all the links on my mobile site @ http://barryteoh.com/). This uses the iPhone Driver as well, so yes I actually wrote an automated test which drives the iPhone (it is actually pretty cool to watch!).

If I can somehow get it posted to YouTube I will do this. (Edit: Youtube video is here)

Anyway, the code is up on GitHub which you can also clone using the GIT repo git://gist.github.com/294392.git

Oh and hey…. I even noticed it runs while the iPhone is locked (something which most commercial tools fails to do!)

Bookmark and Share

Tags: ,

The Benefits of Multithreaded QA Automation

Just noted that writing an automated test that is multi-threaded is a good and very cost effective way of picking up race condition type bugs in an application – especially when there is dependancies between 2 system states.

Of course this will be picked up when doing performance testing, but the earlier a bug is found the better (and cheaper) right?

Bookmark and Share

Tags: ,

Distributing apps between other iPhone developers

It has been around for a while, but I found a pretty easy to explain tutorial on how you can give an iPhone app to another registered iPhone developer (without giving them the source) AND not using an adhoc distribution profile (which personally I think is a huge brainf$@k and very limiting).

To sum it up all it involves is resigning the binary with your own certificate. Hopefully this will not work on apps on the AppStore (not that it matters anyway as most of the apps get cracked within minutes/seconds anyway).

Anyway the link is here. Enjoy!

Bookmark and Share

Tags: ,

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).

Bookmark and Share

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
Bookmark and Share

Tags: , ,

Google Wave Tips

Ever find that google wave is very intimidating? Well there is a wave where you can find out how to use it.

Simply enter in:

id:”googlewave.com!w+Hdq5vmpeF”

into the search bar and follow the wave which has a very comprehensive guide.

Easy peasy!

Bookmark and Share

Tags:

Grand Central Dispatch – Threading done easily

Been doing a lot of mucking around with multi-threading lately (especially the new feature in Snow Leopard).

Using Grand Central dispatch is actually pretty easy.

First of all one needs to #import the header file.

Next they should put the processor intensive code within a block.

For example:

dispatch_async(dispatch_get_global_queue(0,0), ^{
// Code goes here
});
Bookmark and Share

Tags: , ,

Multithreaded Automated UI Testing

Well my latest bit of personal development in the IT world is multithreaded development using scripting languages (as I don’t like waiting for stuff to compile).

Using multithreading as far as UI testing can goes means you can cover a lot in less time.

Where I’m looking at is Python (due to it’s integration with Quality Center) as well as Ruby (because I like the syntax and it offers a language which interacts with the JVM).

An area of interest is probably Jython. If I can get that integrated with a Quality Center VAPI-XP test, then I almost got the best of both worlds.

Bookmark and Share

Tags: , , ,

JDBC/log4j and JRuby

Have been experimental with various Java technologies (At this time, JDBC, reading from properties files and log4j) and tying it all together with JRuby – Which I might add runs also in compiled mode as well.

Simple use of properties files in JRuby involves

# Initialize the properties object from java.util
# Refer to http://java.sun.com/j2se/1.4.2/docs/api/java/util/Properties.html
# on how to use.
properties = java.util::Properties.new
# Make sure that propertyfile exists!
properties.load(java.io.FileInputStream.new("propertyfile.properties"))
puts properties.getProperty("propertykey")

Now for some jdbc action! (The below code is well commented.. Enjoy!)

require 'java' # So we can use the sexy java goodness!
# Make sure you download this from the mysql site! (Link: http://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-3.1.10.zip/from/pick)
require 'mysql-connector-java-3.1.10-bin.jar'
import 'com.mysql.jdbc.Driver' # Load the JDBC Driver

# Replace 'conn_string' with the JDBC Connection string (e.g. jdbc:mysql://localhost:3306/DBNAME)
# Replace 'uid' with the database username
# Replace 'pwd' with the database password
conn = java.sql::DriverManager.getConnection(conn_string, uid, pwd);
# Prepared queries are teh win
stmt = conn.prepareStatement("select field from table where otherfield = ?")
stmt.setString(1, "Whatwearesearchingfor") # First param
stmt.executeQuery
while rs.next do
puts rs.getString("field")
end
stmt.close # Because we should close what we open
conn.close # Same as above

And finally, some log4j thrown in!

require 'log4j-1.2.15.jar' # Assume you have already done require java
# The below is how you use log4j controlled by XML
import org.apache.log4j.Logger
import org.apache.log4j.xml.DOMConfigurator
logger = Logger.getLogger("LoggerName") # LoggerName would be defined in the XML file
DOMConfigurator.configure("log4j.xml")
logger.info "This would log an INFO entry if it were configured to be shown"
# Additionally you can use debug, info, warn, error, fatal

The results have been to a good degree of success. All I will need now to create a kickass and open source web frontend testing framework is:

  • Celerity
  • log4j
  • JDBC
  • properties files

I’ve basically got 90% of the functionality of a commercial tool right here!  Schweet!

Bookmark and Share

Tags: , , , ,