Rails3 Google Maps 使用 selenium 进行测试 [英] Rails3 Google Maps testing with selenium

查看:41
本文介绍了Rails3 Google Maps 使用 selenium 进行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Rails3 测试一个谷歌地图应用程序.我正在使用带有水豚和硒的黄瓜进行 JavaScript 测试.我有一张地图,我等待加载谷歌地图,然后向我的服务器发送一个 ajax 请求,在那里我得到我要插入地图的位置.我想知道 selenium 是否可以等到加载谷歌地图,对我的服务器的 ajax 调用完成并且标记被放置在地图内.另一个问题是,如何在谷歌地图中选择这个标记.有选择器吗?

I'm trying to test a google maps app with Rails3. I'm using cucumber with capybara and selenium for JavaScript testing. I have a map where I wait for google maps to be loaded, and then send an ajax request to my server, where I get locations that I'm inserting into the map. I'm wondering if it's possible with selenium to wait until google maps is loaded, the ajax call to my server is finished and the marker are placed inside the map. The other issue is, how to select this marker within google maps. Are there any selectors?

或者我应该采取其他方式,并使用像 Jasmine 这样的 JS 测试框架来测试我的类是否已加载等等.我对 Jasmine 没有任何经验,所以可以测试谷歌地图吗?

Or should I go the other way, and use a JS testing framework like Jasmine to test if my classes are loaded and so on. I don't have any experience with Jasmine, so is it possible to test a google maps?

也许有人知道一个解决方案,或者一个提示(如果不可能的话),或者一个解决方法或者......;)

Maybe someone knows a solution, or a hint if it's not possible, or a workaround or... ;)

[更新 1]

<罢工>我发现了如何在谷歌地图中选择标记.如果您查看 谷歌硒测试你可以看看他们在做什么.例如选择一个标记:

I've found out how to select markers in google maps. If you look at googles selenium tests you can check out what they are doing. For example selecting a marker:

waitForElementPresent   xpath=//img[contains(@src,'marker')]

但是下一个问题来了.如何选择特定标记?javascript google maps API 中是否有办法为其分配 ID,以便我可以使用 #marker_1#marker_2...?

But here comes the next problem. How do I select a specific marker? Is there a way inside the javascript google maps API to assign an ID to it, so that I can use #marker_1, #marker_2...?

另一个奇怪的事情是,像 wait_for_elementwait_for_condition 这样的函数在我的黄瓜步骤定义中不可用.谷歌硒测试是否使用像 waitForElementPresent 这样的自己的函数?还是这是标准的硒功能?我发现很多帖子,他们总是使用类似的东西

And another strange thing is, that function like wait_for_element or wait_for_condition aren't available inside my cucumber step definitions. Are the google selenium tests using own function like that waitForElementPresent? Or are this standard selenium functions? I've found a lots of posts, where they always use something like

selenium.wait_for_condition
selenium.wait_for_element

or

@selenium.wait_for_condition
...

在我的步骤定义中,selenium@selenium var 为零.我怎样才能访问这些方法?我还发现了这个帖子,但它是从 08 年 10 月开始的,所以我认为必须有更好的解决方案(顺便说一句.这个解决方案一见钟情).

Inside my step definitions the selenium and the @selenium var a nil. How can I access this methods? I've also found this post, but it is from Oct. '08, so I think there must be a better solution (btw. this solution works on the first sight).

喜欢这个page,他们概述了一些 selenium 方法如何等待条件或元素.这还存在吗?如何使用这些功能?

Like on this page, they give an overview of a few selenium methods how to wait for a condition or element. Is this still present? How can I use this functions?

[更新 2]

该死,我发现我上面提到的硒测试是针对谷歌地图的 V2 的,而不是针对 V3 的.我试过了

Damn it, I've found out, that the selenium tests I mentioned above are for V2 of google maps, not for V3. I have tried it with

wait_until { page.has_xpath?("//img[contains(@src,'marker')]") }

但它不起作用.该标记在地图上可见,但我收到超时错误,因为使用此 XPath 选择器找不到它.我想知道是否通常可以从 DOM 中选择一个标记.

But it doesn't work. The marker is visible on the map, but I get a timeout error, because with this XPath selector it is not found. I'm wondering if it is generally possible to select a marker out of the DOM.

我还尝试在创建标记时为其分配一个附加属性:

I also tried to assign an additional attribute to the marker when I create it:

// marker is the marker returned by google maps after adding it to the map
$(marker).attr('id', "marker_1");

但是当我尝试使用 jQuery 选择器 $("#marker_1") 访问它时,它没有找到它.所以,仍然没有解决方案.

But when I try to access it with the jQuery selector $("#marker_1"), it doesn't find it. So, still no solution, yet.

推荐答案

我的做法是像这样执行步骤定义中的调用:

What I do with mine is to execute the calls in your step definitions like so:

page.execute_script("launchmap()")

page.execute_script("launchmap()")

然后检查它们在页面中是否存在..然后在 capybara 中进行正常的 ajax 检查.标记将包含在 div 中,对吗?那么如果你调用 launchmap 并创建标记,capybara 应该能够找到你的标记

then check for their existence in the page..then do your normal ajax check in capybara. the marker will be contained in a div right? then if you call launchmap and create the markers, capybara SHOULD be able find your markers

更新

发现了这个插件:http://vigetlabs.github.com/jmapping/examples/

它为您的谷歌地图提供语义标记(用于优雅降级),允许您使用水豚实际检查标记是否存在.希望它有所帮助(没有时间测试它,但它看起来很有希望)

it gives you semantic markup for your google maps(for graceful degradation) allowing you to actually check if a marker exists using capybara. hope it helps(dont have time to test it but it looks promising)

这篇关于Rails3 Google Maps 使用 selenium 进行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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