- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 Rails 中创建一些 JavaScript 功能时,我使用了一些丑陋的 Capybara 规范来帮助我确保一切正常。其中一个规范如下所示:
it 'creates a report document', js: true do
visit new_project_report_document_path @project
expect(page).to have_active_navigation_items 'Projects'
expect(page).to have_breadcrumbs 'A4AA 2.0', 'Projects', 'Project test name', 'Reports', 'Create'
expect(page).to have_headline 'Create Report'
expect {
select 'Template 1', from: 'report_report_template_id'
}.to change { page.has_css? "[data-template-inputs-id='#{@report_template_1.id}']", visible: true }.from(false)
.and change { page.has_css? "[data-template-inputs-id='#{@report_template_1.id}'] #report_name[disabled]", visible: false}.from(true)
.and change { page.has_css? "[data-template-inputs-id='#{@report_template_1.id}'] #report_description[disabled]", visible: false}.from(true)
.and change { page.has_css? "[data-template-inputs-id='#{@report_template_1.id}'] #report_intro[disabled]", visible: false}.from(true)
.and change { page.has_css? "[data-template-inputs-id='#{@report_template_1.id}'] #report_content[disabled]", visible: false}.from(true)
.and change { page.has_css? "[data-template-inputs-id='#{@report_template_1.id}'] #report_outro[disabled]", visible: false}.from(true)
expect(page).to have_css "[data-template-inputs-id='#{@report_template_1.id}'] #report_name", text: @report_template_1.name
expect(page).to have_css "[data-template-inputs-id='#{@report_template_1.id}'] #report_description", text: ''
expect(page).to have_css "[data-template-inputs-id='#{@report_template_1.id}'] #report_intro", text: @report_template_1.intro
expect(page).to have_css "[data-template-inputs-id='#{@report_template_1.id}'] #report_content", text: @report_template_1.content
expect(page).to have_css "[data-template-inputs-id='#{@report_template_1.id}'] #report_outro", text: @report_template_1.outro
expect {
select 'Template 2', from: 'report_report_template_id'
}.to change { page.has_css? "[data-template-inputs-id='#{@report_template_1.id}']", visible: true }.from(true)
.and change { page.has_css? "[data-template-inputs-id='#{@report_template_1.id}'] #report_name[disabled]", visible: false}.from(false)
.and change { page.has_css? "[data-template-inputs-id='#{@report_template_1.id}'] #report_description[disabled]", visible: false}.from(false)
.and change { page.has_css? "[data-template-inputs-id='#{@report_template_1.id}'] #report_intro[disabled]", visible: false}.from(false)
.and change { page.has_css? "[data-template-inputs-id='#{@report_template_1.id}'] #report_content[disabled]", visible: false}.from(false)
.and change { page.has_css? "[data-template-inputs-id='#{@report_template_1.id}'] #report_outro[disabled]", visible: false}.from(false)
.and change { page.has_css? "[data-template-inputs-id='#{@report_template_2.id}']", visible: true }.from(false)
.and change { page.has_css? "[data-template-inputs-id='#{@report_template_2.id}'] #report_name[disabled]", visible: false}.from(true)
.and change { page.has_css? "[data-template-inputs-id='#{@report_template_2.id}'] #report_description[disabled]", visible: false}.from(true)
.and change { page.has_css? "[data-template-inputs-id='#{@report_template_2.id}'] #report_intro[disabled]", visible: false}.from(true)
.and change { page.has_css? "[data-template-inputs-id='#{@report_template_2.id}'] #report_content[disabled]", visible: false}.from(true)
.and change { page.has_css? "[data-template-inputs-id='#{@report_template_2.id}'] #report_outro[disabled]", visible: false}.from(true)
expect(page).to have_css "[data-template-inputs-id='#{@report_template_2.id}'] #report_name", text: @report_template_2.name
expect(page).to have_css "[data-template-inputs-id='#{@report_template_2.id}'] #report_description", text: ''
expect(page).to have_css "[data-template-inputs-id='#{@report_template_2.id}'] #report_intro", text: @report_template_2.intro
expect(page).to have_css "[data-template-inputs-id='#{@report_template_2.id}'] #report_content", text: @report_template_2.content
expect(page).to have_css "[data-template-inputs-id='#{@report_template_2.id}'] #report_outro", text: @report_template_2.outro
fill_in 'report_name', with: 'newname'
fill_in 'report_description', with: 'newdescription'
fill_in 'report_intro', with: 'newintro'
fill_in 'report_content', with: 'newcontent'
fill_in 'report_outro', with: 'newoutro'
click_button 'Create Report'
expect(page).to have_flash 'Report was successfully created.'
end
我知道这是一个丑陋的规范,但我注意到它需要花费大量时间:大约 40 秒!
Finished in 41.86 seconds (files took 0.44731 seconds to load)
这是另一个激活了 JavaScript 的规范。虽然它小得多(并且只加载一个整页),但我认为它所需的时间比上面的要少得多:
it 'allows to remove an existing finding', js: true, focus: true do
visit edit_project_boilerplate_copy_path(@boilerplate_copy.project, @boilerplate_copy)
click_link 'Remove finding'
expect {
click_button 'Update Boilerplate'
} .to change { Finding.count }.by -1
end
大约需要 6-7 秒:
Finished in 6.62 seconds (files took 0.52104 seconds to load)
所以我想知道为什么第一个需要这么多时间。我的完整规范套件大约有 400 个规范,大约需要一分钟,所以通过添加新规范,它会增加到近 2 分钟!这是 Not Acceptable 。
那么:我的 JS 规范如何改进?这与许多 visible: false
语句有关吗?或者许多 and Change { ... }
的东西?也许两者都在互动?
当删除所有包含 visible: false
的行时,速度会更快:
Finished in 6.39 seconds (files took 0.41315 seconds to load)
我在 OSX El Capitan 上使用 capybara (2.5.0)、rspec (3.3.0)、rails (4.2.1) 和 poltergeist (1.7.0) 以及 phantomjs (1.9.8)。
最佳答案
你的测试之所以如此缓慢,是因为你遇到了 capybara 的等待行为。 #has_css?/have_css
将等待 Capybara.default_max_wait_time 秒,让匹配的元素出现在页面上,如果没有出现,则返回 false。当您期望某个元素不在您想要使用的页面上时,请使用 #has_no_css?/have_no_css (或 not_to #has_css?/have_css 因为它们最终是同一件事),因为一旦找不到该元素,它将立即返回.
has_css?(....) #will wait until element appears or default_max_wait_time
has_no_css?(....) #will wait until element is gone or default_max_wait_time
基本上就您的情况而言,您不想将 change
匹配器与 has_css 一起使用?因为它不允许您在操作的每一侧使用尽快匹配的方法。如果您确实想保留 change
匹配器,一个潜在的选择是将一个小的等待值传递给 has_css? wait: 0.5
或其他可以减少该项目的最大等待时间的值,但可能需要进行调整以允许页面上发生的任何操作实际完成
关于javascript - capybara 和JavaScript : checking for visibility requires huge amounts of time! 如何优化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35272416/
我制作了一个 DIV 和 visibility: hidden 并附加了一个 ::before 这个 DIV 的伪元素 visibility : 可见。这在 Firefox、Chrome 和 Safa
我正在使用 jquery 可见选择器来检查子元素是否可见。但令人惊讶的是 .is("visible") 和 .is(":visible") 在使用 css Visibility:hidden 时显示不
我有这个代码 #checkboxDIV { visibility: hidden; } #itemcard:hover #checkboxDIV {
我有一个用 ul 创建的菜单/li列出。 为了创造一个不错的效果,我有以下 css: #menu ul { /* ... */ visibility:hidden; /* ..
我想要的是,当我点击 Dashboard Button 时,它会像 SlidingDrawer 一样打开,打开后再次点击它会关闭。我使用这个自定义抽屉是因为 SlidingDrawer 已弃用。 现在
如何使用 jQuery 只选择可见元素? jQuery 选择器 :visible 和 :hidden 只尊重 display:none 作为真的隐藏?不是可见性:隐藏或可见性:可见。 我知道它们在技术
为什么我的 $('#myDiv').css('visibility', 'visible'); 不起作用? $('#clickdiv').click(function() { alert($(
在 $(document).ready 函数中通过 JavaScript 将子容器设置为 visibility: visible 时,我遇到了一个奇怪的问题。 问题是: 我有一个父元素,它有 boot
在 jQuery 中: e.is(':visible'); 检查元素是否显示。 jQuery 中是否有一个函数可以检查元素是否具有隐藏或可见属性可见性? 现在我必须自己实现这个功能。但我想使用 jQu
我在 mvc 中使用一个帖子表单,在帖子中我想显示一个隐藏的 div(惊喜)。 我正在使用 js/jquery 来显示 div,它可以在所有浏览器中工作,除了 mac 上的 safari :( 我现在
我似乎无法获得我的 jquery {{NotificationNavDot}} 功能正常工作! 在下面找到帮助文件: {{NotificationNavDot}} 在下面找到我的帮助
WPF 中的 Visibility.Collapsed 和 Visibility.Hidden 有什么区别? 最佳答案 不同之处在于,Visibility.Hidden隐藏了控件,但保留了它在布局中占
我有一个 if 语句来检查我的 div 下面是否没有任何可见内容,如果是,我会隐藏子元素的同级元素。 var $remainingprojects = $searchproject.s
这是我的菜单模型 HTML Menu 1 (overflow:hidden) Item 1 submenu 1 submen
编辑 3 实际上,我根本不需要弄乱可见性/不透明度...所以我删除了那些行(如编辑 2 所示)...添加了动画 -播放状态:暂停/运行.... 得到了我想要的效果... 编辑 2:感谢 Gineto
在我的应用程序中有一个广告 WebView ,它在 Activity 开始时调用一个 url。一切都很好,除了一件小事,它更像是一个可见性问题......所以问题是,当我开始 Activity 时,我
根据 the "visible" binding documentation , 如果 visible 的值为 false,Knockout 使用 display: none 隐藏元素。我怎样才能让它
* { margin: 0; padding: 0; border: 0; } .navBar { background-color: #2A2A2A; min-width: 10
android 布局使用 layout_weight。我的目标是所有组件的 1/3,但有时页脚实际上设置为消失,然后可见。从 gone 设置为 visible 时,权重计算如何工作?我没有看到具有 1
我有两个单选按钮,两个文本框和一个按钮。 当我点击第一个按钮时,应该只会出现一个文本框,当我点击第二个按钮时,应该会出现两个文本框。 但我想使用 visibility:hidden|visible 属
我是一名优秀的程序员,十分优秀!