如何在 WebdriverIO 中使用 id 定位符?
我們可以在 WebdriverIO 中使用 id 定位符。一旦我們導航到一個網頁,我們便必須與頁面上可用的網路元素進行互動,例如單擊連結/按鈕、在編輯框中輸入文字等等,才能完成我們的自動化測試用例。
為此,我們的第一項工作是識別元素。我們能夠使用 id 屬性為某個元素識別它。它是一個非常有用的定位符並且與所有定位符相比,它加快了自動化測試的執行速度。
在 WebdriverIO 程式碼中,我們能夠選擇按照以下格式指定元素的 id 屬性的值 −
$('=#value of id attribute') Or, we can store this expression in a variable: const p = $('=#value of id attribute')
讓我們識別下圖中高亮的元素並單擊它 −
上圖中高亮的連結的標籤名為 - a 並且 id 屬性值為 - redirect
示例
程式碼實現
// test suite name describe('Tutorialspoint application', function(){ //test case it('Identify element with Id', function(){ // launch url browser.url('https://the-internet.herokuapp.com/redirector') //identify element with id then click $("#redirect").click() //obtain page title console.log('Page title after click: ' + browser.getTitle()) }); });
輸出
當命令成功執行結束後,頁面的標題(“The Internet”)將被列印到控制檯。
廣告