量角器测试铬不会在野生动物园时点击项目

时间:2015-07-24 16:53:59

标签: javascript angularjs protractor

我的测试在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 /”的网址。这在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'))

2 个答案:

答案 0 :(得分: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/');
    });
});

答案 1 :(得分:1)

第二次找不到您的链接的原因是您使用sources重新加载了该页面。重新加载后,<script src="javascript.js"> 句柄丢失,webdriver不知道要操作的元素。

您需要在页面重新加载后再次声明<div id="circle"></div> 变量,或者避免重新加载页面。