Appium – ruby

Step to follow:

1. Setup Appium (http://appium.io/) for ruby

2. Start the Appium server

3. Connect real time device r simulator using adb

4. In cmd prompt >adb devices     –> should list at-least 1 test server

5. Run the below code (Assuming that you are comfortable with selenium and watir- webdriver — if not reach out to jazzezravi@gmail.com for detailed explanation)

require ‘rubygems’
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)#, :profile => profile)
browser = Watir::Browser.new driver
browser.driver.manage.timeouts.implicit_wait = 30
puts “new ANDROID Watir browser object instantiated”

#automated browser code using Watir browser object and methods
browser.goto “google.com”

puts browser.text
puts “————-“
browser.text_field(:name,’q’).set(“Raveendran”)
browser.button(:name,’btnG’).click
puts browser.text.include?(“Raveendran”)
puts browser.text_field(:id,’jazzez’).exist?
puts browser.text.include?(“ruby selenium”)
puts browser.select_list(:id,’watir’).exist?

Date @ Ruby — In between dates during particular interval

Code:

require ‘date’

def check_weeks(dd1,dd2)
i=0
while dd1 <= dd2
dd1=dd1+0
if dd1+@interval < dd2
dd3=dd1+@interval
else
dd3=dd2
end
puts “Week from #{dd1} to #{dd3}”
dd1=dd3+1

i+=1
end
end

x=”2014/07/01″
y=”2014/07/30″

d1= Date.new(x.split(“/”)[0].to_i, x.split(“/”)[1].to_i,x.split(“/”)[2].to_i)
d2= Date.new(y.split(“/”)[0].to_i, y.split(“/”)[1].to_i,y.split(“/”)[2].to_i)

#Chnage the interval
@interval = 2

check_weeks(d1,d2)

 

Output:

 

>ruby helper.rb
Week from 2014-07-01 to 2014-07-03
Week from 2014-07-04 to 2014-07-06
Week from 2014-07-07 to 2014-07-09
Week from 2014-07-10 to 2014-07-12
Week from 2014-07-13 to 2014-07-15
Week from 2014-07-16 to 2014-07-18
Week from 2014-07-19 to 2014-07-21
Week from 2014-07-22 to 2014-07-24
Week from 2014-07-25 to 2014-07-27
Week from 2014-07-28 to 2014-07-30
>Exit code: 0

 

Change the @interval = 2 to any interval and implement it in your code

 

 

 

 

 

 

net/ftp – Rubygem

Download and read the file using net/ftp

  

require ‘rubygems’
require ‘net/ftp’

ftp=Net::FTP.new(‘100.444.211.144′)

ftp.login
ftp.chdir(“/Main_flder/Sub_folder1/Sub_folder2/”)
fileList = ftp.nlst(‘*.txt’)  #instead of .txt you can use any file extension

filedata = ‘ ‘

ftp.retrlines(“RETR ” + fileList[0]) do |block|
filedata << block

end

puts filedata    # It will print the content of the entire TEXT FILE

Watir Webdriver and JQuery

Most of the time in internet explorer, JQuery automation is little bit tricky and we need to use some methods like below only for IE browsers.

Put the below methods in to your library folder

def trigger_jquery_change(element)
$browser.execute_script(“$(‘##{element}’).change()”)
end

def trigger_jquery_onblur(element)
$browser.execute_script(“$(‘##{element}’).blur()”)
end
Script section(Including page object)

@SOME_SELECT_FIELD=$browser.select_list(:id,’ID_OF_THE_TEXT_FIELD’)

@SOME_SELECT_FIELD.when_present.select(@ANY_OPTION)
if @browser_name == “ie”
trigger_jquery_change(@SOME_SELECT_FIELD)
end

if @browser_name == “ie”
trigger_jquery_onblur(@SOME_SELECT_FIELD)
end

 

I hope there is no need to explain this code. if required please feel free to end mail to jazzezravi@gmail.com

rubyzip — Ruby gem to zip the folder and files

rubyzip — Ruby gem to zip the folder and files

Requirement:

Zip the file for some folders and files

 

Steps:

1. Create a folder chrome and put some files in it.

2. create a ruby file in same location and paste the below code

a. >gem install rubyzip

def zip_it(path)
require ‘rubygems’
require ‘zip/zip’
require ‘zip/zipfilesystem’

path.sub!(%r[/$],”)
archive = File.join(path,File.basename(path))+’.zip’
FileUtils.rm archive, :force=>true

Zip::ZipFile.open(archive, ‘w’) do |zipfile|
Dir[“#{path}/**/**”].reject{|f|f==archive}.each do |file|
zipfile.add(file.sub(path+’/’,”),file)
end
end
end

zip_it(“chrome”)

3. save and run the ruby file.

4. Zipped file will be created with in the chrome folder.

Running Watir webdriver scripts in Browser Stack Environment

Running Watir webdriver scripts in Browser Stack Environment

Why it is required?:

If we need to run the scripts in more than 5 machines with different OS and browser versions then we may need to invest more in infrasturure. To aviod this, we will have Test server in Browserstack.

 

1. Create trail account in http://www.browserstack.com/

2. Copy the Authentication URL from your acoutn page

3. Run the below code

require ‘rubygems’
require ‘watir-webdriver’

include Selenium

caps = WebDriver::Remote::Capabilities.htmlunit(:javascript_enabled => true)
caps.platform = :WINDOWS
caps[:name] = “Watir WebDriver”
caps[:browser] = “chrome”
caps.version = 20
caps[“browserstack.debug”] = “true”
caps[“browserstack.tunnel”] = true

browser = Watir::Browser.new(:remote,
:url => “PASTE BTASCK URL HERE”, # Sample http://raveendran:x6XqqrJzJ9gUZ7sr@hub.browserstack.com/wd/hub
:desired_capabilities => caps)

browser.goto “http://google.com/&#8221;

sleep 9 # You should use wait methods instead of sleep
puts browser.title
browser.quit

Pros:

Easy implementation with any exisitng framework

Cons:

The execution speed will be 2x~3x slow when comparing with your local machine execution.