RSS Feed

Automating Chrome Browser in Android using Appium

Pre-Requisite:

  1. Install npm from https://nodejs.org/en/download/
  2. Open cmd then rub ‘npm install -g appium’
  3. Install java and include the bin path in environment variables
  4. Then install android sdk from https://developer.android.com/studio/index.html
  5. Include adb location to Environment variables path(Note: All tools of android will be installed in C:\Users\\AppData\Local\Android\Sdk)
  6. Create an emulator and run it
  7. Open cmd run ‘adb devices’ will list the running avd
  8. Install the chrome apk file in the emulator
  9. To Install the chrome apk, download the chrome apk and then open cmd then run ‘adb install ’

Running Appium server:

  1. Open cmd
  2. Run ‘appium -p 4723 –default-capabilities “{\”platformName\”: \”Android\”,\”browserName\”: \”Chrome\”,\”avd\”: \”testmax_API_23\”}”’
  3. For more server arguments https://appium.io/slate/en/master/?ruby#appium-server-arguments

Writing Client script:

  1. Install ruby latest version
  2. Gem install appium_lib

Sample script:

require 'appium_lib'
require 'watir-webdriver'
require 'selenium-webdriver'
# The address and port of our Appium server
server_url = 'http://localhost:4723/wd/hub/'
# Hash with information selenium webdriver needs
capabilities =
{
platformName: 'Android',
platformVersion: '4.4',
deviceName: 'Android Emulator',
browserName: 'Chrome'
}
#Setup webdrivers to talk to Appium and mobile chrome and use implicit_waits to poll with long timeout to see if pages are loaded
driver = Selenium::WebDriver.for(:remote, :desired_capabilities => capabilities, :url => server_url)
browser = Watir::Browser.new driver

Javascript Watir to check element is visible in viewport

Tested in google page search field

element = $browser.text_field(:id => ‘lst-ib’)
script = < 0 &&
rect.right > 0 &&
rect.left < (window.innerWidth || document. documentElement.clientWidth) /*or $(window).width() */ &&
rect.top < (window.innerHeight || document. documentElement.clientHeight) /*or $(window).height() */;
}
return isElementInViewport(arguments[0]);
JS
puts $browser.execute_script(script, element)

Watir Execute java script on element

element = browser.link(:id => ‘link_id’)
script = <<-JS
return arguments[0].innerHTML
JS
puts browser.execute_script(script, element)

Perl – Modules

Including Other Files:

  • do – do will look for a file by searching the @INC path (more on that later). If the file can’t be found, it’ll silently move on. If it is found, it will run the file just as if it was placed in a block within our main program
  • require – is like do, but it’ll only do once. It’ll record the fact that a file has been loaded and will ignore further requests to require it again. It also fails with an error if it can’t find the file you’re loading.
  • use – include all modules before the start of the program

Bless Reference:
bless($self, $class);
Creating own modules:
First parameter passed along with calling a method in class is its own object.
package Person;
# Class for storing data about a person
#person3.pm
use warnings;
use strict;
sub new {
my $class = shift;
my $self = {@_};
bless($self, $class);
return $self;
}
Usage:
my $object = Person::new(“Person”,
“surname”, “Galilei”,
“forename”, “Galileo”,
“address”, “9.81 Pisa Apts.”,
“occupation”,”bombardier”
);
1;

Perl – Subroutine:

Syntax:
sub name(parameters) {
}
Calling Sub routines:
sub_name();
sub sub_name {
.
.
}
if only sub_name is given then it throws error as bare word.
if need to use only sub_name have define the sub routine first and then call the sub.

Return statement:
In general last line of the sub routines values will get return or else we can use the keyword return.
sub secs2hms {
my ($h,$m);
my $seconds = shift;
$h = int($seconds/(60*60)); $seconds %= 60*60;
$m = int($seconds/60); $seconds %= 60;
return ($h,$m,$seconds); or ($h,$m,$seconds);
}
Sub routine reference:
Declaring References to Subroutines
sub something { print “Wibble!\n” }
my $ref = \&something;
Dereferencing subs:
&{$ref};
&{$ref}(@parameters);
&$ref(@parameters);
$ref->();
$ref->(@parameters);

Perl – Reference

Array Reference:

  • my @array = (1, 2, 3, 4, 5);
  • my $array_r = \@array;

Hash Reference:

  • my %hash = ( apple => “pomme”, pear => “poire” );
  • my $hash_r = \%hash;

Scalar Reference:

  • my $scalar = 42;
  • my $scalar_r = \$scalar;

To create Direct reference:

  • Array reference – To get an array reference instead of an array, use square brackets [] instead of parentheses. eg: $a = [1,2,3]
  • Hah Reference – To get a hash reference instead of a hash, use curly braces {} instead of parentheses. eg: $a = {a,2,3}

Dereference:
my @array2 = @{$array_r}; or @$array_r or $ref->[0] or $$ref[0][1]
my %hash = %{$array_r}; or %$array_r or $ref->{key} or $$ref[0]{key}

ARGV in Perl:

ARGV in Perl:
@ARGV array holds any text after the program’s name on the command line.
<ARGV> takes each file after the program’s name on the command line.