2017-05-29 77 views
0

我正在寫一個量角器測試,並且無法識別我想點擊一個角頁面的按鈕。當我檢查此按鈕上的元素時,我附加了按鈕的屏幕截圖以及Chrome中看到的內容。在這個頁面上有4個按鈕緊挨着彼此,我很難區分它們。當我檢查元素時,它們看起來都一樣。在我的代碼中,我可以使用CSS來查找這個元素,還是必須使用不同的標識符?無法識別一個元素我正在嘗試點擊一個角頁面

在我的測試我要點擊「新記錄」按鈕

Screenshot of browser and inspection panel

+0

'by.buttonText('New Record')'? http://www.protractortest.org/#/api?view=ProtractorBy.prototype.buttonText如果這不起作用,只需添加一個適當的類或ID以便於選擇。 – jonrsharpe

回答

0

至少有2種方式來做到這一點,如果你沒有正確的唯一標識符

1.元素(by.buttonText('New Record'))

這已在評論中提及,另請參閱the docs

2.創建它

的方法你可以爲它創建一個方法與filter,像這樣

/** 
 
* Get a specific tile based on a given search string 
 
* or a element number 
 
* @param {number | string } needle 
 
* @return {ElementFinder} 
 
*/ 
 
function getTile(needle) { 
 
    var tiles = $$('md-grd-tile'); 
 

 
    // Just a quick and dirty check, better to use 
 
    // lodash for it with `_.isNumber(needle)` 
 
    if (!isNaN(parseFloat(needle))) { 
 
    return tiles.get(needle); 
 
    } 
 

 
    return tiles.filter(function(tile) { 
 
    return tile.getText() 
 
     .then(function(text) { 
 
     return text.indexOf(needle) > -1; 
 
     }); 
 
    }).first(); 
 

 
    // If you use arrow functions it will be this 
 
    //return tiles.filter(
 
    // (tile) => tile.getText() 
 
    // .then((text) => text.indexOf(needle) > -1)).first(); 
 
} 
 

 
// You can then use it like this 
 
getTile('New Record').click(); 
 

 
// Or like this 
 
getTile(0).click();

通過過濾器,你可以或提供你想要的瓦片的數量(從0開始)或者提供瓦片按鈕/描述的(部分)文本。

希望它有幫助

+0

謝謝您的建議。不幸的是,元素(by.buttonText('New Record'))無效。在與我的經理討論之後,我們可能會拖延量角器測試幾個月。所以我會回到這裏,然後嘗試你的第二個建議。 –

相關問題