HOWTO: Launch IPhone Simulator with Selenium

If you are trying to test a website on an IPhone Simulator, most likely you are using Selenium's WebDriver. If you are, I'm assuming that you've already compiled the project on your machine and saw that it launches IPhone Simulator with WebDriver app running on it. If you haven't done that yet and need to know how to do that, read this page as it tells you exactly what to do!

Once I got that project compiled and ran some of my existing tests on the simulator, I thought that it would be a great idea to have my JUnit class setup method launch the simulator for me and initialize it for testing. As I was doing some research on the topic, I stumbled upon the following class: IPhoneSimulatorBinary. Once you instantiate this object, you can launch your simulator. Here's the code I used to accomplish this task:

private static IPhoneSimulatorBinary _sim;
@BeforeClass
public static void classSetup() {
  try {
    String pathToWebDriverApp = "/path/to/iWebDriver.app/iWebDriver"; // this path is the XCode output of the iWebDriver project
    File iWebDriverApp = new File(pathToWebDriverApp);
    _sim = new IPhoneSimulatorBinary(iWebDriverApp);
    if (!_sim.isRunning()) {
      _sim.launch();
    }
  } catch (Exception e) {
    _sim = null;
    e.printStackTrace();
    Assert.fail(e.getMessage());
  }
}

Of course, after you are done with your tests, you will want to shut down your simulator. You can do that by running your tear down method which should look something like this:

@AfterClass
public static void classTearDown() {
  if ((_sim != null) && (_sim.isRunning())){
    _sim.shutdown();
  }
}

This code does require some imports:

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.openqa.selenium.iphone.IPhoneSimulatorBinary;

If you are lucky, everything should just run! I, however was not that lucky. The "_sim.launch" line would always fail for me with a very annoying message "Process exited with an error: 133 (Exit value: 133)". After some research, it turns out that XCode 4.3 changed change one of the directories and it is no longer located at "/Developer". Doing further research, I stumbled upon a blog that showed how to "fix" this issue. Turns out all you have to do is run the following commands and you're done!

sudo ln -s /Applications/Xcode.app/Contents/Developer/ /Developer
sudo ln -s /Applications/Xcode.app/Contents/OtherFrameworks/DevToolsCore.framework /Developer/Library/PrivateFrameworks/
sudo ln -s /Applications/Xcode.app/Contents/OtherFrameworks/DevToolsCParsing.framework /Developer/Library/PrivateFrameworks/
sudo ln -s /Applications/Xcode.app/Contents/OtherFrameworks/DevToolsFoundation.framework /Developer/Library/PrivateFrameworks/
sudo ln -s /Applications/Xcode.app/Contents/OtherFrameworks/DevToolsInterface.framework /Developer/Library/PrivateFrameworks/
sudo ln -s /Applications/Xcode.app/Contents/OtherFrameworks/DevToolsKit.framework /Developer/Library/PrivateFrameworks/
sudo ln -s /Applications/Xcode.app/Contents/OtherFrameworks/DevToolsRemoteClient.framework /Developer/Library/PrivateFrameworks/
sudo ln -s /Applications/Xcode.app/Contents/OtherFrameworks/DevToolsSupport.framework /Developer/Library/PrivateFrameworks/

Happy coding!

Comments