用于ruby的Selenium WebDriver因“重定向太多”而失败

时间:2012-09-12 12:51:54

标签: ruby selenium webdriver testunit

我有一个测试登录表单的简单测试用例。 由于某种原因,webdriver拒绝运行测试并返回“太多重定向”消息。该页面只是一个普通的登录屏幕,非常简单,没有任何重定向。从服务器到页面的访问似乎没问题。

我在centos服务器上使用selenium-webdriver-2.25.0。

错误信息下方:

(...)

[WARNING] MultiJson is using the default adapter (ok_json). We recommend loading a different JSON library to improve performance.
EE
Finished in 0.206445 seconds.

1) Error: test_login(Login):
Selenium::WebDriver::Error::WebDriverError: too many redirects
/usr/lib/ruby/gems/1.8/gems/selenium-webdriver-2.25.0/lib/selenium/webdriver/remote/http/default.rb:62:in `request'
/usr/lib/ruby/gems/1.8/gems/selenium-webdriver-2.25.0/lib/selenium/webdriver/remote/http/default.rb:63:in `request'
/usr/lib/ruby/gems/1.8/gems/selenium-webdriver-2.25.0/lib/selenium/webdriver/remote/http/common.rb:40:in `call'

(...)

我的代码:

require "rubygems"
require "selenium-webdriver"
require "test/unit"

class Login < Test::Unit::TestCase

  def setup
    @driver =Selenium::WebDriver.for(:remote, :url => "http://selenium.server.com/wd/hub")
    @base_url = "http://www.myservice.com"
    @driver.manage.timeouts.implicit_wait = 30
    @verification_errors = []
  end

  def teardown
    @driver.quit
    assert_equal [], @verification_errors
  end

  def test_login
    @driver.get(@base_url + "/login/")
    @driver.find_element(:id, "username").clear
    @driver.find_element(:id, "username").send_keys "user@server"
    @driver.find_element(:id, "password").clear
    @driver.find_element(:id, "password").send_keys "mykeys!"
    @driver.find_element(:xpath, "//input[@value='Login']").click
    verify { assert element_present?(:link, "Logout") }
    verify { assert element_present?(:link, "Settings") }
    verify { assert element_present?(:link, "Products") }
  end

  def element_present?(how, what)
    @driver.find_element(how, what)
    true
  rescue Selenium::WebDriver::Error::NoSuchElementError
    false
  end

  def verify(&blk)
    yield
  rescue Test::Unit::AssertionFailedError => ex
    @verificatiohttp://jenkins.dev.emesa-auctions.com/cms/n_errors << ex
  end
end

更新

似乎无论我使用什么网址为“基本网址”,错误都会不断发生。

1 个答案:

答案 0 :(得分:0)

我自己设法解决了这个问题。问题是我通过面向公众的URL(selenium.server.com)而不是通过内部局域网(绕过防火墙)访问服务器。

更改修复问题。