Salesforce1 Mobile Automation Testing Adventures – Putting It All Together!

49125506In Part 3, I showed how you can automate typical CRUD flows in any hybrid or native app built using the Salesforce1 Mobile SDK. As I mentioned earlier as well, it’s good to be able to create UI tests with literally point and click capabilities using Appium, but it’s a far fetched reality from achieving end to end automation testing.

So, let’s now solve the last and the most interesting part of the jigsaw puzzle and write some code to build our UI automation framework.

Record

When you are creating any scripts, there’s a ‘Record’ button which actually captures all your actions – taps, keys entered, on focus, off focus, literally everything. If you look closely, you actually see what code it is generating for each action that you perform in the bottom window.

Screen Shot 2015-02-11 at 6.13.23 pm

Appium supports multiple languages for producing such test script code including

  • Java
  • Node
  • Perl
  • PHP
  • Ruby
  • Python
  • .NET

Since I’ve worked with Selenium earlier and used Java to create automation tests for web application, I decided to go the Java route. And giving credit where due, I followed this very informative tutorial to modify some provided examples to write my own scripts for Mobile SDK sample apps.

First things first

Like with Selenium, the first step is to create a RemoteWebDriver and specify various capabilities such as

  • Device – iPhone/ Android
  • UUID – To be set only in case of iPhone when using a real device
  • Bundle ID – Bundle id of your app

I do this in my setup method as shown below.


@BeforeMethod
public void setUp() throws Exception {
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Jdk14Logger");
// set up appium
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName","iOS");
capabilities.setCapability("platformVersion","7.1");
capabilities.setCapability("deviceName", "iPhone Simulator");

Specify App Location

Next up, Appium needs what application and from where it needs to load. This is done as follows.


 File appDir,app;

//Specify directory
 appDir = new File( "/Users/gaurav/Desktop/AppFiles");

//Specify name of app file in that directory
 app = new File(appDir,"AccountEditor.app");

//Set app absolute path
capabilities.setCapability("app", app.getAbsolutePath());

// Load remote driver with server address and capabilites
 driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

// Get a session id
 sessionId = ((RemoteWebDriver) driver).getSessionId().toString();
 driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);

Now the real thing!

Once the remote driver session has been established, the real magic begins. And to be honest, I found this part to be the most amazing. Each action that you perform on the Appium Inspector equates to one click() or sendKeys() call.


try {

// Find the username field in oAuth webview
driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIAScrollView[1]/UIAWebView[1]/UIATextField[1]")).click();

// Enter the username value in oAuth webview
driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIAScrollView[1]/UIAWebView[1]/UIATextField[1]")).sendKeys("dreamforce-demo@appium.com");
// Find the password field in oAuth webview
driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIAScrollView[1]/UIAWebView[1]/UIASecureTextField[1]")).click();

//Enter the password field in the oAuth webview
driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIAScrollView[1]/UIAWebView[1]/UIASecureTextField[1]")).sendKeys("test1234");

// Find the Login button and click it
driver.findElement(By.name("Log in to Salesforce")).click();

// Find the allow button on permissions screen and click it
driver.findElement(By.name(" Allow ")).click();
// We're now on our app home page, let's create a new account
driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIAScrollView[1]/UIAWebView[1]/UIALink[1]")).click();

// Find the account name field and get focus
driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIAScrollView[1]/UIAWebView[1]/UIATextField[2]")).click();

// Enter the account name
driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIAScrollView[1]/UIAWebView[1]/UIATextField[2]")).sendKeys("NewAccount");

//Find the Industry field and get focus
driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIAScrollView[1]/UIAWebView[1]/UIATextField[3]")).click();

//Enter the industry value
driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIAScrollView[1]/UIAWebView[1]/UIATextField[3]")).sendKeys("CRM");

//Find the phone field and get focus
driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIAScrollView[1]/UIAWebView[1]/UIATextField[4]")).click();

// Enter the pone number
driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIAScrollView[1]/UIAWebView[1]/UIATextField[4]")).sendKeys("1243534664");

// We're done, find 'Save' button and click it
driver.findElement(By.name("Save")).click();
Thread.sleep(10000);
// All done
}

There you have it – while manually testing each such operation is a pain in the a**, you can record scripts for such key flows, put them into a Maven project and run it as part of your continous integration cycle, thereby helping achieve end to end automation.

If you’ve followed the earlier tutorials closely, hopefully it all makes sense!

Caveats

While having such tests reduce manual testing efforts, I want to stress on a few things here

  • Appium can test across multiple devices with varying screen sizes/ resolutions, but it can’t really catch any “User Experience” issues which can best be judged by the human eye.
  • I’ve found the framework flaky at times. The Inspector would crash sometimes and often, the Inspector window won’t refresh itself manually. The framework is still maturing so I believe that’s kind of expected.

I created scripts for the following sample apps that are bundled with Salesforce1 Mobile SDK

  • AccountEditor (iOS hybrid)
  • ContactExplorer (iOS hybrid)
  • FileExplorer (iOS Native)
  • RestAPIExplorer (iOS Native)

The scripts are available on my Github repo. If you face any issues understand or running the scripts, drop me a line.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s