Friday, 3 April 2015

Example code for running test on Safari browser - Appium/iPhone

This Piece of code tested on -
  • Appium -1.3.4 IOS -6 (8.1)
  •  Mac: Mavericks [10.9.5] 
  • Java -1.7 & 8 
  • TestNg 
  • WebDriver- 2.45
package com.sma8learning.mobile.browser;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import io.appium.java_client.ios.IOSDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;

/**
 A test to demonstrate the automation on Safari browser using Appium [the open source tool]
*/
public class TestGoogleSearch_SafariBrowser {
 WebDriver wd;

 @BeforeTest
 public void beforeTest() throws MalformedURLException { //set capabilities required
  DesiredCapabilities capabilities = new DesiredCapabilities();
  capabilities.setCapability("deviceName", "iPhone 6");
  capabilities.setCapability("platformName", "iOS");
  capabilities.setCapability("platformVersion", "8.1");
  capabilities.setCapability(CapabilityType.BROWSER_NAME, "safari");
  wd = new IOSDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);//instantiate driver
  wd.manage().timeouts().implicitlyWait( 30,TimeUnit.SECONDS);
 }

 @Test
 public void testSearchPage() throws InterruptedException {
  wd.get("https://www.google.co.in");
  WebElement serachField = wd.findElement(By.name("q"));
  serachField.sendKeys("Pooja Shah Selenium youtube");
  serachField.sendKeys(Keys.ENTER);
  System.out.println(wd.getCurrentUrl());
 }

 @AfterTest
 public void afterTest() {
  //wd.close();
 }
}

Problems with solutions

1. That URL did not map to a valid JSONWP resource
Solution:
the url is wrong, it should be well formed path "http://127.0.0.1:4723/wd/hub"

2.error: Failed to start an Appium session, err was: Error: Could not navigate to webview; there aren't any!
Hack:
set the platformversion capability properly-
ex. capabilities.setCapability("platformVersion", "8.1");

3. “fruitstrap quit unexpectedly” or unable to install SafariLauncher.app 
  Possible solution: it works perfectly on ios simulator on mavericks but running on real device on Yosemite there are issues <br/>
Remember!
with real device you need to separately add a udid in capabilities and SafariLauncher.app and use a proxy called ios-webkit-debug-proxy
group-discussion for details
appium-dicussion for details

4 Could not get the iOS SDK version because the Xcode version could not be determined
it generally happens when appium somehow can't find xcode or you don't have any ios simulator created so if first one is the problem then hit
"xcode-select --switch /Applications/Xcode.app/Contents/Developer" from commandline
or if second is the problem then just create one.
or if all well then shut down appium, simulator and the script &  restart appium  -> script





Note : Now the visual is also available for the same on youtube: 

click on- Automate Safari browser on mobile 


Memories from Conference of the year

Another happy day:-) Got the opportunity to meet & chat with the amazing people around the globe. The brilliant minds behind the inven...