RSS Feed

Accessing outlook mail using Ruby

  • gem install mail
Mail.defaults do
delivery_method :smtp, {
:address => 'smtp.office365.com',
:port => 587,
:domain => 'domain.com',
:user_name => 'username@domain.com',
:password => 'password',
:authentication => :login,
:enable_starttls_auto => true
}
retriever_method :imap, {
:address => 'outlook.office365.com',
:port => 993,
:user_name => 'username@domain.com',
:password => 'password',
:enable_ssl => true
}
end

Advertisements

Meta Programming in Ruby

Metaprogramming:

Metaprogramming is a technique by which you can write code that writes code by itself dynamically at runtime. This means you can define methods and classes during runtime.

Example:

class Test
 def self.back
  "I am back tester"
 end
 
 def front
  "I am front tester"
 end
end

 

We have defined a class with two methods. The first method in this class is a class method and the second one is an instance method. This is basic stuff in Ruby, but there is much more happening behind this code which we need to understand before we proceed further. It is worth pointing out that the class Test itself is actually an object. In Ruby everything is an object, including classes. Since Developer is an instance, it is an instance of class Class.

What is a class method?

Class method is nothing but an instance method defined in the metaclass for the object Developer! A metaclass is essentially a class that Ruby creates and inserts into the inheritance hierarchy to hold class methods, thus not interfering with instances that are created from the class.

Metaclasses:

Every object in Ruby has its own metaclass. It is somehow invisible to a developer, but it is there and you can use it very easily. Since every class is essentially an object of the class Class, it has its own metaclass.

example = "I'm a string object"
def example.something
 self.upcase
end
p example.something
# I'M A STRING OBJECT

What we did here is we added a singleton method something to an object. The difference between class methods and singleton methods is that class methods are available to all instances of a class object while singleton methods are available only to that single instance. Class methods are widely used while singleton methods not so much, but both types of methods are added to a metaclass of that object.

Defining Methods Using “class_eval” and “instance_eval”:

There’s one more way to create a class method, and that is by using instance_eval:

class Test
end

Test.instance_eval do
 p "instance_eval - self is: " + self.to_s
 def backend
 p "inside a method self is: " + self.to_s
 end
end
# "instance_eval - self is: Test"

Test.backend
# "inside a method self is: Developer"

This piece of code Ruby interpreter evaluates in the context of an instance, which is in this case a Test object. And when you are defining a method on an object you are creating either a class method or a singleton method. In this case it is a class method – to be exact, class methods are singleton methods but singleton methods of a class, while the others are singleton methods of an object.

On the other hand, class_eval evaluates the code in the context of a class instead of an instance. It practically reopens the class. Here is how class_eval can be used to create an instance method:

Test.class_eval do
 p "class_eval - self is: " + self.to_s
 def frontend
 p "inside a method self is: " + self.to_s
 end
end
# "class_eval - self is: Test"

p test= Test.new
# #<Test:0x2c5d640>

test.frontend
# "inside a method self is: #<Test:0x2c5d640>"

To summarize, when you call class_eval method, you change self to refer to the original class and when you call instance_eval, self changes to refer to original class’ metaclass.

Defining Missing Methods on the Fly:

One more piece of metaprogramming puzzle is method_missing. When you call a method on an object, Ruby first goes into the class and browses its instance methods. If it doesn’t find the method there, it continues search up the ancestors chain. If Ruby still doesn’t find the method, it calls another method named method_missing which is an instance method of Kernel that every object inherits.

define_method is a method defined in Module class which you can use to create methods dynamically. To use define_method, you call it with the name of the new method and a block where the parameters of the block become the parameters of the new method.

class Test
 define_method :frontend do |*my_arg|
   my_arg.inject(1, :*)
 end
end

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);