【发布时间】:2014-12-18 13:55:28
【问题描述】:
我正在使用 Cucumber 和 watir-webdriver 为我的应用程序编写自动化测试。我的目标是清除以下多选列表中默认选择的“添加”和“CHG”选项,然后重新选择添加选项。
<select name="actions_arr" id="actions_arr" style="width: 190px; height: 110px;" multiple="multiple">
<option value="Any">
<option value="CHG">
<option value="ADD">
<input name="_actions_arr" type="hidden" value="1"/>
我编写了以下 ruby 函数(包括一些诊断调试打印命令)来清除选择列表。在这种情况下,变量 action_type = ADD。问题是,当我在无头模式下运行浏览器时,我无法清除选定的选项,并且我得到一个 watir 异常,说该列表不是多选列表,即使它确实是。但是,如果我使用 IE8 浏览器的本地实例驱动应用程序,则选择列表被识别为多选列表,我可以清除获得预期结果的选项。
def set_action_type(action_type)
s = @browser.div(:id => 'tabs').frame(:id => 'container').div(:id => 'sidebar').select_list(:id => 'actions_arr')
puts "Is the combo list visible?"
puts s.visible?
puts "is the list a multi-select list?"
puts s.multiple?
s.clear
puts "Has the ADD option been selected?"
puts s.selected?action_type
puts "Has the CHG option been selected?"
puts s.selected? 'CHG'
puts s.selected_options
end
1) 使用无头浏览器驱动应用程序时,我得到以下输出:
Is the combo list visible?
true
is the list a multi-select list?
false
Watir::Exception::Error: you can only clear multi-selects
./features/step_definitions/atom_steps.rb:43:in `set_action_type'
./features/step_definitions/atom_steps.rb:169:in `/^I select the "(.*?)" action type$/'
E:\ATOM_TEST\AcceptanceTest\atom\features\filtering.feature:10:in `And I select the "ADD" action type'
1 scenario (1 failed)
5 steps (1 failed, 4 passed)
0m32.188s
Process finished with exit code 1
2) 在驱动 IE8 浏览器的本地实例时,我得到以下正确的输出(忽略场景失败消息,因为所有测试步骤实际上都通过了):
Is the combo list visible?
true
is the list a multi-select list?
true
Has the ADD option been selected?
false
Has the CHG option been selected?
false
[]
1 scenario (1 failed)
5 steps (5 passed)
2m1.766s
Process finished with exit code 1
我的问题是
- 在无头浏览器模式下运行时,如何使上述多选列表测试通过,以便我可以使用 s.clear 清除 select_list 中的选项?
- 还有其他解决方法吗?
关于 2) 我正在尝试使用 selenium webdriver 中的 actionbuilder 类发送 CONTROL 和鼠标单击选择命令,以模拟用户通过 UI 执行的清除选定选项的操作。
请注意我使用的以下代码以无头模式启动浏览器:
require 'watir-webdriver'
require 'selenium/server'
include Selenium
Before do
@server = Selenium::Server.new("selenium-server-standalone-2.0b1.jar", :background => true)
@server.start # run your tests
capabilities = WebDriver::Remote::Capabilities.htmlunit(:javascript_enabled => true)
@browser = Watir::Browser.new(:remote, :url => 'http://127.0.0.1:4444/wd/hub', :desired_capabilities => capabilities)
#Uncomment the below line and comment out the above lines if you don't want to run in headless mode
#@browser = Watir::Browser.new :ie
end
After do
@browser.link(:text => 'Logout').click
@browser.close
@server.stop
end
【问题讨论】:
-
如果您创建一个只有多选列表的页面,
s.multiple?是否仍然总是为您返回false?我尝试重现您的问题(使用 HtmlUnitDriver),但s.multiple?按预期返回了true。 -
@JustinKo 感谢您的建议。我试过了,
s.multiple?总是返回false -
您使用的是什么版本的 Selenium-WebDriver 和 Watir-WebDriver?我注意到您的代码显示了“selenium-server-standalone-2.0b1.jar”的用法,它已经 4 岁了?
-
我的开发环境是:Windows 2003 Server、IE8(本地浏览器)、Ruby 1.9.3 -p194、selenium-webdriver v2.26.0和watir-webdriver v0.6.1。我应该更新其中的任何一个吗?当您尝试重现您的环境设置时?
-
忘记补充说我使用的是“selenium-server-standalone-2.0b1.jar”
标签: ruby selenium-webdriver watir watir-webdriver