/*
Copyright 2007-2009 WebDriver committers
Copyright 2007-2009 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.openqa.selenium;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.core.IsNot.not;
import static org.openqa.selenium.Ignore.Driver.IE;
import org.openqa.selenium.environment.GlobalTestEnvironment;
import org.openqa.selenium.environment.webserver.AppServer;
import java.net.URL;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;
public class CookieImplementationTest extends AbstractDriverTestCase {
public void testAddCookiesWithDifferentPaths() {
driver.get(simpleTestPage);
driver.manage().deleteAllCookies();
Cookie cookie1 = new Cookie("fish", "cod", "/animals");
Cookie cookie2 = new Cookie("planet", "earth", "/galaxy");
WebDriver.Options options = driver.manage();
options.addCookie(cookie1);
options.addCookie(cookie2);
AppServer appServer = GlobalTestEnvironment.get().getAppServer();
driver.get(appServer.whereIs("animals"));
Set<Cookie> cookies = options.getCookies();
assertThat(cookies.contains(cookie1), is(true));
assertThat(cookies.contains(cookie2), is(false));
driver.get(appServer.whereIs("galaxy"));
cookies = options.getCookies();
assertThat(cookies.contains(cookie1), is(false));
assertThat(cookies.contains(cookie2), is(true));
}
public void testGetAllCookies() {
driver.get(simpleTestPage);
driver.manage().deleteAllCookies();
long time = System.currentTimeMillis() + (60 * 60 * 24);
Cookie cookie1 = new Cookie("fish", "cod", "", new Date(time));
Cookie cookie2 = new Cookie("planet", "earth");
WebDriver.Options options = driver.manage();
options.addCookie(cookie1);
options.addCookie(cookie2);
Set<Cookie> cookies = options.getCookies();
assertThat(cookies.contains(cookie1), is(true));
assertThat(cookies.contains(cookie2), is(true));
}
@Ignore(IE)
public void testCookieIntegrity() {
String url = GlobalTestEnvironment.get().getAppServer().whereElseIs("animals");
driver.get(url);
driver.manage().deleteAllCookies();
Calendar c = Calendar.getInstance();
long time = System.currentTimeMillis() + (60 * 60 * 24);
Cookie cookie1 = new Cookie("fish", "cod", "/animals", new Date(time));
WebDriver.Options options = driver.manage();
options.addCookie(cookie1);
Set<Cookie> cookies = options.getCookies();
Iterator<Cookie> iter = cookies.iterator();
Cookie retrievedCookie = null;
while(iter.hasNext()) {
Cookie temp = iter.next();
if (cookie1.equals(temp)) {
retrievedCookie = temp;
break;
}
}
assertThat(retrievedCookie, is(notNullValue()));
//Cookie.equals only compares name, domain and path
assertThat(retrievedCookie, equalTo(cookie1));
assertThat(retrievedCookie.getValue(), equalTo(cookie1.getValue()));
// assertThat(retrievedCookie.getExpiry(), equalTo(cookie1.getExpiry()));
assertThat(retrievedCookie.isSecure(), equalTo(cookie1.isSecure()));
}
public void testDeleteAllCookies() {
driver.get(simpleTestPage);
Cookie cookie1 = new Cookie("fish", "cod");
Cookie cookie2 = new Cookie("planet", "earth");
WebDriver.Options options = driver.manage();
options.addCookie(cookie1);
options.addCookie(cookie2);
Set<Cookie> cookies = options.getCookies();
assertThat(cookies.contains(cookie1), is(true));
assertThat(cookies.contains(cookie2), is(true));
options.deleteAllCookies();
driver.get(simpleTestPage);
cookies = options.getCookies();
assertThat(cookies.contains(cookie1), is(false));
assertThat(cookies.contains(cookie2), is(false));
}
public void testDeleteCookie() {
driver.get(simpleTestPage);
Cookie cookie1 = new Cookie("fish", "cod");
Cookie cookie2 = new Cookie("planet", "earth");
WebDriver.Options options = driver.manage();
options.addCookie(cookie1);
options.addCookie(cookie2);
options.deleteCookie(cookie1);
Set<Cookie> cookies = options.getCookies();
assertThat(cookies.size(), equalTo(1));
assertThat(cookies, hasItem(cookie2));
}
public void testDeleteCookieWithName() {
driver.get(simpleTestPage);
driver.manage().deleteAllCookies();
String cookieOneName = "fish";
String cookieTwoName = "planet";
String cookieThreeName = "three";
Cookie cookie1 = new Cookie(cookieOneName, "cod");
Cookie cookie2 = new Cookie(cookieTwoName, "earth");
Cookie cookie3 = new Cookie(cookieThreeName, "three");
WebDriver.Options options = driver.manage();
options.addCookie(cookie1);
options.addCookie(cookie2);
options.addCookie(cookie3);
options.deleteCookieNamed(cookieOneName);
options.deleteCookieNamed(cookieTwoName);
Set<Cookie> cookies = options.getCookies();
//cookie without domain gets deleted
assertThat(cookies, not(hasItem(cookie1)));
//cookie with domain gets deleted
assertThat(cookies, not(hasItem(cookie2)));
//cookie not deleted
assertThat(cookies, hasItem(cookie3));
}
public void testShouldNotDeleteCookiesWithASimilarName() {
driver.get(simpleTestPage);
driver.manage().deleteAllCookies();
String cookieOneName = "fish";
Cookie cookie1 = new Cookie(cookieOneName, "cod");
Cookie cookie2 = new Cookie(cookieOneName + "x", "earth");
WebDriver.Options options = driver.manage();
options.addCookie(cookie1);
options.addCookie(cookie2);
options.deleteCookieNamed(cookieOneName);
Set<Cookie> cookies = options.getCookies();
assertThat(cookies, not(hasItem(cookie1)));
assertThat(cookies, hasItem(cookie2));
}
public void testGetCookieDoesNotRetriveBeyondCurrentDomain() {
driver.get(simpleTestPage);
driver.manage().deleteAllCookies();
Cookie cookie1 = new Cookie("fish", "cod");
WebDriver.Options options = driver.manage();
options.addCookie(cookie1);
String url = GlobalTestEnvironment.get().getAppServer().whereElseIs("");
try {
driver.get(url);
} catch (IllegalStateException e) {
if (isIeDriverTimedOutException(e)) {
System.err.println("Looks like IE timed out. Is the site accessible?");
return;
}
}
Set<Cookie> cookies = options.getCookies();
assertThat(cookies, not(hasItem(cookie1)));
}
@Ignore(IE)
public void testShouldBeAbleToSetDomainToTheCurrentDomain() throws Exception {
driver.get(simpleTestPage);
driver.manage().deleteAllCookies();
URL url = new URL(driver.getCurrentUrl());
String host = url.getHost() + ":" + url.getPort();
Cookie cookie1 = new Cookie.Builder("fish", "cod").domain(host).build();
WebDriver.Options options = driver.manage();
options.addCookie(cookie1);
driver.get(javascriptPage);
Set<Cookie> cookies = options.getCookies();
assertThat(cookies, hasItem(cookie1));
}
@Ignore(IE)
public void testShouldNotBeAbleToSetDomainToSomethingThatIsNotTheCurrentDomain() {
driver.get(simpleTestPage);
driver.manage().deleteAllCookies();
Cookie cookie1 = new Cookie.Builder("fish", "cod").domain("example.com").build();
WebDriver.Options options = driver.manage();
try {
options.addCookie(cookie1);
fail("Should not be able to set cookie on another domain");
} catch (WebDriverException e) {
// This is expected
}
}
}
分享到:
相关推荐
在使用Selenium WebDriver进行自动化测试或网页操作时,有时候我们需要模拟用户登录状态,这就涉及到添加cookie的操作。本文将详细介绍如何使用Selenium WebDriver添加cookie,并通过实际案例帮助理解其过程。 一、...
WebDriver API 是一种自动化测试工具,用于模拟用户与网页的交互。这个中文版文档详细介绍了如何在不同的浏览器上使用 WebDriver,并提供了各种操作页面元素的方法。以下是对这些知识点的深入阐述: ### 第1章:...
此外,Cookie处理和自动化登录也是提高自动化测试效率的重要环节。 本书的特点是包含了多种编程语言的实践篇幅,例如Python篇、Ruby篇和Java篇,这表明Selenium-WebDriver支持多种编程语言,让不同语言背景的开发者...
### Selenium跳过WebDriver检测并模拟登录淘宝的知识点详解 #### 一、背景介绍与问题分析 随着互联网技术的发展,各大网站对于用户行为的检测也越来越严格。对于自动化测试工具如Selenium来说,许多网站都设置了...
在网站开发和测试中,了解和操作cookie对于模拟真实用户行为非常有用。 ChromeDriver是Selenium WebDriver的一个实现,用于驱动谷歌浏览器。Selenium WebDriver是一个开放源代码的API,允许开发者通过编程方式控制...
Webdriver提供了对cookies的操作,如获取所有cookies、删除所有cookies、获取特定的cookie、删除特定的cookie、添加一个cookie等。例如,driver.get_cookies()可以获取所有的cookies,driver.delete_all_cookies()...
- 模拟不同浏览器进行自动化测试。 - 分析用户设备特性以优化用户体验。 - 实现基于cookie的身份验证或会话管理。 总结 通过C#的WebBrowser控件,我们可以方便地获取网页的cookie和userAgent信息,这对于开发Web...
Selenium WebDriver是一种流行的自动化测试工具,用于测试web应用程序。它通过模拟用户操作来检查web应用程序的各个方面是否按预期工作。Selenium WebDriver支持多种编程语言,但通常与Java一起使用。它允许自动化...
- **定义**: Selenium WebDriver 是一种用于自动化 Web 测试的工具,能够直接与浏览器交互,并且支持多种编程语言,例如 Java、Python、C# 和 Ruby 等。 - **特性**: 它可以模拟用户的行为,如点击按钮、填写表单、...
【WebDriver实用指南Java版本】是乙醇老师精心创作的一份57页的详细教程,涵盖了自动化测试中的多种关键操作。WebDriver是一种广泛使用的自动化测试工具,它允许开发者通过编写代码来控制浏览器,执行各种用户交互,...
4. **自动化测试**:在自动化测试场景下,比如使用Selenium WebDriver,获取和设置Cookie也是常见需求。例如,可以通过`driver.manage().getCookies()`获取所有Cookie,或者用`driver.manage().addCookie()`添加新的...
Python Webdriver Exceptions Cheat Sheet是一份由灰蓝整理编辑的文档,专注于列出和解释在使用Python的webdriver进行自动化测试时可能遇到的各种异常。webdriver是Web自动化测试工具,允许测试人员模拟用户与网页的...
在自动化测试中,例如使用Selenium WebDriver,如果每个测试都需要独立登录,那么Cookie池可以避免反复进行登录操作,提高测试速度。在网络爬虫中,Cookie池可以帮助爬虫在模拟登录后保持登录状态,遍历需要权限访问...
在处理cookies的部分,将已有的cookie信息(如PHPSESSID)存储为字典,然后使用`add_cookie`方法将其添加到浏览器会话中,刷新页面使服务器接收并验证这些cookies,从而达到绕过验证码的目的。 之后,实战中演示了...
我必须测试在所有情况下都正确设置了cookie。 当我开始编写测试用例时,我发现没有在Selenium中添加“自定义”请求标头的“官方”方法。 实际上,这是团队明确拒绝实现的功能: 解决此问题的两种可能方法是: 在...
本次我们将探讨四个关键知识点:下拉框选择、文件上传、Cookie操作以及实战代码示例,这些都是UI自动化测试中常见的场景。 1. **下拉框选择**: 下拉框在Web界面中广泛应用,用于提供用户多项可选的选项。在自动化...
WebDriver能够更好地支持动态网页的测试,其目标是提供一个面向对象的、设计良好的API,以支持现代的Web应用程序测试问题。 接下来,文档介绍了如何搭建自动化测试环境。搭建过程包括Python的安装与配置,以及...
在本文中,我们将探讨如何使用Selenium 3.7和Python 3来添加cookie并模拟登录。Selenium是一个强大的自动化测试工具,特别适用于网页应用的交互测试。在Python 3环境中,我们可以利用Selenium库来模拟用户行为,包括...