- Watir 教程
- Watir - 首页
- Watir - 概览
- Watir - 简介
- Watir - 环境设置
- Watir - 安装浏览器驱动
- Watir - 使用浏览器
- Watir - Web 元素
- Watir - 定位 Web 元素
- Watir - 处理 Iframe
- Watir - 自动等待
- Watir - 无头测试
- Watir - 移动测试
- Watir - 捕获屏幕截图
- Watir - 页面对象
- Watir - 页面性能
- Watir - Cookie
- Watir - 代理
- Watir - 警报
- Watir - 下载
- Watir - 浏览器窗口
- Watir 实用资源
- Watir - 快速指南
- Watir - 实用资源
- Watir - 讨论
Watir - Cookie
在本教程中,我们了解如何使用 Watir 处理 Cookie。
这里讨论了一个简单的例子,它将提取给定 URL 的 Cookie。
提取 Cookie 的语法
browser.cookies.to_a
示例
require 'watir' b = Watir::Browser.new :chrome b.goto 'https://tutorialspoint.com' puts b.cookies.to_a
输出
{:name=>"_gat_gtag_UA_232293_6", :value=>"1", :path=>"/", :domain=>".tutorialspoint.com", :expires=>2019-05-03 08:33:58 +0000, :secure=>false} {:name=>"_gid", :value=> "GA1.2.282573155.1556872379", :path=>"/", :domain=>".tutorialspoint.com", :expires=>2019-05-04 08:32:57 +0000, :secure=>false} {:name=>"_ga", :value=>"GA1.2.2087825339.1556872379", :path=>"/", :domain=>".tutorialspoint.com", :expires=> 2021-05-02 08:32:57 +0000, :secure=>false}
现在,我们增加 Cookie,如下所示:
增加 Cookie 的语法
browser.cookies.add 'cookiename', 'cookievalue', path: '/', expires: (Time.now + 10000), secure: true
示例
require 'watir' b = Watir::Browser.new :chrome b.goto 'https://tutorialspoint.com' puts b.cookies.to_a b.cookies.add 'cookie1', 'testing_cookie', path: '/', expires: (Time.now + 10000), secure: true puts b.cookies.to_a
添加 Cookie 之前的输出
{:name=>"_gat_gtag_UA_232293_6", :value=>"1", :path=>"/", :domain=>".tutorialspoint.com", :expires=>2019-05-03 08:44:23 +0000, :secure=>false} {:name=>"_gid", :value=>"GA1.2.1541488984.1556873004", :path=>"/", :domain=>".tutorialspoint.com", :expires=>2019-05-04 08:43:24 +0000, :secure=>false} {:name=>"_ga", :value=>"GA1.2.1236163943.1556873004", :path=>"/", :domain=>".tutorialspoint.com", :expires=>2021-05-02 08:43:24 +0000, :secure=>false}
添加 Cookie 之后的输出
{:name=>"_gat_gtag_UA_232293_6", :value=>"1", :path=>"/", :domain=>".tutorialspoint.com", :expires=>2019-05-03 08:44:23 +0000, :secure=>false} {:name=>"_gid", :value=>"GA1.2.1541488984.1556873004", :path=>"/", :domain=>".tutorialspoint.com", :expires=>2019-05-04 08:43:24 +0000, :secure=>false} {:name=>"_ga", :value=>"GA1.2.1236163943.1556873004", :path=>"/", :domain=>".tutorialspoint.com", :expires=>2021-05-02 08:43:24 +0000, :secure=>false} {:name=>"cookie1", :value=>"testing_cookie", :path=>"/", :domain=>"www.tutorialspoint.com", :expires=>2039-04-28 08:43:35 +0000, :secure=>true}
请注意,最后一个是我们使用 watir 添加的 Cookie。
清除 Cookie
语法
browser.cookies.clear
示例
require 'watir' b = Watir::Browser.new :chrome b.goto 'https://tutorialspoint.com' puts b.cookies.to_a b.cookies.clear puts b.cookies.to_a
输出
{:name=>"_gat_gtag_UA_232293_6", :value=>"1", :path=>"/", :domain=>".tutorialspoint.com", :expires=>2019-05-03 08:48:29 +0000, :secure=>false} {:name=>"_gid", :value=>"GA1.2.1264249563.1556873251", :path=>"/", :domain=>".tutorialspoint.com", :expires=>2019-05-04 08:47:30 +0000, :secure=>false} {:name=>"_ga", :value=>"GA1.2.1001488637.1556873251", :path=>"/", :domain=>".tutorialspoint.com", :expires=>2021-05-02 08:47:30 +0000, :secure=>false Empty response ie a blank line will get printed after cookie.clear is called.
删除特定 Cookie
语法
browser.cookies.delete 'nameofthecookie'
示例
require 'watir' b = Watir::Browser.new :chrome b.goto 'https://tutorialspoint.com' puts b.cookies.to_a puts b.cookies.delete "_ga" puts b.cookies.to_a
输出
All cookies: {:name=>"_gat_gtag_UA_232293_6", :value=>"1", :path=>"/", :domain=>".tutorialspoint.com", :expires=>2019-05-03 08:52:38 +0000, :secure=>false} {:name=>"_gid", :value=>"GA1.2.1385195240.1556873499", :path=>"/", :domain=>".tutorialspoint.com", :expires=>2019-05-04 08:51:37 +0000, :secure=>false} {:name=>"_ga", :value=>"GA1.2.1383421835.1556873499", :path=>"/", :domain=>".tutorialspoint.com", :expires=>2021-05-02 08:51:37 +0000, :secure=>false} After delete cookie with name _ga {:name=>"_gat_gtag_UA_232293_6", :value=>"1", :path=>"/", :domain=>".tutorialspoint.com", :expires=>2019-05-03 08:52:38 +0000, :secure=>false} {:name=>"_gid", :value=>"GA1.2.1385195240.1556873499", :path=>"/", :domain=>".tutorialspoint.com", :expires=>2019-05-04 08:51:37 +0000, :secure=>false}
广告位