使用Webdriver和远程HTMLUnit进行测试时无法登录gmail

时间:2011-02-14 17:44:36

标签: watir htmlunit webdriver

出于某种原因,登录到像gmail这样的网站后,htmlunit无效。它无法找到html元素。

以下是一个非常简单的ruby脚本来显示问题,请注意它假设webdriver服务器在运行它的同一台机器上运行:

require 'rubygems'
require 'watir-webdriver'
require 'rspec/expectations'

##
## THE FOLLOWING TWO WAYS WORK
#
#browser = Watir::Browser.new(:remote, :url => "http://127.0.0.1:4444/wd/hub", :desired_capabilities => :firefox)
#browser = Watir::Browser.new(:remote, :url => "http://127.0.0.1:4444/wd/hub", :desired_capabilities => :internet_explorer)

##
## THIS WAY FAILS
##
capabilities = Selenium::WebDriver::Remote::Capabilities.htmlunit(:javascript_enabled => true)
browser = Watir::Browser.new(:remote, :url => "http://127.0.0.1:4444/wd/hub", :desired_capabilities => capabilities)

#Login to gmail
browser.goto "http://gmail.com"
browser.text_field(:id,'Email').set 'roberttestingstuff041'
browser.text_field(:id,'Passwd').set 'k4238chsj55983w'
browser.button(:id,'signIn').click

sleep 5.0  #sleep shouldnt be needed, but just to be sure we are waiting long enough for log in to complete

frame = browser.frame(:id,'canvas_frame')
#It fails on the next line when using htmlunit
frame.link(:text, 'Sign out').exist?.should == true
frame.link(:text, 'Sign out').visible?.should == true
frame.div(:id, 'guser').exist?.should == true
frame.div(:text,'Compose mail').exist?.should == true

请注意,如果我使用firefox或IE创建浏览器对象,则可以使用此简单测试。

似乎挂起了登录过程中发生的重定向。我真正尝试测试的网站遵循一个非常相似的模式,所以我用gmail设置了这个简化的例子,这似乎表明了同样的问题。

任何人都可以帮忙把它变成通过测试吗?请注意,我可以使用Celerity进行类似的测试,它也基于HTMLUnit,所以我相信应该有一些方法可以让它工作吗?

这是webdriver服务器中显示的错误,清楚地显示它无法找到属性:

12:31:16.321 INFO - WebDriver remote server: INFO: Executing: [find element: By.xpath: .//a[normalize-space()='Sign out'
] at URL: /session/1297704604365/element)
12:31:17.996 WARN - WebDriver remote server: WARN:
org.openqa.selenium.NoSuchElementException: Unable to locate a node using .//a[normalize-space()='Sign out']
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_21'
Driver info: driver.version: EventFiringWebDriver
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByXPath(HtmlUnitDriver.java:699)
        at org.openqa.selenium.By$6.findElement(By.java:205)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver$4.call(HtmlUnitDriver.java:1133)

2 个答案:

答案 0 :(得分:3)

我认为Gmail正在检测我们的无头浏览器(在这种情况下,HtmlUnit with Rhino)不支持JavaScript。

如果您在

之后查看Gmail的退货
 browser.button(:id,'signIn').click

您会看到我们正在使用“必须启用JavaScript”页面

p browser.text
 "<style> #loading {display:none} </style> <font face=arial>JavaScript must be enabled in order for you to use Gmail in standard view. However, it seems JavaScript is either disabled or not supported by your browser. To use standard view, enable JavaScript by changing your browser options, then <a href=\"\">try again</a>. <p>To use Gmail's basic HTML view, which does not require JavaScript, <a href=\"?ui=html&zy=c\">click here</a>.</p></font><p><font face=arial>If you want to view Gmail on a mobile phone or similar device <a href=\"?ui=mobile&zyp=c\">click here</a>.</font></p> \n Loading tim.koops@gmail.com\342\200\246 \n\n\n\n Loading standard view | Load basic HTML (for slow connections)"

在这种情况下,我们可以使用仅限HTML的Gmail版本来帮助您完成,但不幸的是我认为我们现在已经陷入困境。我将把这个失败的测试用例传递给webdriver开发人员进行审核。

另外,希望这些不是您真正的Gmail凭据!

答案 1 :(得分:0)

您需要为HtmlUnit驱动程序启用javascript。 它默认是禁用的。 使用Capability HTMLUNITWITHJS而不是默认的HTMLUNIT。 我正在使用Python绑定中的名称,但我确信Ruby正在使用类似的东西。

相关问题