量角器测试非常慢,经常超时

时间:2017-03-20 02:37:51

标签: javascript html angularjs protractor

我在下午的大部分时间里一直在与这个测试作斗争,我已经评论了我试图做的但是它不会起作用。我有能力搜索公司内的用户,然后生成一个表,我想选择一个与用户名匹配的表。目前我只使用td.text-center,因为它是表中唯一的td,但是如果其他具有相同名称的用户在系统中,这似乎很差并且容易破裂。

尝试2似乎是最好的解决方案,但它始终会给元素找不到错误。有没有人找到一种可靠的方法在桌面上定位TD?下面的方法适用于其他表格,而不是这个......

  this.editUser = function(name) {

        // Original Code Block
        //browser.actions().mouseMove(element(by.css('td.text-center'))).perform();
        //var editUserBtn = element(by.css('.user-action-open'));
        //editUserBtn.click().then(function() {
        //    var editDetails = element(by.cssContainingText('li > a', 'Edit Details'));
        //    browser.wait(EC.visibilityOf(editDetails), 3000);
        //    editDetails.click();
        //});


        // Attempt 2

        // browser.actions().mouseMove(element(by.css('td.text-center'))).perform();
        //browser.sleep(3000);
        //var edit = element.all(by.repeater('user in users')).filter(function(rowElement){
        //    return rowElement.element.all(by.css('td[ng-bind="user.Firstname"]')).getText().then(function(text){
        //        return text.trim() == name;
        //    });
        //}).first();
        //browser.actions().mouseMove(edit).perform();


        // Currently just times out and doesn't find the element.

        var editUserButton = $('.user-action-open');
        browser.wait(EC.visibilityOf(editUserButton), 20000).then(function() {
            editUserButton.click().then(function() {
                var editDetails = element(by.cssContainingText('li > a', 'Edit Details'));
                browser.wait(EC.visibilityOf(editDetails), 3000);
                editDetails.click();
            });
        });
    }

1 个答案:

答案 0 :(得分:1)

您可以使用ExpectedConditions做一些等待而不是任意休眠3秒。你可以改为浏览器等待。如果对象处于所需状态,这将等待超时。

    let users = element.all(by.repeater('user in users');

    // the original post shows that this is an element.all
    // this would only be true if the user can have multiple first names
    let firstName = element(by.css('td[ng-bind="user.Firstname"]'));

    browser.actions().mouseMove(element(by.css('td.text-center'))).perform();

    // wait to see if the repeater is present or timeout in 3 seconds
    browser.wait(users).isPresent(), 3000);

    let edit = element.all(users).filter((rowElement) => {
      // wait for first name, get the first name of the row's element
      // if the text matches the name, resolve the promise true.
      return browser.wait(rowElement.firstName, 3000).then(() => {
        return rowElement.firstName.getText().then((text) => {
          return text.trim() == name;
        });
      });
    }).first();

    browser.actions().mouseMove(edit).perform();

rowElement.firstName可能不是正确的语法。

链接承诺

  // wait to see if the repeater is present or timeout in 3 seconds
  browser.wait(users).isPresent(), 3000).then(() => {
    let edit = element.all(users).filter((rowElement) => {
      // wait for first name, get the first name of the row's element
      // if the text matches the name, resolve the promise true.
      return browser.wait(rowElement.firstName, 3000).then(() => {
        return rowElement.firstName.getText().then((text) => {
          return text.trim() == name;
        });
      });
    }).first();

    browser.actions().mouseMove(edit).perform();
  }).catch(err => {
    // we could also have asserted the length was at least 1
    // maybe we want to catch on something else? maybe this isn't a
    // fail case so maybe do not use `fail(`
    fail("did not find users"); 
  });