Showing posts with label android. Show all posts
Showing posts with label android. Show all posts

Tuesday, November 22, 2016

Running Mobile Web (Chrome) Tests on Android Virtual Device (Genymotion) using Appium

Requirements:
- OS X El Capitan (10.11.6)
- Android SDK (with tools and platform-tools)
- JDK 1.8
- Node.js v7.0.0 (brew install node)
- appium 1.6.0 (npm install -g appium)
- VirtualBox
Genymotion (2.8.0) virtual device (requires an account - free for personal use)
- chrome.apk
- chromedriver (brew install chromedriver)

Steps:
- Start the Android Genymotion virtual device.



- Verify appium installation/setup using the ff command:
$ appium-doctor --android



- Start appium using the ff command:
$ appium --default-capabilities android.json

where android.json:
{
"app":"chrome",
"newCommandTimeout":600,
"chromedriverExecutable":"/usr/local/bin/chromedriver"
}




- Run the following code:


import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class AppiumAndroid {
 private static WebDriver driver;

 public static void main(String[] args) throws MalformedURLException, InterruptedException {
  String hubUrl = "http://127.0.0.1:4723/wd/hub";

  DesiredCapabilities capabilities = new DesiredCapabilities();
  capabilities.setCapability("deviceName", "Android Emulator");
  capabilities.setCapability("platformName", "Android");
  capabilities.setCapability("platformVersion", "7.0");
  capabilities.setCapability("browserName", "Chrome");
  capabilities.setCapability("app", "/Users/blago/Development/appium/chrome.apk");
  // http://www.apkmirror.com/apk/google-inc/chrome/chrome-54-0-2840-68-release/chrome-54-0-2840-68-3-android-apk-download/
  driver = new RemoteWebDriver(new URL(hubUrl), capabilities);
  driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);

  String[] searchTerms = new String[] { "Bernard Lago Blogspot", "Bernard Lago Youtube",
    "Bernard Lago Google Plus" };

  for (int i = 0; i < searchTerms.length; i++) {
   googleSearch(searchTerms[i]);
   Thread.sleep(3000);
   getInfoUsingResultIndex(1); // 1 for the first link, 2 for 2nd
  }

  driver.quit();
  System.out.println("Done.");
 }

 public static void googleSearch(String searchString) {
  System.out.println("Search term: " + searchString);
  driver.navigate().to("https://www.google.com.ph/#q=" + searchString);
 }

 public static void getInfoUsingResultIndex(int index) {
  String firstLinkLocator = String.format("div.g:nth-child(%s) h3.r a", index);
  String firstCiteLocator = String.format("div.g:nth-child(%s) cite", index);
  WebElement firstLink = driver.findElement(By.cssSelector(firstLinkLocator));
  System.out.println("First Link URL: " + driver.findElement(By.cssSelector(firstCiteLocator)).getText());
  System.out.println("First link Page Title: " + firstLink.getText());
 }

}


Here's the screenshot of the actual execution.



You can also watch it here.

That's it. Thanks for visiting!

Saturday, August 6, 2016

Automating Mobile Web Gestures Using RSpec and Appium and Ruby's Touch Action library.



The following libraries are used:
ruby 2.3.0
rspec-core (3.4.0)
touch_action (1.3.3)
appium_lib (8.0.2)

The code below will automate the following mobile web gestures using Chrome on a real mobile device:

  tap
  double tap
  swipe left to right
  swipe right to left
  swipe up
  swipe down
  move/drag
  rotate
  pinch


 require 'rubygems'  
 require 'rspec/core'  
 require 'touch_action'  
 require 'appium_lib'  

 describe "Mobile Web Gestures" do  
 
      # install android sdk with platform-tools and build-tools  
      # run 'adb devices' to list deviceName  
      # start appium on a terminal  
      # on another terminal, run the following command once.  
      # bundle install   
      # to run the tests, use the ff:  
      # rspec mobile_web_gestures.rb --format documentation  

      before(:all) do  
           desired_caps = {  
                caps: {  
                     platformName: 'Android',  
                     versionNumber: '5.0.2',  
                     deviceName: '<device_id>',  
                     device: 'Android',  
                     browserName: 'Chrome'  
                }  
           }  
           @driver = Appium::Driver.new(desired_caps).start_driver  
           @driver.manage.timeouts.implicit_wait = 120  
           base_url = 'http://hammerjs.github.io'  
           @driver.navigate.to(base_url + '/')  
           sleep 2  
           @driver.execute_script("window.stop")  
           device = @driver.find_element(:css, 'div.device')  
           @driver.execute_script("arguments[0].scrollIntoView(true);", device)  
           @element = @driver.find_element(:id, 'hitarea')  
      end 
 
      after(:all) do  
           @driver.quit  
      end  

      it "performs mobile gestures" do  
           sleep 2  
           @element.touch_action(:tap)  
           sleep 2  
           @element.touch_action(:doubletap)  
           sleep 2  
           @element.touch_action(:flick, axis: "x", distance: 100, duration: 50)  
           sleep 2  
           @element.touch_action(:flick, axis: "y", distance: 100, duration: 500)  
           sleep 2  
           @element.touch_action(:move, xdist: 70, ydist: -50, duration: 500)  
           sleep 2  
           @element.touch_action(:rotate, {rotation: -75})  
           sleep 2  
           @element.touch_action(:rotate, {rotation: 0})  
           sleep 2  
           @element.touch_action(:pinch, r1: 50, r2: 100)  
      end  

 end  

See it in action here.

Thanks for visiting!