如何检测功能规范中是否启用了 javascript
How to detect if javascript is enabled in a feature spec
我有一个测试助手,如果它被启用了 javascript 的功能规范使用,则需要有不同的行为,例如
我有这样的测试:
it 'test something', js: true do
... (code that uses javascript)
special_method
end
还有一个这样的测试
it 'test something else' do
... (code that does not use javascript)
special_method
end
和特殊方法:
def special_method
... (lots form filling)
if js_true?
find_button('Update').trigger 'click'
else
click_button 'Update
end
end
其中 js_true?
是 returns true
如果调用功能规范具有 js: true
的某种方法。我需要此功能,因为当 js: true
页面显示不同并且 click_button
不再有效时。
有一些错误,这意味着当我尝试在启用 javascript 的情况下使用 click_button
时,我收到以下错误:
Capybara::Poltergeist::MouseEventFailed:
Firing a click at co-ordinates [73, 700.5] failed. Poltergeist detected another element with CSS selector 'html body.home-page div.cc-window.cc-banner.cc-type-info.cc-theme-block.cc-bottom.cc-color-override--1608664607 span#cookieconsent:desc.cc-message' at this position. It may be overlapping the element you are trying to interact with.
If you don't care about overlapping elements, try using node.trigger('click').
错误消息没有帮助,因为没有重叠元素,消息继续说:
# --- Caused by: ---
# Capybara::Poltergeist::BrowserError:
#
There was an error inside the PhantomJS portion of Poltergeist.
If this is the error returned, and not the cause of a more detailed error response, this is probably a bug, so please report it.
因此,在解决此错误之前,我需要一个解决方法来检测是否为测试启用了 javascript。
更新: 由于问题已更新以提供驱动此功能请求的实际错误消息,我们可以看到实际问题是一个重叠元素。我知道你声称没有重叠元素并且认为这纯粹是一个错误(因为 Poltergeist 已被弃用而永远不会被修复),但是从该错误消息来看肯定有一个重叠元素并且来自 class 名称对于重叠元素,我会说它是横跨屏幕底部的横幅,要求用户接受网站使用 cookie 的事实(save_and_open_screenshot
会确认这一点)。
无论如何,如果您选择不通过执行用户必须执行的操作(接受 cookie 横幅)来修复测试,那么您可以使用当前的 RSpec 示例元数据
def js_true?
RSpec.current_example.metadata[:js]
end
确定您当前是否正在参加设置了 :js
元数据的测试。
我想再次强调,在测试中使用 trigger
是一个非常糟糕的主意。它不会复制用户可以做的事情,会使转向更现代的驱动程序变得困难,并且最终会使它所使用的测试变得毫无意义。
------ 之前的答案 ------
你的问题令人困惑,因为你有一个带有 js: true
元数据的测试,这意味着它应该只 运行 用于支持 JS 的测试,但是你在其中调用了一个尝试的方法如果支持 JS,可以做一些不同的事情。不支持 JS 时如何调用它?
如果您以某种方式从不支持 JS 的驱动程序调用它,所有支持 JS 的驱动程序都应该以 true 响应 wait?
所以您应该能够执行 page.driver.wait?
或者您可以使用 RSpec.current_example.metadata
来确定是否设置了 js: true
- 但是并非所有支持 JS 的驱动程序都支持 trigger
并且如果您正在测试应用程序,您真的不应该使用 trigger
(如果只是从页面抓取数据)。如果您弄清楚为什么您认为需要使用 trigger
(它不会复制用户行为)并将其替换为用户实际可以执行的行为,那么您将进行更好的测试。
我有一个测试助手,如果它被启用了 javascript 的功能规范使用,则需要有不同的行为,例如
我有这样的测试:
it 'test something', js: true do
... (code that uses javascript)
special_method
end
还有一个这样的测试
it 'test something else' do
... (code that does not use javascript)
special_method
end
和特殊方法:
def special_method
... (lots form filling)
if js_true?
find_button('Update').trigger 'click'
else
click_button 'Update
end
end
其中 js_true?
是 returns true
如果调用功能规范具有 js: true
的某种方法。我需要此功能,因为当 js: true
页面显示不同并且 click_button
不再有效时。
有一些错误,这意味着当我尝试在启用 javascript 的情况下使用 click_button
时,我收到以下错误:
Capybara::Poltergeist::MouseEventFailed:
Firing a click at co-ordinates [73, 700.5] failed. Poltergeist detected another element with CSS selector 'html body.home-page div.cc-window.cc-banner.cc-type-info.cc-theme-block.cc-bottom.cc-color-override--1608664607 span#cookieconsent:desc.cc-message' at this position. It may be overlapping the element you are trying to interact with.
If you don't care about overlapping elements, try using node.trigger('click').
错误消息没有帮助,因为没有重叠元素,消息继续说:
# --- Caused by: ---
# Capybara::Poltergeist::BrowserError:
#
There was an error inside the PhantomJS portion of Poltergeist.
If this is the error returned, and not the cause of a more detailed error response, this is probably a bug, so please report it.
因此,在解决此错误之前,我需要一个解决方法来检测是否为测试启用了 javascript。
更新: 由于问题已更新以提供驱动此功能请求的实际错误消息,我们可以看到实际问题是一个重叠元素。我知道你声称没有重叠元素并且认为这纯粹是一个错误(因为 Poltergeist 已被弃用而永远不会被修复),但是从该错误消息来看肯定有一个重叠元素并且来自 class 名称对于重叠元素,我会说它是横跨屏幕底部的横幅,要求用户接受网站使用 cookie 的事实(save_and_open_screenshot
会确认这一点)。
无论如何,如果您选择不通过执行用户必须执行的操作(接受 cookie 横幅)来修复测试,那么您可以使用当前的 RSpec 示例元数据
def js_true?
RSpec.current_example.metadata[:js]
end
确定您当前是否正在参加设置了 :js
元数据的测试。
我想再次强调,在测试中使用 trigger
是一个非常糟糕的主意。它不会复制用户可以做的事情,会使转向更现代的驱动程序变得困难,并且最终会使它所使用的测试变得毫无意义。
------ 之前的答案 ------
你的问题令人困惑,因为你有一个带有 js: true
元数据的测试,这意味着它应该只 运行 用于支持 JS 的测试,但是你在其中调用了一个尝试的方法如果支持 JS,可以做一些不同的事情。不支持 JS 时如何调用它?
如果您以某种方式从不支持 JS 的驱动程序调用它,所有支持 JS 的驱动程序都应该以 true 响应 wait?
所以您应该能够执行 page.driver.wait?
或者您可以使用 RSpec.current_example.metadata
来确定是否设置了 js: true
- 但是并非所有支持 JS 的驱动程序都支持 trigger
并且如果您正在测试应用程序,您真的不应该使用 trigger
(如果只是从页面抓取数据)。如果您弄清楚为什么您认为需要使用 trigger
(它不会复制用户行为)并将其替换为用户实际可以执行的行为,那么您将进行更好的测试。