禁用 JavaScript 的 Selenium WebDriver 测试 [英] Selenium WebDriver tests with JavaScript disabled

查看:44
本文介绍了禁用 JavaScript 的 Selenium WebDriver 测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在浏览器中禁用了 javascript(使用 .

仅供参考,使用 selenium 2.44.0 和 firefox 33.1.1.

据我所知(借助提出的几点此处),禁用 javascript 会杀死 javascript webdriver 本身.这是真的吗?如果是,我的选择或解决方法是什么?

<小时>

注意事项:

  • 对于 chrome,过去可以通过 --disable-javascript 命令行参数,但 不是 不再.

  • 这导致了一个解决方法 0 - 将 chrome 降级到支持命令行标志的旧版本 - 这将是一个未经测试的计划 B

  • 设置 javascript.enabled=false firefox 首选项适用于 python selenium 绑定:

    from selenium import webdriverprofile = webdriver.FirefoxProfile()profile.set_preference('javascript.enabled', False)驱动程序 = webdriver.Firefox(firefox_profile=profile)driver.get('https://my_internal_url.com')# 没有错误,我可以断言错误存在

我愿意接受任何建议,并且可以为您提供任何其他信息.

解决方案

这是实际发生的事情.

事实证明,在探索了protractor和selenium js webdriver的源代码后,关键问题不在js webdriver或protractor,而是在我的测试的编写方式.

有一个设置叫/code> 默认为 false:

/*** 如果为 true,Protractor 将不会尝试与之前的页面同步* 执行操作.这可能是有害的,因为量角器不会等待* 直到处理完 $timeouts 和 $http 调用,这可能导致* 测试变得片状.这应该仅在必要时使用,例如* 当页面使用 $timeout 连续轮询 API 时.** @type {布尔}*/this.ignoreSynchronization = false;

而且我没有将它设置为 true 这使得 protractor 尝试与页面同步并执行客户端脚本,evaluate.js 负责.

解决方案是如此简单我无法想象 - 只需将 ignoreSynchronization 设置为 true 即可解决问题:

'use strict';require('茉莉花-期望');描述('禁用Javascript',函数(){beforeEach(函数(){browser.ignoreSynchronization = true;browser.get('index.html');});it('应该显示禁用的 js', function () {var element = browser.findElement(by.tagName('noscript'));expect(element.getText()).toEqual('请启用Javascript并重试.');});});

希望这会在未来对某人有所帮助.

One of our internal applications (written in angularjs) has a special error box appearing if javascript is disabled in the browser (using noscript), similar to the one on stackoverflow:

I'm trying to write an automated test for it, but having difficulties.

We are using protractor, but I'm pretty sure this is not about it. Here is the protractor configuration file:

'use strict';

var helper = require('./helper.js');

exports.config = {
    seleniumAddress: 'http://localhost:4444/wd/hub',
    baseUrl: 'http://localhost:9001',

    capabilities: helper.getFirefoxProfile(),

    framework: 'jasmine',
    allScriptsTimeout: 20000,

    jasmineNodeOpts: {
        showColors: true,
        isVerbose: true,
        includeStackTrace: true
    }
};

where helper.js is:

var q = require('q');
var FirefoxProfile = require('firefox-profile');

exports.getFirefoxProfile = function() {
    var deferred = q.defer();

    var firefoxProfile = new FirefoxProfile();
    firefoxProfile.setPreference("javascript.enabled", false);
    firefoxProfile.encoded(function(encodedProfile) {
        var capabilities = {
            'browserName': 'firefox',
            'firefox_profile' : encodedProfile,
            'specs': [
                '*.spec.js'
            ]
        };
        deferred.resolve(capabilities);
    });

    return deferred.promise;
};

As you see, we are setting javascript.enabled firefox preference to false which has been proven to work if you manually open up about:config in firefox, change it to false - you would see the contents of noscript section.

But, when I run the tests, I am getting the following error:

Exception thrown org.openqa.selenium.WebDriverException: waiting for evaluate.js load failed

Here is the complete traceback.

FYI, selenium 2.44.0 and firefox 33.1.1 are used.

As far as I understand (with the help of several points raised here), disabling javascript is killing the javascript webdriver itself. Is it true? If yes, what are my options or workarounds?


Notes:

  • in case of chrome, in the past it was possible to disable javascript via --disable-javascript command-line argument, but not anymore.

  • this leads to a workaround number 0 - downgrade chrome to an old version which supported the command-line flag - this would be a not-tested plan B

  • setting javascript.enabled=false firefox preference works with python selenium bindings:

    from selenium import webdriver
    
    profile = webdriver.FirefoxProfile()
    profile.set_preference('javascript.enabled', False)
    driver = webdriver.Firefox(firefox_profile=profile)
    
    driver.get('https://my_internal_url.com')
    # no errors and I can assert the error is present
    

I'm open to any suggestions and can provide you with any additional information.

解决方案

Here is what actually happened.

As it turns out, after exploring the source code of protractor and selenium js webdriver, the key problem is not in the js webdriver or protractor, it was in the way my test was written.

There is a setting called ignoreSynchronization which by default is false:

  /**
   * If true, Protractor will not attempt to synchronize with the page before
   * performing actions. This can be harmful because Protractor will not wait
   * until $timeouts and $http calls have been processed, which can cause
   * tests to become flaky. This should be used only when necessary, such as
   * when a page continuously polls an API using $timeout.
   *
   * @type {boolean}
   */
  this.ignoreSynchronization = false;

And I was not setting it to true which made protractor to try synchronizing with the page and execute client side scripts, which evaluate.js is responsible for.

The solution was so simple I could not imagine - just setting ignoreSynchronization to true solved the problem:

'use strict';

require('jasmine-expect');

describe('Disabled Javascript', function () {
    beforeEach(function () {
        browser.ignoreSynchronization = true;
        browser.get('index.html');
    });

    it('should show disabled js', function () {
        var element = browser.findElement(by.tagName('noscript'));
        expect(element.getText()).toEqual('Please enable Javascript and try again.');
    });
});

Hope this would help somebody in the future.

这篇关于禁用 JavaScript 的 Selenium WebDriver 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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