Friday, August 28, 2009

Use of getoptlong in Ruby

this post is to describe how to use getoptlong package in ruby. Once i need to use it while i have given a task to generate a csv file in a specified format and need to upload that file into my automated testing tool "TESTDROID". there are some snippet of code that may help you to write code for your own.

you need to require some gem:

the format of getoptlong comments for the help and the text for special help is as follows:


then i need to handle the usage according to my requirements. as you can see that requirement in the last snnipet. so i had to decide which usage option i need to call according to the help option. so i have written a method for this requirement just look at this:

now the question is to call or use wrtten code. so just look at this:


hope this may help you to implement this.
Cheers!!

Monday, August 3, 2009

Effective use of xpath in automation (Recursive Descent Xpath)

using xpath in the automation can let you down any time. so there is a big question what do we do when there is no option left except using xpath. so we have recursive descent xpath.it actually search for particular context element.
for example:




following is the description of recursive descent xpath from Microsoft:

An expression that uses the double forward slash (//) indicates a search that can include zero or more levels of hierarchy. When this operator appears at the beginning of the pattern, the context is relative to the root of the document.For example,
the following expression refers to all elements anywhere within the current document:


Wednesday, July 1, 2009

Automatic Mail Generation From Ruby Code

I need to send a mail in between my program and need to add a file as an attachment.
so for accomplishing this task we nee to require some file as follows:

now we need to send mail for this we are going to use SMTP server.
so first we need to connect with the server through your domain name and port no.

and don't forget to start the smtp server after creating an object.

now almost done just prepare your mail header.
i have used mailfactory rather to use hard code header.
mailfactory is a gem.

Now the mailfactory object creation and creation of email header:

after creating the object we just need to kick off the object through SMTP server.

don't forget to destroy smtp object and close the connection.

so guys enjoy sending a mail through your code.
cheers!!!

Wednesday, June 17, 2009

Work on excel file in ruby

To play with excel file through ruby, you have to insatll a gem.

> gem install 'parseexcel'

Now you need to require 'parseexcel'
so you code will look like:










as we know excel looks like a book where multiple pages exists.
so we have to create an instances accordingly.

so another snippet says:















like wise we can traverse through every worksheet.

enjoy working with excel.
cheers!

Friday, June 5, 2009

Mysql connectivity in ruby while database is allocated at LINUX machine

db = Mysql.real_connect(ip, Username, password, database_name)

username is that user which has all the privileges from mysql(CRUD).

now we should handle some exception handling to this code of line.

begin
# connect to the MySQL server
db = Mysql.real_connect(ip, Username, password, database_name)
rescue Mysql::Error => e
puts "Error code: #{e.errno}"
puts "Error message: #{e.error}"
puts "Error SQLSTATE: #{e.sqlstate}" if e.respond_to?("sqlstate")
end


oops! i forgot to require mysql file. obviously, i have to know the address where i am going to kill the real culprit.

so gusy require "mysql"

so lets go to attain the purpose of doing that mysql connectivity.

insertion of data into a specific table:

db.query("insert into table_name values(x,yz1,'For testing',null)")

edit a perticular record of a perticular table:

db.query("update table_name set column_1 = NULL , column_2 = NULL where Column_3 = 1")

deletion of a record of a perticular table:

db.query("DELETE from table_name where id > 2")

Tuesday, March 17, 2009

"is_element_present" does not return FALSE !!!

While writing automation script in selenium using ruby I had a need to assert and check element on the page. I have created an API for element present and check assertion to mention in the report and log. So I got “is_element_present” method which returns TRUE if it finds element on the page otherwise FALSE.

But actually it doesn’t return FALSE. In fact throws an error and just demolish all the script and go out from code. You can handle this situation and even can write this to your report and log as well.

Have a look:

def is_element_present(id)
begin
$browser.is_element_present(id)
rescue SeleniumCommandError
return FALSE
end
end


this actually handling the exception and that case when element doesn’t present on the page you can explicitly return FALSE. By this, “is_element_present” works expectedly.

Wednesday, January 21, 2009

Forgery error while maintaining session in ruby on rails

hi guys recently i have tried pragmatic rails application depot. it was going good while the implementation apart from there were some changes because of i am working on the new version of rails 2.0.2
as soon as i have reached to store session the application have started giving me a stupid error (frogery error). it was just refusing me to pass explicit session. then i came to know that button_to or link_to creates its own session and passes this session to the controller.
for avoiding this crap i have just followed the steps

go to depot/config/environment.rb and uncomment this,

config.action_controller.session_store = :active_record_store

go to depot/app/controller/application.rb and uncomment the following

protect_from_forgery :secret => '165451f36fafd3eba6cd953bbcb6daf0'

now just restarted my server this went fine.

hope this will help you.
cheers