url
url
命令會在瀏覽器中載入 URL。 如果在設定中指定了 baseUrl,它會使用 node 的 url.resolve() 方法將其添加到 url 參數的前面。 使用與上次相同的 URL 呼叫 browser.url('...')
將觸發頁面重新載入。
此命令會傳回一個 WebdriverIO.Request
物件,其中包含頁面載入的請求和回應資料的相關資訊。
此命令支援下列選項
wait
請求資源在完成命令之前應處於的所需狀態。它支援下列狀態
none
:在發出頁面請求並收到回應後不等待interactive
:等到頁面可互動complete
:等到頁面的 DOM 樹完全載入networkIdle
:等到沒有擱置的網路請求
headers
要與請求一起傳送的標頭。
預設: {}
auth
基本驗證憑證。 注意:如果 headers
選項中提供,這將覆寫現有的 Authorization
標頭。
timeout
如果設定為數字,則命令將等待指定的毫秒數,直到頁面載入所有回應後再傳回。
注意:若要使其產生影響,需要將 wait
選項設定為 networkIdle
。
預設: 5000
用法
browser.url(url, options)
參數
名稱 | 類型 | 詳細資訊 |
---|---|---|
url optional | 字串 | 要導覽至的 URL |
options optional | UrlOptions | 導覽選項 |
範例
url.js
// navigate to a new URL
const request = await browser.url('https://webdriverio.dev.org.tw');
// log url
console.log(request.url); // outputs: "https://webdriverio.dev.org.tw"
baseUrlResolutions.js
// With a base URL of http://example.com/site, the following url parameters resolve as such:
// When providing a scheme:
// https://webdriverio.dev.org.tw
await browser.url('https://webdriverio.dev.org.tw');
// When not starting with a slash, the URL resolves relative to the baseUrl
// http://example.com/site/relative
await browser.url('relative');
// When starting with a slash, the URL resolves relative to the root path of the baseUrl
// http://example.com/rootRelative
await browser.url('/rootRelative');
basicAuth.js
// navigate to a URL with basic authentication
await browser.url('https://the-internet.herokuapp.com/basic_auth', {
auth: {
user
pass
}
});
await expect($('p=Congratulations! You must have the proper credentials.').toBeDisplayed();
onBeforeLoad.js
// navigate to a URL and mock the battery API
await browser.url('https://pazguille.github.io/demo-battery-api/', {
onBeforeLoad (win) {
// mock "navigator.battery" property
// returning mock charge object
win.navigator.getBattery = () => Promise.resolve({
level: 0.5,
charging: false,
chargingTime: Infinity,
dischargingTime: 3600, // seconds
})
}
})
// now we can assert actual text - we are charged at 50%
await expect($('.battery-percentage')).toHaveText('50%')
// and has enough juice for 1 hour
await expect($('.battery-remaining')).toHaveText('01:00)