【问题标题】:Protractor test chrome doesn't click on item while safari does量角器测试 chrome 在 safari 时不会单击项目
【发布时间】:2015-10-15 10:37:06
【问题描述】:

我的测试在 safari 中顺利通过,而在 chrome 中这个特殊位似乎不起作用:

 it('should click the first source and get to the source preview page', function () {
    var grid_icon = element(by.css('.fa-th'));
    var sources = element.all(by.repeater('source in shownSources'));

    sources.get(0).element(by.tagName('a')).click();
    browser.pause();
    // Check url
    expect(browser.getCurrentUrl()).toContain('/source/');
});

点击超链接后,它应该会变成一个包含“/source/”的url。这在 Safari 中运行良好,但在 Chrome 中却失败了

我的量角器配置文件:

exports.config = {
framework: 'jasmine2',
seleniumServerJar: '../node_modules/protractor/selenium/selenium-server-standalone-2.45.0.jar',
seleniumPort: 4444,
troubleshoot: false,
basePath: '../',
specs: ['protractor/***/**/*.js'],
baseUrl: 'http://localhost:9000',
capabilities: {
    browserName: 'chrome'
},
onPrepare: function() {
    browser.manage().window().maximize();
}
};

编辑:我最初的问题似乎不再发生。但是测试仍然表现得很奇怪。这一点在 Safari 中工作得非常好:

it('should add all sources to the list and show the cart icon on the source in inventory', function () {
    browser.get('/sources');
    var ordersources = element.all(by.repeater('orderSource in orderSources'));
    var sources = element.all(by.repeater('source in shownSources'));

    sources.get(0).element(by.css('a')).click();
    var add_to_cart_btn = element(by.binding('addBtnText'));
    add_to_cart_btn.click();
    browser.get('/sources');

    sources.get(1).element(by.css('a')).click();
    var add_to_cart_btn = element(by.binding('addBtnText'));
    add_to_cart_btn.click();
    browser.get('/sources');

    browser.pause();

    sources.get(2).element(by.css('a')).click();
    var add_to_cart_btn = element(by.binding('addBtnText'));
    add_to_cart_btn.click();
    browser.get('/sources');

    expect(ordersources.count()).toBe(3);

    sources.each(function (field) {
        var isInCart_symbol = field.element(by.css('.fa-cart-arrow-down'));
        expect(isInCart_symbol.getAttribute('aria-hidden')).toBe('false');
    });
});

但是在 Chrome 中,第二次没有找到“a”项,并且 browser.sleep() 永远不会执行,下一个“it”开始运行。

编辑:我通过 class 属性使用另一个 html 元素让它工作。

element.(by.css('.example'))

【问题讨论】:

    标签: javascript angularjs protractor


    【解决方案1】:

    我假设当你说它失败时,期望失败了?您可以尝试以下 3 种方法。

    // Wait Till Url Contains
    function WaitTillUrlContains(text, time, errMessage){
      if(typeof(time) ==='undefined') time = 5000;
      browser.getCurrentUrl().then(function (currentUrl) {
        browser.wait(function () {
           return browser.getCurrentUrl().then(function (newUrl) {
              var test = newUrl;
              if( test.indexOf(text) >= 0){
                // Found word
                return true;
              }
    
           });
        }, time , errMessage);
      });
    };
    

    (1) 在期望之前添加等待。

    it('should click the first source and get to the source preview page', function () {
        var grid_icon = element(by.css('.fa-th'));
        var sources = element.all(by.repeater('source in shownSources'));
    
        sources.get(0).element(by.tagName('a')).click();
    
        // Check url
        WaitTillUrlContains("/source/", 5000, "✗ Failed to wait for page to load");
        expect(browser.getCurrentUrl()).toContain('/source/');
    });
    

    (2) 点击后执行 .then() 函数

    it('should click the first source and get to the source preview page', function () {
        var grid_icon = element(by.css('.fa-th'));
        var sources = element.all(by.repeater('source in shownSources'));
    
        sources.get(0).element(by.tagName('a')).click().then(function(){
    
            // Check url
            WaitTillUrlContains("/source/", 5000, "✗ Failed to wait for page to load");
            expect(browser.getCurrentUrl()).toContain('/source/');
        });
    });
    

    (3) 获取元素后执行 .then() 函数然后点击

    it('should click the first source and get to the source preview page', function () {
        var grid_icon = element(by.css('.fa-th'));
        var sources = element.all(by.repeater('source in shownSources'));
    
        sources.get(0).element(by.tagName('a')).then(function(elem){
    
            elem.click();
            // Check url
            WaitTillUrlContains("/source/", 5000, "✗ Failed to wait for page to load");
            expect(browser.getCurrentUrl()).toContain('/source/');
        });
    });
    

    【讨论】:

    • 对不起,我没有足够的 sepcify。我在我的问题中提到的问题不再发生。但是测试仍然表现得很奇怪。
    • 抱歉,我没有具体说明我的问题。请看我的编辑
    【解决方案2】:

    第二次找不到你的链接是因为你用browser.get()重新加载了页面。重新加载后,sources 句柄丢失,webdriver 不知道要操作哪个元素。

    您要么需要在页面重新加载后再次声明sources 变量,要么避免重新加载页面。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-11
      • 1970-01-01
      • 1970-01-01
      • 2014-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-19
      相关资源
      最近更新 更多