有没有办法从 JavaScript 检测到我在 Selenium Webdriver 页面中

我想在我的测试中抑制 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.

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

So, is there some JavaScript 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 JavaScript?

如果真的很重要,我会通过 Cucumber 和 Cucumbera href="https://en.wikipedia.org/wiki/Capybara_(software)" rel="nofollow noreferrer">Capybara 在 Mac OS X 上.

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

推荐答案

据我所知,Selenium 没有提供跨浏览器方法来检测它是否正在驱动浏览器.在 Firefox 中,webdriver 在 html 元素上设置 webdriver 属性,但在其他浏览器中显然没有.也许有一天这将成为检测浏览器是否由 Selenium 驱动的方法,但目前还不是.我刚刚使用 Firefox 和 Chrome 对其进行了测试:该属性存在于 Firefox 中,但不存在于 Chrome 中.原来如此……

As far as I know there is no cross-browser method that Selenium provides to detect that it is driving the browser. In Firefox, 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 Firefox and Chrome: the attribute was present in Firefox, but not Chrome. So that's that...

有时我需要做一些你想要实现的事情.我使用 Selenium 运行大型测试套件.这些套件可在 Linux、Windows 和 OS X 上的多个版本的 Chrome、Firefox 和 Internet Explorer 上运行,其中一些测试在 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 Internet Explorer, on Linux, Windows and OS X, with some of the tests being run remotely on Sauce Labs.

我使用的方法依赖于 executeScript.(我链接到 Java 文档,但这种方法适用于 Selenium 可用的所有平台.)我用它在浏览器端运行代码在运行测试之前.我用过这个方法的两种方式:

The methods I've used rely on executeScript. (I'm linking to the Java documentation, but this method exists for all platforms 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. 在我的浏览器代码检查的 window 上设置一个变量浏览器端.所以我可以例如设置 window.running_test_suite_for_foobar = true 然后让代码检查它.存在冲突的风险,但如果小心使用变量名,风险很小.

  1. Set a variable browser-side on window that my browser code checks. So I could for instance set window.running_test_suite_for_foobar = true and then have code check that. There's a risk of a clash but if the variable name is used carefully the risk is minimal.

我使用的另一种方法是设计我的代码,使其具有可以调用的配置选项或未记录的方法,以便为测试环境正确设置或完全禁用它.例如,我有一个 onbeforeunload 模块,它可以防止用户离开未保存修改的页面.在测试中,一般都打开这个是没有用的.

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 可以处理弹出窗口,但是当您远程运行测试时,每一点交互都会产生巨大的成本.然后多次测试几十个,然后你就有了一个测试套件,可以轻松地多花几分钟来运行.所以我有一个方法可以调用来关闭它.

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.

  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.

此处其他答案中为 Firefox 和 Chrome 显示的方法完全替换了用户代理字符串(与某些人所说的相反).要追加到它,你必须知道未修改的字符串是什么.这会因浏览器和版本而异.

The methods shown for Firefox 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.

而且你不能懒惰地使用正确的用户代理字符串.虽然浏览器代码确实应该进行特征检测而不是浏览器检测,但仍然存在某些情况,其中唯一合理的方法是知道代码必须处理一个特殊情况是通过知道它运行在哪个浏览器的版本.

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.

当问题是浏览器中的错误时,没有功能需要检查.检查错误是否发生可能成本太高或无法可靠地完成.所以代码必须检查用户代理字符串.您的代码可能不必这样做,但第三方代码可以.(例如,我遇到了 getBoundingClientRect 发生的问题,其中坐标在 Internet Explorer 中通常不正确,但仅在 Chrome 的一个版本中.检查错误的成本太高在运行时,我不能确定更改字体或显示设置不会产生误报.)

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 Internet Explorer, 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.)

相关文章