如何将 JQuery 定位器添加到 Selenium 远程控制 [英] How do I add a JQuery locators to Selenium Remote Control

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

问题描述

我一直很高兴地使用 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.

我已经用谷歌搜索了它,但找不到任何似乎对我有用的文章.谁能提供有关如何使用 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# 编写我的 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 Server 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; " +
            "}"
        );

请注意,上面添加的定位器策略是在 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 服务器 jar.创建一个与 jar 相同级别的文件夹,名为core",并将修改后的 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 coreRemoteRunner.html
jar -uf selenium-server-standalone-2.0b3-APT.jar corejquery.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 FilesJavajdk1.6.0_22injar.exe" <arguments>

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

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