简体   繁体   English

瓦蒂尔和露比; 里面有几个初学者“我该怎么做”的问题

[英]Watir & Ruby; A couple beginner “how can I do this” questions inside

Programming newbie here, about a month in with Ruby. 在这里使用Ruby编写新手程序大约需要一个月的时间。 I'm currently playing around with Watir and have a few questions to streamline the automation process of a simple form fill. 我目前正在与Watir一起玩,并且有几个问题可以简化简单表单填写的自动化过程。

Current Code: 当前代码:

# Start Watir
require 'watir-webdriver'
# Open Firefox w/ Watir
browser = Watir::Browser.new :ff
# Visit URL w/ browser
browser.goto("http://website.com")
# Fill in page name field
browser.text_field(:name => "page_name").set "page_name1"
# Fill in password field
browser.text_field(:name => "password").set "static_password"
# Click publish button
browser.button(:value => "Publish").click
# Puts URL
puts browser.url

What I'd like to add on to this is: 我想补充的是:

  • Loop the process XXX amount of times (do, end?) 循环处理XXX次(结束,结束?)
  • Pull the page name from an ordered list (array?), don't use same name twice 从有序列表(数组?)中提取页面名称,不要两次使用相同的名称
  • After the page is published, group & save the page name + URL + password (hash?) 页面发布后,分组并保存页面名称+ URL +密码(哈希?)

Any guidance on this is much appreciated, I'm not getting too far guessing, I just need a push in the right direction. 非常感谢您对此提供任何指导,我不会太过猜测,我只需要朝着正确的方向前进即可。

  • Loop: xxx.times do ... end 循环: xxx.times do ... end
  • Page name array: page_names.each do |page_name| ... end 页面名称数组: page_names.each do |page_name| ... end page_names.each do |page_name| ... end
  • How about a nested hash? 嵌套哈希怎么样? Use the URL as key for the outer hash and the name and password as keys in the inner hash. 使用URL作为外部哈希的键,并使用名称和密码作为内部哈希的键。
 { "http://foo.com" => { :name => "Foo", :password => "super_s3kr3t" } "http://bar.com" => { :name => "Bar", :password => "also_s3kr3t" } } 

Ruby has a wonderful concept called iterables which can help with this. Ruby有一个很棒的概念叫iterables,它可以帮助您解决这个问题。

First bullet. 第一颗子弹。 looping a given number of times, lets say 100 for example. 循环给定的次数,例如100次。 There about 10 different ways to skin this cat, but my favorite is just this 大约有10种方法可以给这只猫换皮,但我最喜欢的就是

100.times do
  #code lines to be repeated
end

Second bullet, which likely changes the approach presuming what you want is to do something once for every line in the array of site names. 第二个项目符号,它可能会更改方法,假设您想要对站点名称数组中的每一行执行一次操作。

First create your array, then make use of the wonderful .each method to do something with each row of the array. 首先创建数组,然后使用美妙的.each方法对数组的每一行进行处理。 If your array was named page_names then 如果您的数组名为page_names

page_names.each do |name|
  #code to be repeated, use `name` variable at point you need the value
  browser.text_field(:name => "page_name").set name
end

This .each method can be used with just about any 'collection' of items, which means you can end up doing stuff that looks like this 该.each方法几乎可以与任何“集合”项一起使用,这意味着您最终可以做类似这样的事情

pages.each do |page|
  code that does something with page.name
  code that does something with page.password
end

Third bullet, yep Michael (who I saw just posted an answer) has what I'd have suggested which is a nested hash, although since you are starting with page names, and that's the part you know, and the URL is something the script is gathering, I'd use the page_name as the key to the outer hash that would let you look up the url and password values for each page name. 第三点,是的,迈克尔(我看到刚刚发布了答案)具有我建议的嵌套哈希值,尽管由于您以页面名称开头,所以这是您所知道的部分,而URL是脚本的一部分正在收集时,我将使用page_name作为外部哈希的键,该哈希可让您查找每个页面名称的url和密码值。 (I'm presuming in that case you would be doing something like generating the passwwords randomly and thus the need to store them) (我想在这种情况下,您将做一些类似随机生成密码的事情,因此需要存储它们)

Combining the above with .each it's important to know that in that case of using .each with hashes, the first thing you get back from .each is the key, and then the value. 将以上内容与.each结合使用时,重要的是要知道在将.each与哈希一起使用的情况下,从.each中获得的第一件事是关键,然后是价值。 So if you had an the nested hash discussed above prepopulated with passwords, and keyed on page names, you might do something like this 因此,如果您有一个上面讨论过的嵌套哈希,并预先填充了密码,并以页面名称作为关键字,则可能会执行以下操作

pages.each do |page_name, pageinfo|
  browser.goto("http://website.com")
  browser.text_field(:name => "page_name").set page_name
  browser.text_field(:name => "password").set pageinfo[:password]
  browser.button(:value => "Publish").click
  pages[page_name][:url] = browser.url
end

(given murphy loves me I expect I've made some mistake above, which another rubiest will no doubt spot.. fortunately you can edit these answers to fix that stuff later) (考虑到墨菲爱我,我希望我在上面犯了一些错误,毫无疑问,这是另一个最红宝石的地方。

What I would do is create an array for all of the page names and iterate through it. 我要做的是为所有页面名称创建一个数组并对其进行迭代。

pages = ["page_name1", "page_name2", "page_name3"]

pages.each {|page| browser.text_field(:name => "page_name").set page}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM