[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 
    } 
  } 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值