Appium Setup, Sample Test Scripts.

Posted: April 4, 2014 in Technology

Here is the good document regarding Appium Architecture, which is very useful to read before starting actual coding. 🙂

  • Important Documentation :-

  • Here are the steps to run rspec ruby script for android automation for android API level >17 i.e using uiautomator:-

(Note :- uiautomator is android’s default ui test runner available on API level <17)

  • Make sure you have latest version of java installed and JAVA_HOME environment variable SET.

  • Download and install Android SDK (use USE AN EXISTING IDE option to just download SDKs). After installation open Android SDK Manager and install all API levels.

  • Set System Variable Android_HOME to C:\Users\abc\android-sdks and accordingly for MAC and Linux.

  • Set Path variable to %Android_HOME%\platform_tools, %Android_HOME%\tools

  • Open Terminal or Command-prompt. Run adb if it is successful identified your command means your ANDROID_HOME set properly otherwise check it again.

  • Follow the step to create new AVD(Android Vertual Devices)  here or use external android devices to run automation.

  • Download Appium Setup Extract zip file to windows user folder rename folder as a Appium. for MAC/Linux install .dmg package.

  • Download and install node.js server.

  • Try to run Appium(if your .Net framework is outdated it will ask you to install updated version. Please do install latest .Net Framework)

  • Following command will launch emulator or use external devices.

emulator @NameofEmulator

  • Following command will show all attached devices list with device id

adb devices

  • Launch Appium by double clicking on the Appium icon in Appium folder that you extracted in your user folder for Windows. for MAC appium& will launch the node.js server.

  • Here are the steps to run rspec ruby script for android automation for android API level <17:-

(Note :- uiautomator is android’s default ui test runner available on API level <17) so we are using selendroid server

  • Clone appium github code (Don’t download zip file which leads problems in git url not found when rebuilding selendroid.)

git clone https://github.com/appium/appium

  • Go to C:\Windows\Temp folder and delete  selendroid.com.examlepackage.mobility.apk file also com.examlepackage.mobility folder

  • we may need to rebuild selendroid server by cd to appium-master github code directory and run command

reset.bat –selendroid

  • To run automation on lower android devices we need to use ‘Selendroid’ as shown below( Appium Bug  is still open)


capabilities =  {

   ‘browserName’ => ‘Selendroid’,

   ‘platform’ => ‘WINDOW’,#Mac, Linux

   ‘version’ => ‘2.3.3’,

   ‘device’ => ‘Selendroid’,

   ‘app’ => absulute_path,

   ‘app-package’ => ‘com.example.android.contactmanager’,

   ‘app-activity’ => ‘com.example.android.contactmanager.ContactManager’

}

  • Node server will be launched.

  • install necessary ruby gems


     gem install selenium-webdriver

     gem install rspec

     gem install net-http-persistent

  • Ruby script :- (Run rspec your_file_name.rb)


require ‘selenium-webdriver’

require ‘selenium/webdriver/remote/http/persistent’

require ‘rspec’

absulute_path = ‘C:\Users\abc\Desktop\AppiumTest\ContactManager.apk’

capabilities =  {

   ‘browserName’ => ‘Android’,

   ‘platform’ => ‘WINDOW’,#Mac, Linux

   ‘version’ => ‘4.3’,

   ‘device’ => ‘Android’,

   ‘app’ => absulute_path,

   ‘app-package’ => ‘com.example.android.contactmanager’,

   ‘app-activity’ => ‘com.example.android.contactmanager.ContactManager’

 }

server_url = ‘http://127.0.0.1:4723/wd/hub&#8217;

describe “Contact” do

 before :all do

   @driver ||= Selenium::WebDriver.for( :remote,

     :desired_capabilities => capabilities,  :url => server_url,

   )

end

 after :all do

   @driver.quit if @driver

 end

 it “add a contact” do

new_button = @driver.find_element(:name, “Add Contact”)

new_button.click

       wait = Selenium::WebDriver::Wait.new(:timeout => 20) # seconds

textfields = @driver.find_elements(:tag_name,”textfield”)

textfields[0].send_keys(“Android_Appium”)

textfields[1].send_keys(“12345”)

textfields[2].send_keys(“Android@Appium.com”)

@driver.find_element(:name, “Save”).click()

 end

end  

  • Explanation of ruby script :-


*Capabilities*

capabilities =  {

   ‘browserName’ => ‘Android’,

   ‘platform’ => ‘WINDOW’,#Mac, Linux

   ‘version’ => ‘2.3.3’,#  4.3

   ‘device’ => ‘Selendroid’,#Android

   ‘app’ => absulute_path,

   ‘app-package’ => ‘com.example.android.contactmanager’,

   ‘app-activity’ => ‘com.example.android.contactmanager.ContactManager’

 }

Instantiate Selenium driver instance with server_url as node.js server uses JSON-Wire Protocol.

Appium will execute JsonWireProtocol command by converting selenium code.

server_url = ‘http://127.0.0.1:4723/wd/hub&#8217;

@driver = Selenium::WebDriver.for(:remote, :desired_capabilities => capabilities, :url => server_url)

@driver instance will launch your application specified in capabilities on emulator or on attached devices.

  • I’m able to run ruby scripts for android devices on Win 7(64 bit) machine with android Devices having API level >15(version 4.1+) as uiautomator is only supported by android devices having API level >15(version 4.1+) with capabilities


capabilities =  {

   ‘browserName’ => ‘Android’,

   ‘platform’ => ‘WINDOW’,#Mac, Linux

   ‘version’ => ‘4.3’,

   ‘device’ => ‘Android’,

   ‘app’ => absulute_path,

   ‘app-package’ => ‘com.example.android.contactmanager’,

   ‘app-activity’ => ‘com.example.android.contactmanager.ContactManager’

}

For Java use TestNg framework or any frame work you like to run this code.

package com.test.selenium; import org.openqa.selenium.*; import org.openqa.selenium.interactions.HasTouchScreen; import org.openqa.selenium.interactions.TouchScreen; import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteTouchScreen; import org.openqa.selenium.remote.RemoteWebDriver; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import java.io.File; import java.net.URL; import java.util.List; public class AndroidContactsTest { private WebDriver driver; @BeforeMethod public void setUp() throws Exception { // set up appium File classpathRoot = new File(System.getProperty(“user.dir”)); File appDir = new File( “C:\Users\abc\appium-master\sample-code\apps\ContactManager\”); File app = new File(appDir, “ContactManager.apk”); DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability(“device”, “Android”); capabilities.setCapability(CapabilityType.BROWSER_NAME, “Android”); capabilities.setCapability(CapabilityType.VERSION, “4.2”); capabilities.setCapability(CapabilityType.PLATFORM, “Windows”); capabilities.setCapability(“app”, app.getAbsolutePath()); capabilities.setCapability(“app-package”, “com.example.android.contactmanager”); capabilities.setCapability(“app-activity”, “.ContactManager”); driver = new SwipeableWebDriver(new URL(“http://127.0.0.1:4723/wd/hub&#8221;), capabilities); } @AfterMethod public void tearDown() throws Exception { driver.quit(); } @Test public void addContact() { WebElement el = driver.findElement(By.name(“Add Contact”)); el.click(); List textFieldsList = driver.findElements(By .tagName(“textfield”)); textFieldsList.get(0).sendKeys(“Some Name”); textFieldsList.get(2).sendKeys(“Some@example.com”); driver.findElement(By.name(“Save”)).click(); } public class SwipeableWebDriver extends RemoteWebDriver implements HasTouchScreen { private RemoteTouchScreen touch; public SwipeableWebDriver(URL remoteAddress, Capabilities desiredCapabilities) { super(remoteAddress, desiredCapabilities); touch = new RemoteTouchScreen(getExecuteMethod()); } public TouchScreen getTouch() { return touch; }}}

Comments are closed.