如何在 WebdriverIO 中管理 Cookie?
我們可以在 WebdriverIO 中管理 Cookie。Cookie 有助於識別使用者。這是一種有效的技術,可以將資訊從一個網站會話傳遞到另一個會話,或在兩個連線的網站的會話之間傳遞。
我們可以使用以下方法在 WebdriverIO 中新增、刪除和獲取 Cookie:
browser.setCookies - 用於為當前頁面設定單個 Cookie 或多個 Cookie。要為頁面設定 Cookie,我們必須先啟動並處於該頁面。
語法
browser.setCookies({cookie, cookie.name, cookie.value, cookie.path,
cookie.domain, cookie.secure, cookie.httpOnly, cookie.expiry} )這裡,cookie 是 Cookie 物件或物件陣列,可以包含以下值:
cookie.name - 可選引數,指 Cookie 名稱。
cookie.value - 可選引數,指 Cookie 值。
cookie.path - 可選引數,指 Cookie 路徑。預設值為 /(如果新增 Cookie 時未新增)。
cookie.domain - 可選引數,指 Cookie 域名。預設值為當前瀏覽上下文活動文件的 URL 域名(如果新增 Cookie 時未新增)。
cookie.secure - 可選引數,用於檢查 Cookie 是否安全。預設值為 false(如果新增 Cookie 時未新增)。
cookie.httpOnly - 可選引數,用於檢查 Cookie 是否為 HTTP 型別。預設值為 false(如果新增 Cookie 時未新增)。
cookie.expiry。
browser.getCookies - 用於從現有頁面獲取 Cookie。如果將 Cookie 名稱作為引數提供給此方法,則將獲取該特定 Cookie。否則,將獲取當前頁面上的所有 Cookie。
語法
//to get a specific cookie browser.getCookies(['Topic']) Or, //to get all cookies browser.getCookies()
browser.deleteCookies - 用於從現有頁面刪除 Cookie。如果將 Cookie 名稱作為引數提供給此方法,則將刪除該特定 Cookie。否則,將刪除當前頁面上的所有 Cookie。
語法
//to delete a specific cookie browser.deleteCookies(['Topic']) Or, //to delete all cookies browser.deleteCookies()
示例
程式碼實現
// test suite name
describe('Tutorialspoint application', function(){
//test case
it('Cookies', function(){
// launch url
browser.url('https://tutorialspoint.tw/index.htm')
//set cookies
browser.setCookies([
{name: 'topic1', value: 'WebdriverIO'},
{name: 'topic2', value: 'Selenium'}
])
//get a particular cookie
const t = browser.getCookies(['topic1'])
console.log(t);
//get all cookies
const a = browser.getCookies()
console.log(a);
//delete a cookie with name topic2
browser.deleteCookies(['topic2'])
d = browser.getCookies()
console.log(d)
//delete all cookies
browser.deleteCookies()
m = browser.getCookies()
console.log(m)
});
});
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP