[webdriver]cookie测试

简介:
/* 
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 
    } 
  } 
}
目录
相关文章
|
3月前
|
Web App开发 JavaScript Java
自动化测试的利剑:Selenium WebDriver入门与实践
【9月更文挑战第21天】在软件开发的海洋中,自动化测试犹如一艘船,帮助开发者们快速航行至质量保证的彼岸。本文将作为你的罗盘,指引你了解和掌握Selenium WebDriver这一强大的自动化测试工具。通过深入浅出的方式,我们将探索Selenium WebDriver的基本概念、安装过程以及编写简单测试脚本的方法。无论你是刚接触自动化测试的新手,还是希望提升测试技能的开发者,这篇文章都将为你提供有价值的指导。
|
3月前
|
Web App开发 Java 测试技术
自动化测试的利器:Selenium WebDriver入门与实践
【9月更文挑战第8天】在软件开发的海洋中,测试是确保我们不会溺水的那根救生索。Selenium WebDriver,作为自动化测试的明星工具,让这根救生索更加结实可靠。本文将带你快速上手Selenium WebDriver,从基础设置到实际操作,再到实战演练,让你的开发之旅更加平稳顺畅。
|
4月前
|
XML Java 测试技术
Selenium WebDriver自动化测试(基础篇):不得不掌握的Java基础
关于Selenium WebDriver自动化测试的Java基础篇,涵盖了Java的变量、数据类型、字符串操作、运算符、流程控制、面向对象编程、关键字用法、权限修饰符、异常处理和IO流等基础知识点,为进行自动化测试提供了必要的Java语言基础。
110 1
|
3月前
|
JavaScript 前端开发 Windows
使用webdriver实现基于GUI的测试
本文介绍的webdriver是playwright的插件
24 0
|
4月前
|
Web App开发 Java 测试技术
自动化测试的新篇章:使用Selenium WebDriver进行高效测试
【8月更文挑战第31天】 在软件开发的海洋中,自动化测试犹如一艘航船,带领着质量保证团队驶向效率与精准的彼岸。本文将揭开Selenium WebDriver的神秘面纱,通过实际案例引导您掌握这一强大的自动化测试工具。我们将从Selenium WebDriver的基础概念出发,逐步深入到代码示例,最后探讨其在现实项目中的应用场景和优势,旨在为您的软件测试之旅提供清晰的指南。
|
4月前
|
Web App开发 敏捷开发 Java
自动化测试中的神器 —— Selenium WebDriver
【8月更文挑战第31天】在软件开发的海洋里,自动化测试是那艘能带领我们驶向高效与精准彼岸的帆船。本文将带你领略Selenium WebDriver的魅力,从安装到实战,一步步解锁自动化测试的新境界。你将看到代码如何化繁为简,让重复的测试工作变得轻松愉快。让我们一起驾驭这股代码的风,向着软件质量的灯塔进发!
|
4月前
|
Web App开发 敏捷开发 测试技术
自动化测试之美:使用Selenium WebDriver进行网页功能验证
【8月更文挑战第29天】在数字时代,软件质量是企业竞争力的关键。本文将深入探讨如何通过Selenium WebDriver实现自动化测试,确保网页应用的可靠性和性能。我们将从基础设置到编写测试用例,逐步引导读者掌握这一强大的测试工具,同时分享实战经验,让测试不再是开发的负担,而是质量保证的利器。
|
4月前
|
Web App开发 Java 测试技术
自动化测试中的神器:Selenium WebDriver
【8月更文挑战第2天】在软件开发的海洋中,自动化测试犹如一艘航船,而Selenium WebDriver则是这艘航船上的强力引擎。本文将带你领略Selenium WebDriver的魅力,从安装到实战应用,一步步揭开其神秘的面纱,让你在自动化测试的旅程上乘风破浪。
37 1
|
4月前
|
Web App开发 测试技术 API
Web自动化测试框架(基础篇)--Selenium WebDriver工作原理和环境搭建
本文详细介绍了Selenium WebDriver的工作原理,包括其架构、通信机制及支持的浏览器,并指导读者如何在Python环境下搭建Selenium WebDriver的测试环境,从安装Python和Selenium库到编写并运行第一个自动化测试脚本。
239 0
|
5月前
|
Web App开发 Java 测试技术
探索自动化测试的利器:Selenium WebDriver
【7月更文挑战第31天】在软件开发的海洋中,自动化测试如同指南针,为质量保驾护航。本文将深入介绍Selenium WebDriver这一强大的自动化测试工具,通过实际代码示例,揭示其在提高测试效率与准确性方面的巨大潜力。
34 0