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

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

问题描述

我们的内部应用程序(写在 angularjs 中)有一个特殊的错误框出现,如果在浏览器中禁用javascript(使用。

FYI,硒 2.44.0 和firefox 33.1.1 被使用。



据我了解几点提出这里),禁用javascript正在杀死JavaScript webdriver本身。这是真的吗?如果是的话,我的选择或解决方法是什么?






注意:

在chrome的情况下,过去有可能通过 - disable-javascript 命令行参数,但不是 id = 6672rel =nofollow noreferrer>再也没有问题了。 旧版本支持命令行标志 - 这将是一个未经测试的计划B

  • 设置 javascript.enabled = false firefox首选项可与 python selenium绑定一起使用

      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')
    #没有错误,我可以断言错误是目前




  • <我可以提供任何建议,并可以提供任何额外的信息。

    这是实际发生的事情。

    事实证明,在研究了量角器和selenium js webdriver的源代码,关键的问题不在于js webdriver或量角器,这是我的测试写的。



    有一个设置叫 ignoreSynchronization 默认是 false

      / ** 
    *如果为true,则量角器wi在
    *执行操作之前,不要尝试与页面同步。这可能是有害的,因为量角器不会等待
    *直到$超时和$ http调用已经被处理,这可能导致
    *测试变成片状。这应该只在必要时使用,如
    *当一个页面使用$ timeout持续轮询API时。
    *
    * @type {boolean}
    * /
    this.ignoreSynchronization = false;

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



    解决方案非常简单,我无法想象 - 只需将 ignoreSynchronization 设置为 true 解决了这个问题:

     'use strict'; 

    require('jasmine-expect');

    描述('Disabled Javascript',function(){
    beforeEach(function(){
    browser.ignoreSynchronization = true;
    browser.get('index。 (''应该显示禁用js',函数(){
    var元素= 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.

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

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