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

Thursday, December 18, 2008

how to automate sproutcore appliaction through watir

Automation of a sproutcore application is still a headache for programmers. We can automate sproutecore application through selenium and watir as well.

I have successfully created a sample script which runs from both selenium and watir as well.
Actually, I have managed some api which run according to the environment like watir or selenium.

The question how you automate a sproutcore application while there is no such HTML tag on the page. Complete application runs through JavaScript. We can not click on any event directly from id. So, how can we deal with this problem?
So guys cheer up in the sproutcore everything is a link. So I have classified the event into three parts:

1. text field
2. button
3. Check box or radio button.

For the first case we deal it normally as we did normally with watir

$browser.text_field(:id,”id”).set(“hello sproutcore”)

The second and third case is different as we can not click on the button or any event directly.

We need to find out the controller and corresponding method associated to the event. We need to execute JavaScript. Well guys, there is a hell lot of difference to click on button or check box. Have a look at the controller.

There is a direct method associated to the button. Check out the view page this is an action while radio button and check box bind the value. Now go to the controller you will see that the corresponding method is actually bind from the variable. That means it always observes the value of the variable and performs action accordingly.

Ok. I am giving you an example how to automate it from watir.

I have create a sample application which have a button with the id=”toggle_button”.
I just find out the source code and go to the index of “toggle_button”. The reason being I need the controller and method information as you can see the following source code:


toggleButton: SC.ButtonView.extend({
action: "SproutHello.appController.toggleGreeting"
}).outletFor("#toggle_button") ,



So now I have all information. I just need to create a JavaScript and just execute it.

body_array = page_source()
action_str = body_array[body_array.index("}).outletFor(\"##{button_id}\")")-1]
action = action_str.split("\"")[1]
ff.js_eval("var w_all = getWindows(); var w_one = w_all[#{window_count}]; w_one.content.#{action}();")


So, guys start playing with clicking on button. :)

We can not click on check_box with the above code the difference is in the javascript because check box and radio button the working as toggle. If it is on then you need to turn it off and vice versa. So you need to get the value and set the value.

body_array = page_source()
action_str = body_array[body_array.index("}).outletFor(\"##{event_id}\")")-3]
action = action_str.split("\"")[1]
_obj = action.split(".")
_get = "w_one.content" + "." + _obj[0].to_s + "." + _obj[1].to_s + ".get(\"" + _obj[2] + "\")"
_set = "w_one.content" + "." + _obj[0].to_s + "." + _obj[1].to_s + ".set(\"" + _obj[2] +"\",!" + _get.to_s + ")"
ff.js_eval("var w_all = getWindows(); var w_one = w_all[#{window_count}]; #{_set}")


Don’t get confused with window count this is to check how much firefox windows are open and on which script should work upon.

Enjoy sproutcore automation
:)
gud luck.

Monday, December 15, 2008

data migration from YAML using ruby

When I was given a task to implement YAML, I did not know how to implement and even did not get any clue to proceed. The prime concern was to give our successful first release. At last I have only a weekend and my lead has postpone this YAML implementation to the next build and told me to implement this concept with an demo application which is to be given with the release.

She even not expecting this to be implemented successful but the expectation was that I should be answerable on this topic at the time of demo.


Well I had to work whole weekend night and had to spend complete two days alone in the office, even my team was not aware about this implementation.


Monday I told to my lead and she was so surprised about you not even implemented the YAML successfully but you have made changes to the others 25 module.


The reaction from the lead was made me happy and encouraged a lot.

I just want to share some little concept what I have done with YAML.


YAML is a straightforward machine parsable data serialization format designed for human readability and interaction with scripting languages such as Perl and Python.


Early in its development, YAML was said to mean "Yet Another Markup Language", but was retronymed to distinguish its purpose as data-centric, rather than document markup.


So the task is to make a data structure in the YAML which returns an array of hash.



Data.yml

-

suite_file: "test_status_codes_test_suite.rb"

flag: "0"

-

suite_file: "testing_types_test_suite.rb"



The Most important this is to take care of spaces. YAML file does not support tabs. And you need to follow the same space convention through out the file.


‘-‘ creates an array element.

I have given the two white spaces to create an hash key-value within the array element.


Now you want to remove the iteration from your ruby code so you want a hash instead of array.



Code.yml



TCM_SS_MTSC_1:

desc: "Hi vaibhav."

TCM_SS_MTSC_3:

desc: "Good luck team!."

TCM_SS_MTSC_2:

desc: "done a good job."

TCM_SS_MTSC_4:

desc: "you are owe-some"

flag: "0"



Above is the hash of hash, you will get it like:


{: TCM_SS_MTSC_1 => { :desc => "Hi vaibhav."}, : TCM_SS_MTSC_3 => { :desc =>"Good luck team!."}}


Again take care of spaces otherwise it will give you an unidentifiable error at run time.

Finally the question is how to load the YAML File and use the data


J

So friends lets have a look:



$Data = YAML::load(File.read('config/Data.yml'))


Now whatever I have specified in the data.yml file I will get as in a data structure.

You should prefer to use YAML file when you need to migrate data into database. So dear enjoy playing with YAML.


J

Good luck