通过Watir-Webdriver访问Gmail中的验证链接

时间:2013-07-10 09:22:29

标签: ruby testing watir watir-webdriver

我是Watir的新手并且正在学习它。为了学习我已经开发了一个注册但完成注册的脚本,我需要通过我的GMAIL账户上发送的邮件进行验证。所以我已经安装了ruby GMAIL gem并经历了github的教程,并在下面制作了示例脚本:

require "watir-webdriver"
require "gmail"
br = Watir::Browser.new :chrome
br.goto "gmail.com"
gmail = Gmail.new("sample@registration.com", "111111")
gmail.inbox.count(:unread)
gmail.inbox.click(:unread, :from => "noreply@registration.com").label("Confirm Verification")

但是脚本没有工作,所以可能是错误,还有我如何访问任何邮件并点击验证链接。

1 个答案:

答案 0 :(得分:2)

您需要调整脚本以查找和阅读验证邮件。以下内容可用于阅读电子邮件。根据电子邮件的复杂程度和收件箱的内容,您可能需要进行一些调整。

verification_link = ''
Gmail.new("sample@registration.com", "111111") do |gmail|
    #Get the email the first email that is:
    #  unread and 
    #  from 'noreply@registration.com'
    email = gmail.inbox.emails(:unread, :from => 'noreply@registration.com').first

    #Get the message body, which will be in html
    message = email.body.decoded

    #Parse the message body for the link
    #  You may need to adjust the regex depending on the complexity of the email
    #  If the email is very complex, use Nokogiri to parse the html 
    verification_link = /<a.*?href="(.+?)".*?>Verify Now<\/a>/m.match(message)[1].gsub(/\s/, '')
    #=> "http://innovify.in/kwexcui/index.php?r=site/registerFirstSlap/key/3f21f205d603ce83e4dbd4667ff66a1f:2a/id/MTI2ODg2NjM4NTUyNw/lng/eng/email/maulik.goswami@bypeopletechnologies.in/companyName/R09PR0xFIFVLIExJTUlURUQ=/phoneNumber/012345678901234"
end

您将无法“点击”该链接。但是,它应该与在浏览器中导航到它相同 - 即将watir转到从电子邮件中提取的链接。

br = Watir::Browser.new :chrome
br.goto verification_link