How to call Function argument into another ruby script

Consider I have a ruby file called library.rb.

In which i defined a function like this :

def Name(testcase, result)
pdf = PDF::Writer.new
testcase= $testcase
pdf.text $testcase, :font_size => 72, :justification => :center
result= $result
pdf.text $result, :font_size => 72, :justification => :center
end

now i have another ruby file named web.rb which calls thus library.rb.

My Problem is I need to pass these arguments separately.

Means , I need to call Testcase argument firstly and then after many
conditions I need to pass result as Pass or fail.

The Testcase is like I am doing a web automation and the library.rb is
the report part where From the web.rb I need to pass the testcase name
and then at the end I need to pass end result.

Please help me to do this.

On Mon, Nov 21, 2011 at 6:55 PM, hari mahesh [email protected]
wrote:

end
and then at the end I need to pass end result.

Please help me to do this.

You could create a class in library.rb that receives a testcase in the
constructor, and has a method report which receives the result and
does what you are doing in the above method. Then in your web.rb you
will create an instance of that class passing the testcase, then you
do your thing, and at the end you call the report method. Something
like:

#library.rb
class Report
def initialize testcase
@testcase = testcase
end

def report result
pdf = PDF::Writer.new
pdf.text @testcase, :font_size => 72, :justification => :center
pdf.text result, :font_size => 72, :justification => :center
end
end

#web.rb
require ‘library’

report = Report.new testcase
#… your testing stuff
report.report result

Jesus.

2011/11/21 Jess Gabriel y Galn [email protected]:

pdf.text $result, :font_size => 72, :justification => :center
the report part where From the web.rb I need to pass the testcase name

end
end

#web.rb
require ‘library’

report = Report.new testcase
#… your testing stuff
report.report result

Just a minor nitpick: the file should not be called “library.rb”
because that is way too unspecific. In your example it could be
called “report.rb” or “report_test.rb” or “test-report.rb”.

Kind regards

robert

Hi Jesus,

When I run I am getting an error “undefined local variable or method
`testcase’ for main:Object (NameError)”

I will share u my both scripts :

#testcase_lib.rb

require ‘rubygems’
require ‘pdf/writer’
require ‘pdf/simpletable’

class Report
def initialize testcase
@testcase = testcase
end

def report result
pdf = PDF::Writer.new
pdf.select_font “Times-Roman”
pdf.text @testcase, :font_size => 72, :justification => :center
pdf.text result, :font_size => 72, :justification => :center
pdf.save_as(“Report.pdf”)
end
end

#testcase.rb

require ‘rubygems’
require ‘pdf/writer’
require “testcase_lib”

report = Report.new testcase

@testcase = “Login Test”;

report.report result
result = “PASS”

What I was trying to do is, when i run I want to pass the testcase name
to testcase_lib.rb, which will be saved to PDF, after running the
testcase, the result Pass or Fail will be passed as result. I tried for
2 days but no matter what its not working. Please help

On Nov 21, 2011, at 8:26 PM, Robert K. wrote:

result= $result
pdf.text $result, :font_size => 72, :justification => :center
end

now i have another ruby file named web.rb which calls thus library.rb.

My Problem is I need to pass these arguments separately.

Means , I need to call Testcase argument firstly and then after many
conditions I need to pass result as Pass or fail.

If I understand that correctly, what you’re looking for is actually
textbook partial application or currying/schnfinkeling. If you’re using
Ruby 1.9 you can use Proc#curry directly like so:

unbind the method and convert it to Proc to use #curry

report = method(:Name).to_proc.curry[testcase]

and later apply the second argument

report[result]

Alternatively, you can also implement partial application like this:

define a lambda that that captures your test case in a closure so you

can pass the result later
report = lambda { |result| Name(testcase, result) }

later, execute the lambda with your result

report[result]

Note that in your case it is probably better to follow the advice posted
by Jesus and Robert for a cleaner solution. It’s just that functional
programming is fun, and your question really seemed to call for partial
application!

Sylvester

On Nov 21, 2011, at 9:36 PM, hari mahesh wrote:

require ‘pdf/writer’
pdf.text @testcase, :font_size => 72, :justification => :center
require “testcase_lib”

Here you’re passing the variable ‘testcase’ which you have not defined
before.

report = Report.new testcase

@testcase = “Login Test”;

You probably want something like this:

report = Report.new “Login Test”

Or else:

testcase = “Login Test”
report = Report.new testcase

Here you will run into the same error; always define your variables
before you use them!

report.report result
result = “PASS”

On 22/11/11 09:36, hari mahesh wrote:

require ‘pdf/writer’
pdf.text @testcase, :font_size => 72, :justification => :center
require “testcase_lib”
What I was trying to do is, when i run I want to pass the testcase name
to testcase_lib.rb, which will be saved to PDF, after running the
testcase, the result Pass or Fail will be passed as result. I tried for
2 days but no matter what its not working. Please help

Looks like you are trying to use variables before they are defined.

Try this;

#testcase.rb

require ‘rubygems’
require ‘pdf/writer’
require “testcase_lib”

testcase = “Login Test”;
report = Report.new testcase

result = “PASS”
report.report result

Sam

Please help

Wow…thanks a lot it worked. So my another doubt arises. This was for 2
variables, similarly if i have for eg :

#testcase_lib.rb

def Name(testcase, module1, module2, final_result)
pdf = PDF::Writer.new
testcase= $testcase
pdf.text $testcase, :font_size => 72, :justification => :center
module1= $module1
pdf.text $module1, :font_size => 72, :justification => :center
module2= $module2
pdf.text $module2, :font_size => 72, :justification => :center
final_result1= $final_result1
pdf.text $final_result1, :font_size => 72, :justification => :center
pdf.save_as(“chunky.pdf”)
end

Firstly I need to print the Testcase name. Then module 1 will be run and
needs to print the module1 result then module2 result and then finally
the final result.

I have got experience in WATIR but I never used these classes and all in
WATIR, It was a real help from u guys and please help me to solve this
issue also.

This is my entire script

#report.rb
require ‘rubygems’
require ‘pdf/writer’
require ‘pdf/simpletable’

pdf = PDF::Writer.new
pdf.select_font “Helvetica”

inserting image

i0 = pdf.image “./logo.jpg”, :resize => 0.40, :justification => :right

pdf.move_pointer(20)
pdf.text “TEST RUN RESULTS”, :font_size => 23, :justification =>
:center
#pdf.rectangle(pdf.left_margin, y0, 325, 50).stroke

pdf.move_pointer(40)
#Creating tables
PDF::SimpleTable.new do |tab3|
pdf.text “Test Details”, :justification => :left,
:font_size => 14
tab3.column_order = [ “Module”, “version” ]
tab3.show_lines = :all
tab3.show_headings = false
tab3.orientation = :right
tab3.position = :left
tab3.width = 535
tab3.text_color = Color::RGB.new(0,0,225)

    data = [

      { "Module" => "Test Case Name", "version" => $tc_name },
      { "Module" => "Version", "version" => $version },
      { "Module" => "Tester", "version" => $tester }
      ]
    tab3.data.replace data
    tab3.render_on(pdf)

end
pdf.move_pointer(70)
PDF::SimpleTable.new do |tab1|
pdf.text “Test Results”, :justification => :left,
:font_size => 14
tab1.column_order = [ “Function1”, “result” ]
tab1.show_lines = :all
tab1.show_headings = false
tab1.shade_rows = true
tab1.orientation = :right
tab1.position = :left
tab1.width = 535
tab1.text_color = Color::RGB.new(0,0,0)
if $login_result == “PASS”
tab1.shade_color = Color::RGB.new(0,220,0)
else
tab1.shade_color = Color::RGB.new(220,0,0)
end
data = [

      { "Function1" => "Login", "result" => $login_result }
      ]
    tab1.data.replace data
    tab1.render_on(pdf)

end

PDF::SimpleTable.new do |tab2|
tab2.column_order = [ “Function2”, “result” ]
tab2.show_lines = :all
tab2.show_headings = false
tab2.shade_rows = true
tab2.orientation = :right
tab2.position = :left
tab2.width = 535
tab2.text_color = Color::RGB.new(0,0,0)
if $checkout_result == “PASS”
tab2.shade_color = Color::RGB.new(0,220,0)
else
tab2.shade_color = Color::RGB.new(220,0,0)
end
data = [
{ “Function2” => “Checkout”, “result” => $checkout_result }
]
tab2.data.replace data
tab2.render_on(pdf)
end

PDF::SimpleTable.new do |tab3|
tab3.column_order = [ “Function3”, “result” ]
tab3.show_lines = :all
tab3.show_headings = false
tab3.shade_rows = true
tab3.orientation = :right
tab3.position = :left
tab3.width = 535
tab3.text_color = Color::RGB.new(0,0,0)
$signout_result = " FAIL"
if $signout_result == “PASS”
tab3.shade_color = Color::RGB.new(0,220,0)
else
tab3.shade_color = Color::RGB.new(220,0,0)
end
data = [
{ “Function3” => “Signout”, “result” => $signout_result }
]
tab3.data.replace data
tab3.render_on(pdf)
end

pdf.move_pointer(90)
PDF::SimpleTable.new do |tab4|
pdf.text “Final Summary”, :justification => :left,
:font_size => 14
tab4.column_order = [ “Test”, “status” ]
tab4.show_lines = :all
tab4.show_headings = false
tab4.shade_rows = true
tab4.orientation = :right
tab4.position = :left
tab4.width = 535
tab4.text_color = Color::RGB.new(0,0,0)
tab4.shade_color = Color::RGB.new(0,220,0)

if $login_result == "PASS" && $checkout_result == "PASS" && 

$signout_result == “PASS”
$result = “PASS”
tab4.shade_color = Color::RGB.new(0,220,0)
else
$result = “FAIL”
tab4.shade_color = Color::RGB.new(220,0,0)
end
data = [
{ “Test” => “Final Result”, “status” => $result}
]
tab4.data.replace data
tab4.render_on(pdf)
end
pdf.save_as(“Report.pdf”)

and these are the variables i need to pass from the testcase

variables = $tc_name, $version, $tester, $login_result,
$checkout_result, $signout_result

On Tue, Nov 22, 2011 at 4:05 PM, hari mahesh [email protected]
wrote:

This is my entire script

Please use a public pastebin or a Gist for such a lengthy piece of
code. That makes it much easier to read. On first glance there seems
to be a lot of repetitive patterns in there so you may be able to
shrink this considerably.

Kind regards

robert

It will be great if someone give a reply since its bugging me for last
couple of days

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs