出于某种原因,登录到 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 设置了这个简化示例,它似乎显示了同样的问题。
任何人都可以帮助将其变成通过测试吗?请注意,我可以使用同样基于 HTMLUnit 的 Celerity 进行类似的测试,所以我相信应该有某种方法可以使这项工作成功?
这是在 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)
最佳答案
我认为 Gmail 正在检测我们的 headless 浏览器(在本例中为 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)"
在这种情况下,我们可以转到 Gmail 的纯 HTML 版本来帮助您通过,但不幸的是,我认为我们现在被卡住了。我会将这个失败的测试用例传递给 webdriver 开发人员进行审查。
另外,希望这些不是您的真实 Gmail 凭据!
关于watir - 使用带有远程 HTMLUnit 的 Webdriver 的测试无法登录到 gmail,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4995393/