有没有办法检测我在一个Selenium Webdriver页面从Javascript [英] Is there a way to detect that I'm in a Selenium Webdriver page from Javascript

查看:604
本文介绍了有没有办法检测我在一个Selenium Webdriver页面从Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的测试中禁止TinyMCE的初始化,如果Javascript可以检测到我在Selenium自动化页面中运行,可以很容易地做到这一点。

I'd like to suppress the initialization of TinyMCE inside my tests and can do this easily if the Javascript can detect that I'm running inside a Selenium-automated page.

那么,有没有一些JS代码,我可以用来检测Selenium驱动程序?或者,如何扩展userAgent字符串以包含我可以从JS检测到的模式?

So, is there some JS code that I can use to detect the Selenium driver? Alternatively, how can I extend the userAgent string to include a pattern that I can detect from JS?

如果真的很重要,我通过黄瓜和capybara运行Mac OS X。

If it really matters, I'm running this through cucumber and capybara on Mac OS X.

推荐答案

据我所知,没有Selenium提供的跨浏览器方法来检测它是驱动浏览器。在FF中,webdriver在 html 元素上设置 webdriver 属性,但显然不在其他浏览器中。也许有一天,这将是的方式来检测浏览器是由Selenium驱动,但现在不是。我刚刚用FF和Chrome测试它:属性存在于FF但不是Chrome。这是...

As far as I know there is no cross-browser method that Selenium provides to detect that it is driving the browser. In FF, webdriver sets the webdriver attribute on the html element but apparently not in other browsers. Maybe some day this will be the way to detect that the browser is being driven by Selenium but not for now. I've just tested it with FF and Chrome: the attribute was present in FF but not Chrome. So that's that...

需要做一些像你想实现的东西。我运行大型测试套件与硒。这些套件在Linux,Windows和OS X上运行多个版本的Chrome,Firefox和IE,一些测试在Sauce Labs上远程运行。

Sometimes I need to do something like what you are trying to achieve. I run large test suites with Selenium. These suites run on multiple versions Chrome, Firefox and IE, on Linux, Windows and OS X, with some of the tests being run remotely on Sauce Labs.

方法I已使用 executeScript 。 (我链接到Java文档,但此方法存在于所有平台,Selenium可用。)我使用它来运行代码在浏览器端之前运行测试。我使用这种方法的两种方式:

The methods I've used rely on executeScript. (I'm linking to the Java doc but this method exists for all platform that Selenium is available for.) I use it to run code on the browser side before running a test. The two ways I've used this method:


  1. / code>我的浏览器代码检查。所以我可以设置 window.running_test_suite_for_foobar = true 然后有代码检查。

我使用的另一种方法是设计我的代码,以便它具有可以调用的配置选项或未记录的方法,以便为测试环境正确设置或完全禁用它。例如,我有一个 onbeforeunload 模块,阻止用户离开具有未保存的修改的页面。在测试中,使这个通常打开是没有用的。 Selenium 可以处理弹出窗口,但是当您远程运行测试时,每一个交互都有显着的成本。然后多次通过几十个测试,然后你有一个测试套件,可以很容易地再花几分钟运行。

Another method I've used is to design my code so that it has configuration options or undocumented methods that can be called to set it up properly for a test environment or to disable it completely. For instance I have an onbeforeunload module that prevents users from moving away from a page with unsaved modifications. In testing, it is not useful to have this generally turned on. Selenium could handle the popup but when you run tests remotely every bit of interaction has a significant cost. Then multiple by dozens of tests and then you have a test suite that can easily take a few more minutes to run. So I have a method that I call to turn it off.



更改用户代理的问题



The Problems with Changing the User Agent


  1. 不同浏览器的方法不同。您的代码必须检查您要运行的浏览器,然后根据浏览器执行正确的操作。

  1. The methods to do it differ from browser to browser. Your code has to check which browser you want to run and then perform the right action depending on the browser.

FF和Chrome在其他答案中显示的方法这里完全替换用户代理字符串(与之前的说法相反)。要添加,您必须知道未修改的字符串。这从更改浏览器到浏览器和版本到版本。我想你可以有一个股票用户代理字符串表来修改。这不是我想要维护的东西。或者您可以启动浏览器两次:一次查询股票用户代理,一次使用修改的用户代理运行测试。

The methods shown for FF and Chrome in other answers here completely replace the user agent string (contrarily to what some have said). To append to it, you'd have to know what the unmodified string would be. This changes from browser to browser and version to version. I guess you could have a table of stock user agent strings to modify. This is not something I'd want to have to maintain. Or you could start the browser twice: once to query the stock user agent and once to run the test with the modified user agent.

而且你不能懒惰使用正确的用户代理字符串。尽管浏览器代码应该能够进行 功能检测而不是浏览器检测,但仍然有一些情况代码必须处理一个特殊情况是通过知道其运行的浏览器的版本。当问题是浏览器中的错误时,没有功能进行检查。检查错误是否发生可能成本太高或不可能可靠。所以代码必须检查用户代理字符串。您的代码可能不必这样做,但第三方代码可能。 (例如,我遇到了一个问题,发生在 getBoundingClientRect ,其中坐标一般不正确在IE,但只在一个版本的Chrome。检查运行时的错误,我不能确定字体或显示设置的更改不会产生假阴性。)

And you can't be lazy about using the right user agent string. While it is true that browser code should do feature detection rather than browser detection, there remain some cases where the only reasonable way to know that the code has to handle a special case is by knowing which version of the browser it is running in. When the problem is a bug in the browser, there is no feature to check for. Checking that the bug is happening may be too costly or impossible to do reliably. So the code has to check the user agent string. Your code may not have to do this but third party code may. (For instance, I've run into an issue that happens with getBoundingClientRect where the coordinates would be incorrect generally in IE but only in one version of Chrome. It is too costly to check for the bug at run-time and I can't be sure that a change of fonts or display settings would not yield false negatives.)

这篇关于有没有办法检测我在一个Selenium Webdriver页面从Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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