A Mac OS X Web Browser for JavaScript Testing
Posted: October 2nd, 2005 Tags: Javascript, My SoftwareI was playing with a very JavaScript-heavy web page and wanted my automated tests to use a real web browser. I tried running them inside Safari, but it’s a bit annoying to have my tests take control of my browser. Plus, there were caching issues. So I wrote a very simple web browser with the following features:
- It uses WebKit so it works just like Safari
- It doesn’t do any caching
- It takes a URL command-line parameter and loads it
- It quits immediately [update: I added a noquit paramter in case you don't want it to quit immediately]
It currently has the following problems:
- It’s uses AppKit, so it has a UI. I really wanted it to be a command-line-only app, but I haven’t yet figured out how to do that
- Invoking it is painful: /Applications/wkget.app/Contents/MacOS/wkget -url http://www.apple.com
- It only tests WebKit; one day I might make a version that uses Firefox’s JavaScript engine
I called it wkget and you can download it here. [Update: I had an incorrectly-built version up here for a while. It's fixed now.]
Here’s the code (sorry for the formatting; I had to wrap everything for this very narrow column):
@implementation WkgetController
- (void)awakeFromNib {
[self gotoUrl:self];
[webView setFrameLoadDelegate:self];
}
- (IBAction)gotoUrl:(id)sender {
NSString *url = [[NSUserDefaults standardUserDefaults]
stringForKey:@"url"];
if (url == nil) {
url = @"http://www.apple.com/";
}
NSURLRequest *request = [NSURLRequest
requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:1.0];
[[webView mainFrame] loadRequest:request];
}
- (void)webView:(WebView *)sender
didFinishLoadForFrame:(WebFrame *)frame {
if (![[NSUserDefaults standardUserDefaults]
boolForKey:@"noquit"]) {
[NSApp terminate:self];
}
}
@end
October 6th, 2005 at 11:56 pm
If you get the open source version of WebKit, it includes a DumpRenderTree tool which runs as command-line (loads a web page in an offscreen WebView, dumps the results) and testkjs which loads a JavaScript file and executes it in a non-web-page context. It might make for a helpful example.
October 7th, 2005 at 8:37 am
I’ll check it out. Thanks!