开发者

How can I create log files on loop using mechanize with ruby

I am trying to make more than one log file on localhost

one file is 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

the second is 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

how can I combine these two files and run them on loop in order to create more than one log file


I don't know much about Mechanize, but is there any reason you can't simply combine the two bits of code and put them a while loop? I don't know how often you need to do Mechanize.new. To make more than one log file, simply open two different files and write to them.

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜