我对 Ruby 非常陌生,并且使用 Watir 开发脚本以进行验收测试,并且我陷入了一种我什至不确定是否有可能摆脱的陈规之中。
我有一个运行的小脚本,它基本上查看一个下拉列表,将所有下拉项存储到一个全局变量中,然后我对其进行迭代并运行一些其他函数。
当我在运行每个下拉项的所有功能后尝试刷新浏览器时会出现此问题,然后尝试迭代到下一个下拉项并执行相同的功能,直到所有下拉项都完成。
$browser = Watir::Browser.new :chrome
$wait = Watir::Wait
def wait_while_loading(what)
lightbox = $browser.div(:class , 'lightbox')
begin
# Capture load start
start = Time.now
$wait.until { lightbox.present? }
$wait.until { !lightbox.present? }
# Capture load end
finish = Time.now
load_time = (finish - start).to_s
rescue => e
print e.backtrace.join("\n")
#log_error('Failed while loading ' + what + '.')
end
end
def cycle_all_views
$views = []
$current_view
$view_dropdown = $browser.div(:id, 'views-dropdown')
$views = $browser.div(:class, 'dropdown-views').div(:class, 'dropdown-menu--footer').ul(:class, 'dropdown-menu').divs(:class, 'dropdown-menu-item')
start_on_view = 10
begin
$views.drop(start_on_view).each_with_index do |view, i|
$view_dropdown.click
$current_view = view.text
index = i.to_s
safe_name = sanitize($current_view)
view.click
wait_while_loading($current_view)
$browser.refresh
wait_while_loading('Refreshing Page...')
end
rescue => e
#log_error('Failed to load view ' + $current_view)
end
end
触发后$browser.refresh
,单击$views_dropdown
后迭代到下一个view
,我收到一个错误,说明no implicit conversion of nil into String
在尝试执行时假设$current_view = view.text
。页面刷新后是否会丢失对变量的所有分配?如果是这样,有没有办法保留它们?
如果有人还可以指出我调试 Watir 脚本的更好方法,我很想知道有什么方法可以让我的生活更轻松。
堆栈跟踪返回:
C:/Ruby23/lib/ruby/gems/2.3.0/gems/watir-webdriver-0.9.3/lib/watir-webdriver/ele
ments/element.rb:536:in `assert_element_found'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/watir-webdriver-0.9.3/lib/watir-webdriver/ele
ments/element.rb:524:in `ensure_not_stale'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/watir-webdriver-0.9.3/lib/watir-webdriver/ele
ments/element.rb:503:in `assert_exists'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/watir-webdriver-0.9.3/lib/watir-webdriver/ele
ments/element.rb:81:in `text'
main.rb:55:in `block in cycle_all_views'
main.rb:52:in `each'
main.rb:52:in `each_with_index'
main.rb:52:in `cycle_all_views'
main.rb:122:in `block in <main>'
main.rb:112:in `each'
main.rb:112:in `<main>'Failed to load view First View