如何向Selenium Remote Control添加JQuery定位器 [英] How do I add a JQuery locators to Selenium Remote Control

查看:188
本文介绍了如何向Selenium Remote Control添加JQuery定位器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用XPath与Selenium相当愉快,甚至使用getEval,但是一个Javascript,但一个同事说,能够使用Selenium中的JQuery选择器不是太好。

I've been using XPath with Selenium quite happily and even using getEval with a but of Javascript, but a colleague said wouldn't it be great to be able to use JQuery selectors in Selenium.

我已经google了,但找不到任何似乎为我工作的文章。任何人都可以提供有关如何使用JQuery语法从selenium中提取doc元素及其相应的值的综合指南。

I've googled it, but can't find any articles that seem to work for me. Could anyone provide a comprehensive guide on how to use JQuery syntax to extract doc elements and their respective values out of selenium.

我使用C#编写我的硒测试,

I'm using C# to write my selenium tests, so if any examples could be from a C# perspective that'd be great.

感谢

推荐答案

Karl Swedberg撰写了一篇关于它的优秀博客文章,可以在 http://www.learningjquery.com/2009/04/better-stronger-safer-jquerify-bookmarklet

Karl Swedberg wrote an excellent blog entry about it which can be found at http://www.learningjquery.com/2009/04/better-stronger-safer-jquerify-bookmarklet

我们调整这基本上在Selenium服务器jar文件中我们修改了RemoteRunner.html以包括jquery JavaScript(从 http://code.jquery.com/jquery-latest.min.js ):

We adapted this and basically in the Selenium Server jar file we modified RemoteRunner.html to include the jquery JavaScript (obtained from http://code.jquery.com/jquery-latest.min.js):

        <script language="JavaScript" type="text/javascript" src="jquery.min.js"></script>
        <script language="JavaScript" type="text/javascript">
            function openDomViewer() {
                var autFrame = document.getElementById('selenium_myiframe');
                var autFrameDocument = new SeleniumFrame(autFrame).getDocument();
                this.rootDocument = autFrameDocument;
                var domViewer = window.open(getDocumentBase(document) + 'domviewer/domviewer.html');
                return false;
            }
        </script>

然后,为了在Selenium中使用它,我们添加了位置策略:

Then to enable this for use in Selenium we add the location strategy:

mySelenium.addLocationStrategy("jquery",
            "var loc = locator; " +
            "var attr = null; " +
            "var isattr = false; " +
            "var inx = locator.lastIndexOf('@'); " +

            "if (inx != -1){ " +
            "   loc = locator.substring(0, inx); " +
            "   attr = locator.substring(inx + 1); " +
            "   isattr = true; " +
            "} " +

            "var found = jQuery(inDocument).find(loc); " +
            "if (found.length >= 1) { " +
            "   if (isattr) { " +
            "       return found[0].getAttribute(attr); " +
            "   } else { " +
            "       return found[0]; " +
            "   } " +
            "} else { " +
            "   return null; " +
            "}"
        );

注意上面添加的locator策略是在Java中,但它只是一个字符串,所以应该很容易复制C#。
JQuery确实使事情更快,特别是在Internet Explorer中!

Note the above addition of locator strategy is in Java but its just a string so should be easily replicated in C#. JQuery does make things a lot faster, especially in Internet Explorer!

要修改jar,可以使用java命令行工具更新下载的selenium服务器罐。创建一个与名为core的jar相同的文件夹,并将修改后的RemoteRunner.html和jquery.min.js文件放在那里。然后运行如下:

To modify the jar you can use the java command line tool to update the downloaded selenium server jar. Make a folder at the same level as the jar called "core" and put the modified RemoteRunner.html and jquery.min.js files there. Then run something like:

jar -uf selenium-server-standalone-2.0b3-APT.jar core\RemoteRunner.html
jar -uf selenium-server-standalone-2.0b3-APT.jar core\jquery.min.js

如果jar不在您的路径中,您可以使用完整路径,例如在Windows上,您可以执行它:

If jar isn't in your path you can use the full path, for example on windows you could execute it with something like:

"C:\Program Files\Java\jdk1.6.0_22\bin\jar.exe" <arguments>

这篇关于如何向Selenium Remote Control添加JQuery定位器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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