2017-03-20 148 views
0

我大部分時間都在做這個測試,我已經評論了我所做的事情,但它不起作用。我有能力搜索公司內的用戶,然後生成一個表,並且我想選擇一個匹配用戶名稱的表。目前我只使用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

你可以使用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"); 
    }); 
+0

,看起來不錯,我想我試圖做的主要事情是去除browser.actions.mouseMove到行盤旋,並用環路濾波器更換隻需找到該行並點擊它,不管出於什麼原因,我似乎都無法做到這一點,所以會嘗試這種方法 – theHussle

+0

如果你只是想點擊一個對象,只要確保它可以點擊。任意超時並不理想。另外,我不確定爲什麼有鼠標移動。我認爲這與在「td.text-center''上懸停後出現的菜單有關。另一個缺少的是點擊行動,如果你打算點擊它。 – cnishina

+0

是的,它不是一個理想的設計,但必須與之合作。基本上它會生成一個表,當td懸停時出現一個編輯該特定行的按鈕,因此顯示的代碼會讓該行顯示,但是我覺得這個設置很容易中斷,因爲它依賴於搜索返回一個td行。 – theHussle