如何使用ruby的mechanize在循环上创建日志文件

时间:2011-07-03 18:48:24

标签: ruby-on-rails ruby mechanize

我正在尝试在localhost上创建多个日志文件

一个文件是sign_in.rb

require 'mechanize'

@agent = Mechanize.new
page = @agent.get('http://localhost:3000/users/sign_in')
form =page.forms.first
form["user[username]"] ='admin'
form["user[password]"]= '123456'
@agent.submit(form,form.buttons.first)

pp page

第二个是profile_page.rb

require 'mechanize'
require_relative 'sign_in'


page = @agent.get('http://localhost:3000/users/admin')
form =page.forms.first

form.radiobuttons_with(:name => 'read_permission_level')[1].check
@agent.submit(form,form.buttons.first)


pp page

如何组合这两个文件并在循环中运行它们以创建多个日志文件

1 个答案:

答案 0 :(得分:0)

我对Mechanize了解不多,但是有什么理由你不能简单地将两位代码组合起来并将它们放在一个while循环中吗?我不知道你需要多久做一次Mechanize.new。要创建多个日志文件,只需打开两个不同的文件并写入即可。

require 'mechanize'
require_relative 'sign_in'

log1 = File.open("first.log", "w")
log2 = File.open("second.log", "w")

@agent = Mechanize.new
while true
    # @agent = Mechanize.new  # not sure if this is needed
    page = @agent.get('http://localhost:3000/users/sign_in')
    form = page.forms.first
    form["user[username]"] ='admin'
    form["user[password]"]= '123456'
    @agent.submit(form,form.buttons.first)
    PP.pp page, log1

    # @agent = Mechanize.new  # not sure if this is needed
    page = @agent.get('http://localhost:3000/users/admin')
    form = page.forms.first
    form.radiobuttons_with(:name => 'read_permission_level')[1].check
    @agent.submit(form,form.buttons.first)
    PP.pp page, log2
end