Capybara不识别动态添加的DOM元素? [英] Capybara doesn't recognize dynamically added DOM elements?

查看:139
本文介绍了Capybara不识别动态添加的DOM元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用Capybara和Selenium时,我似乎无法测试使用jQuery做的漂亮的JavaScript动作。预期的行为是当用户点击链接添加资源时动态生成表单。 Capybara将能够点击链接,但无法识别新的表单元素(即resource [name])。



有没有办法重新加载DOM对于水豚来说,还是还有一些我还没有学到的宝石的元素?



提前感谢!



==编辑==



目前正在尝试我的运气与硒:

  wait_for_element 

方法。 b
$ b

==编辑==



我继续收到一个undefined方法wait_for_element为nill类尝试执行以下操作时:

  @ selenium.wait_for_element 

看来,具体的方法,或者可能 wait_for 与一个巨大的选择器访问我预期的DOM元素是正确的行动方针,但现在试图让硒会话开始是一个巨大的头痛。

解决方案

我在RSpec中使用基于Webdriver的Capybara驱动,我配置和使用像这样,它一定会处理JS,不需要重新加载dom。关键是使用一个wait_until和一个条件,当你的AJAX响应结束时,它将成立。

  before(:each)do 
select_driver(示例)
logout
login('databanks')
end

def select_driver(示例)
如果exam​​ple.metadata [ :js]
Capybara.current_driver =:selenium
else
Capybara.use_default_driver
end
end

它应该让我删除一个场景,:js => true do
select(Mysite Search,:from =>'scenario_id')
wait_until {page.has_content?('mysite_searchterms')}
click_ondelete
wait_until {!page.has_content?('mysite_searchterms')}
访问'/ databanks'
page.should_not have_content('Mysite Search')
end

我还想出了一个黑客,以减慢webdriver昨天晚上,像这样,如果你想看东西在slo-mo:

  #set命令延迟
需要'selenium-webdriver'

module :: Selenium :: WebDriver :: Remote
class Bridge
def execute(* args)
res = raw_execute(* args)['value']
sleep 0.5
res
end
end
end

正如其他人所说,如果您正在等待该元素超时,您可以查看以下内容:

  Capybara.default_wait_time = 10 


I seem to be having trouble testing the slick javascript things I do with jQuery when using Capybara and Selenium. The expected behavior is for a form to be dynamically generated when a user clicks on the link "add resource". Capybara will be able to click the link, but fails to recognize the new form elements (i.e. "resource[name]").

Is there a way to reload the DOM for Capybara, or is there some element of this gem that I just haven't learned of yet?

Thanks in advance!

==Edit==

Currently trying my luck with selenium's:

wait_for_element 

method.

==Edit==

I keep getting an "undefined method 'wait_for_element` for nill class" when attempting to do the following:

@selenium.wait_for_element

It appears that that specific method, or perhaps wait_for with a huge selector accessing the DOM element I expect is the correct course of action, but now trying to get the selenium session is starting to be a huge headache.

解决方案

I use the Webdriver based driver for Capybara in RSpec, which I configure and use like this and it will definitely handle JS and doesn't need a reload of the dom. The key is using a wait_until and a condition that will be true when your AJAX response has finished.

before(:each) do
  select_driver(example)  
  logout
  login('databanks') 
end

 def select_driver(example)
   if example.metadata[:js]
    Capybara.current_driver = :selenium
  else
    Capybara.use_default_driver
  end
end

it "should let me delete a scenario", :js=>true do
    select("Mysite Search", :from=>'scenario_id')  
    wait_until{ page.has_content?('mysite_searchterms')}    
    click_on "delete"     
    wait_until{ !page.has_content?('mysite_searchterms')}   
    visit '/databanks'
    page.should_not have_content('Mysite Search')
  end

I also figured out a hack to slow down webdriver last night, like this, if you want to watch things in slo-mo:

   #set a command delay
   require 'selenium-webdriver'

   module ::Selenium::WebDriver::Remote
     class Bridge
       def execute(*args)
         res = raw_execute(*args)['value']
         sleep 0.5
         res
       end
     end
   end  

As someone else mentioned, if you are getting a timeout waiting for the element, you could look at upping this:

Capybara.default_wait_time = 10

这篇关于Capybara不识别动态添加的DOM元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆