简体   繁体   English

测试黄瓜中javascript的内容

[英]test contents of javascript in cucumber

I have an application, Rails, tested with Cucumber. 我有一个经过Cucumber测试的应用程序Rails。 This application has a Google Map and I want a Cucumber-step to test whether or not that map has some "Pins" (Markers). 此应用程序有一个Google Map,我希望执行Cucumber步骤来测试该地图是否具有一些“ Pins”(标记)。 As far as I know, the pins cannot be found in the DOM, so I thought to simply test, whether or not the page calls map.addMarker() with certain parameters. 据我所知,在DOM中找不到这些引脚,因此我想简单地测试一下该页面是否使用某些参数调用map.addMarker()

Then /^I should see the top 50 campings on the map$/ do
  Camping.geocoded.top(50).each do |camping|
    page.should have_content /map\.addMarker\(#{camping.latitude}, #{camping.longitude}, "#{camping.name}"\)/
  end
end

The page has a script tag at the bottom that gets filled with a content_for , the source has: 该页面的底部有一个script标记,其中填充有content_for ,源代码具有:

<script>
  $(document).ready(map.addMarker(51.7780246, 5.97571, "Bij Ons"))
</script>
</body>

map.addMarker is some custom JavaScript, which adds a marker to the map. map.addMarker是一些自定义JavaScript,可将标记添加到地图。

I am not (really) interested in testing whether my custom JavaScript and the Google maps API actually turns that call into a visible pin (although that would be neat); 我(真的)对测试我的自定义JavaScript和Google Maps API是否实际上将该调用转换为可见的引脚(尽管那很整洁)不感兴趣; for now simply testing if that JavaScript method gets called will do. 现在,只需测试该JavaScript方法是否可以被调用即可。

How can I test whether page contains certain JavaScript? 如何测试page是否包含某些JavaScript? The code in the Then step above, only tests for actual content and omits the content in the <script> tag. 上面的“ Then步骤中的代码仅测试实际内容,并忽略了<script>标记中的内容。

诀窍是不使用have_content ,而是使用更通用的选择器,例如have_xpath

page.should have_xpath "//script[contains(.,'map.addMarker(#{camping.latitude}, #{camping.longitude}, \"#{camping.name}\")')]"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM