background-color · border · border-color · color · Ruby · watir-webdriver

Watir-Webdriver – Verify Elements Color , background-color and border

Description:

To Verify the Elements Color / border-color / border / background-color using Watir-webdriver

Code

require ‘rubygems’
require ‘watir-webdriver’

@browser= Watir::Browser.new :chrome

@browser.goto(“bit.ly/watir-webdriver-demo”)

def verify_element_color(element,property,content,message)
begin
rgb_color=element.style property
rgb_color=rgb_color.sub(“rgba(“,””)
rgb_color=rgb_color.sub(“)”,””)
hex_color=rgb_color.split(“,”)
color=”#%02X%02X%02X%02X” % hex_color

if color.downcase.include? content.downcase
puts “Passed – #{message}”
else
puts “Failed – #{message}”
end
rescue
puts “Failed – #{message}”
end
end

verify_element_color(@browser.text_field(:id,’entry_1000000′),”border-color”,”#BBBBBB”,”Verify that Name Text Field has Block Color Border”)
@browser.button(:name,’submit’).click
verify_element_color(@browser.text_field(:id,’entry_1000000′),”border-color”,”#C43B1D”,”Verify that Name Text Field has Block Color Border”)

Usage:

You can pass property as a “border-color”,”border”, “color”, “background-color”

nokogiri · open-uri · raveendran · Ruby · scrap

Nokogiri – Rubygem

Sample code to save the website in to html in local machine.

Benefit : We can scrap the content from the target website.

Code

require ‘open-uri’
require ‘nokogiri’

link=”http://google.com”

doc = Nokogiri::HTML(open(link))

file1=File.open(“test.html”,’w’)

file1.puts doc

file1.close

appium · Ruby · Ruby 1.9 · selenium-webdriver · watir-webdriver

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 · Ruby 1.9

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

 

 

 

 

 

 

Blogroll

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.

Auto Complete · Ruby · watir-webdriver

Automating ‘Auto Complete’ text field using Watir-WebDriver

Requirement:

1. Navigate to Google.com

2. Type “Ravee” in text Field
 3. Select “Raveendran” from Auto Complete option from Suggested option.
Steps to Solve the issues:
1. Navigate to Google.com
2. Type “Rav” in text Field
3. Using IE Developer Tool Bar –> Click The suggested Option (So You will get the source code for suggestion lists)
4. Find the parent id –> Ex. It should TABLE mostly.
So we have Table id/name/class
5. Collect All the Rows under this table
6. Check the text for each and every row
7. If Any one of the row matches  “Raveendran”
8. Then Simply Click It
Code:

require 'rubygems'
require 'watir-webdriver'

browser=Watir::Browser.new :ie
browser.goto("google.com")

browser.text_field(:name,"q").set("Ravee")
sleep 3
auto_content=browser.table(:class,'gssb_m').trs

auto_content.each do |g|
  if g.text.downcase == "raveendran"
    g.click
    sleep 3
    break
  end
end

 

Demo –> http://bit.ly/j_autocomplete_watir

Ruby

Ruby – Some Useful Links

Ruby:
Homepage : http://www.ruby-lang.org

Downlaod : http://www.ruby-lang.org/en/downloads/

Documentation: http://www.ruby-lang.org/en/documentation/

Libraries: http://www.ruby-lang.org/en/libraries/

Community: http://www.ruby-lang.org/en/community/

News: http://www.ruby-lang.org/en/news/

Security: http://www.ruby-lang.org/en/security/

About Ruby: http://www.ruby-lang.org/en/about/

 

Documentation:

Getting Started –>

http://pine.fm/LearnToProgram/

http://www.ruby-lang.org/en/documentation/quickstart/

http://www.ruby-lang.org/en/documentation/ruby-from-other-languages/

http://rubylearning.com/

http://www.techotopia.com/index.php/Ruby_Essentials

http://www.meshplex.org/wiki/Ruby/Ruby_on_Rails_programming_tutorials
Manual –>
http://www.ruby-doc.org/docs/ProgrammingRuby/

http://www.rubyist.net/~slagell/ruby/

http://en.wikibooks.org/wiki/Ruby_programming_language

 

Reference Documentation –>

http://www.ruby-doc.org/core/

http://www.ruby-doc.org/stdlib/

 
Further details –>

http://ruby-doc.org/

 

books –>

http://www.ruby-doc.org/bookstore

http://antoniocangiano.com/ruby-and-rails-recommended-books/

http://www.rubycentral.com/book/

http://www.freeprogrammingresources.com/ruby-tutorial.html

http://www.programmingbooks.org/Ruby

http://pspxworld.com/book/programming/ruby.php

http://www.sitepoint.com/books/rails1/

http://www.buildingwebapps.com/learning_rails

http://lifehacker.com/software/ruby/free-e+book-teaches-ruby-programming-225976.php

http://kickjava.com/books/ruby.htm

http://www.onlinecomputerbooks.com/free-ruby-books.php

http://www.easywebtech.com/ebooks/Free_Ruby_ebooks_Download-0.html

http://www.scribd.com/doc/1622/Free-Ruby-eBook

http://www.infoq.com/minibooks/ruby

http://www.sapphiresteel.com/The-Little-Book-Of-Ruby

http://www.rorsecurity.info/the-book/

http://www.freebooksclub.net/ruby-ebooks/

http://www.freebookcentre.net/programming-books-download/Whys-(Poignant)-Guide-to-Ruby.html

http://knowfree.net/category/it-ebooks/ruby

http://www.webstuffscan.com/2007/07/13/download-free-ruby-ebooks-pdf/

http://www.webappers.com/2007/10/07/sitepoint-giving-away-ruby-on-rails-complete-free-book/

http://www.freetechbooks.com/ruby-f49.html

http://nealenssle.com/blog/2007/01/05/free-ruby-e-book/

http://www.sapphiresteel.com/The-Book-Of-Ruby-free-in-depth

http://www.asiaing.com/mr.-neighborlys-humble-little-ruby-book-free-ebook.html

http://www.humblelittlerubybook.com/book/

http://2020ok.com/books/37/guide-to-ruby-34437.htm

http://www.bookrags.com/shortguide-ruby-in-the-smoke/

 

Course –>

http://h2kinfosys.com

decryption · encryption · Ruby · Testing

Ruby — Encryption and Decryption using OpenSSL

Code:

require 'rubygems'
require 'openssl'

$key = "A75435F0B240012A9489000C2952E41F"

class String

def encrypt(key=$key)
e = OpenSSL::Cipher::Cipher.new 'DES-EDE3-CBC'
e.encrypt key
s = e.update self
s << e.final
s = s.unpack('H*')[0].upcase
s
end

def decrypt(key=$key)
e = OpenSSL::Cipher::Cipher.new 'DES-EDE3-CBC'
e.decrypt key
s = self.to_a.pack("H*").unpack("C*").pack("c*")
s = e.update s
s << e.final
end
end
puts "raveendran".encrypt("password")
puts "509067DC2076497134DBF9A7DE5992B2".decrypt("password")

Output

509067DC2076497134DBF9A7DE5992B2

raveendran

Note:

You can chnage the $key vlaue.

You can encrypt the string with password. The password should match when trying to decrypt the encrypted string.