
- WebdriverIO 教程
- WebdriverIO - 首頁
- WebdriverIO - 簡介
- WebdriverIO - 先決條件
- WebdriverIO - 架構
- WebdriverIO - 使用 NodeJS 入門
- WebdriverIO - NPM 安裝
- WebdriverIO - VS Code 安裝
- WebdriverIO - package.json
- WebdriverIO - Mocha 安裝
- Selenium 獨立伺服器安裝
- WebdriverIO - 配置檔案生成
- WebdriverIO - VS Code 智慧提示
- WebdriverIO - wdio.conf.js 檔案
- WebdriverIO - XPath 定位器
- WebdriverIO - CSS 定位器
- WebdriverIO - 連結文字定位器
- WebdriverIO - ID 定位器
- WebdriverIO - 標籤名定位器
- WebdriverIO - 類名定位器
- WebdriverIO - 名稱定位器
- 斷言的 Expect 語句
- WebdriverIO - 成功路徑流程
- WebdriverIO - 通用瀏覽器命令
- WebdriverIO - 處理瀏覽器大小
- WebdriverIO - 瀏覽器導航命令
- 處理複選框和下拉列表
- WebdriverIO - 滑鼠操作
- 處理子視窗/彈出視窗
- WebdriverIO - 隱藏元素
- WebdriverIO - 框架
- WebdriverIO - 拖放
- WebdriverIO - 雙擊
- WebdriverIO - Cookie
- WebdriverIO - 處理單選按鈕
- Web元素上的 Chai 斷言
- WebdriverIO - 多個視窗/標籤頁
- WebdriverIO - 滾動操作
- WebdriverIO - 警報
- WebdriverIO - 除錯程式碼
- WebdriverIO - 捕獲螢幕截圖
- WebdriverIO - JavaScript 執行器
- WebdriverIO - 等待
- WebdriverIO - 並行執行測試
- WebdriverIO - 資料驅動測試
- 從命令列引數執行測試
- 使用 Mocha 選項執行測試
- 從 Allure 生成 HTML 報告
- WebdriverIO 有用資源
- WebdriverIO - 快速指南
- WebdriverIO - 有用資源
- WebdriverIO - 討論
WebdriverIO - Cookie
我們可以使用 WebdriverIO 處理 Cookie。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'])
或者,
//to get all cookies browser.getCookies()
browser.deleteCookies
這用於從現有頁面刪除 Cookie。如果將 Cookie 名稱作為引數提供給此方法,則將刪除該特定 Cookie。否則,將刪除當前頁面上的所有 Cookie。
語法
語法如下:
//to delete a specific cookie browser.deleteCookies(['Topic'])
或者,
//to delete all cookies browser.deleteCookies()
首先,請按照“使用 WebdriverIO 的成功路徑流程”一章中步驟 1 到 5 的操作,如下所示:
步驟 1 - 安裝 NodeJS。有關如何執行此安裝的詳細資訊,請參閱“使用 NodeJS 入門”一章。
步驟 2 - 安裝 NPM。有關如何執行此安裝的詳細資訊,請參閱“NPM 安裝”一章。
步驟 3 - 安裝 VS Code。有關如何執行此安裝的詳細資訊,請參閱“VS Code 安裝”一章。
步驟 4 - 建立配置檔案。有關如何執行此安裝的詳細資訊,請參閱“配置檔案生成”一章。
步驟 5 - 建立一個規範檔案。有關如何執行此安裝的詳細資訊,請參閱“Mocha 安裝”一章。
步驟 6 - 將以下程式碼新增到建立的 Mocha 規範檔案中。
// 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) }); });
執行配置檔案 - wdio.conf.js 檔案,使用以下命令:
npx wdio run wdio.conf.js
有關如何建立配置檔案的詳細資訊,請參閱“wdio.conf.js 檔案”和“配置檔案生成”一章。
您的計算機上將顯示以下螢幕:

成功執行命令後,首先將名稱為 topic1 的 Cookie 詳細資訊列印到控制檯。然後,將名稱為 topic1 和 topic2 的兩個 Cookie 詳細資訊都顯示出來。
您的計算機上將顯示以下螢幕:

然後,我們刪除了名稱為 topic2 的 Cookie,因此其他 Cookie 列印到控制檯。最後,在刪除所有 Cookie 後,控制檯列印一個空陣列。