visit
作用:
訪問一個遠程URL。
(建議:使用前設置 baseUrl)
語法:
cy.visit(url)
cy.visit(url, options)
cy.visit(options)
使用:
cy.visit('http://localhost:3000') // 產生遠程頁面的窗口
參數:
> url (String)
要訪問的URL。(如果你設置了
baseUrl
,將使用baseUrl
配置的url作為前綴)> options (Object)
傳入一個options對象來控制此方法。
Options:
選項 |
默認 |
描述 |
|
|
要訪問的URL,與 |
|
|
在訪問中使用的HTTP方法,可以是 |
|
|
與 如果它是一個字符串,將原封不動地去傳遞。 如果它是一個對象,將被編碼為字符串並與 |
|
|
將HTTP標頭名稱映射到要與請求一起發送的值的對象。 注意: |
|
|
在命令日志中顯示該命令 |
|
|
添加基本授權標頭 |
|
|
當響應碼不是 |
|
|
在頁面加載了所有資源之前調用 |
|
|
在你的頁面觸發其加載事件后調用 |
|
|
當狀態代碼錯誤時是否自動重試 |
|
|
當網絡錯誤時是否自動重試 |
|
|
頁面加載超時時間,單位毫秒。 |
例子:
//設置超時 cy.visit('/index.html', { timeout: 30000 }) //添加身份驗證(1) cy.visit('https://www.acme.com/', { auth: { username: 'wile', password: 'coyote' } }) //添加身份驗證(2) cy.visit('https://wile:coyote@www.acme.com') //頁面加載完所有資源后調用函數 cy.visit('http://localhost:3000/#dashboard', { onBeforeLoad: (contentWindow) => { // contentWindow是遠程頁面的窗口對象 } }) //觸發加載事件后調用函數 cy.visit('http://localhost:3000/#/users', { onLoad: (contentWindow) => { // contentWindow是遠程頁面的窗口對象 if (contentWindow.angular) { // 一些事件 } } }) //發送post請求 cy.visit({ url: 'http://localhost:3000/cgi-bin/newsletterSignup', method: 'POST', body: { name: 'George P. Burdell', email: 'burdell@microsoft.com' } })