[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
    评论
Selenium Firefox WebDriver是一个用于自动化测试的工具,它可以模拟用户在Firefox浏览器中的操作。使用Selenium WebDriver执行自动化测试时,会在浏览器上留下一些痕迹,比如浏览历史、Cookie信息等。 一些常见的痕迹包括: 1. 浏览历史:Selenium WebDriver在执行测试时会访问不同的网页,这些访问记录会留在浏览器的历史记录中。 2. Cookie信息:当WebDriver打开一个网页时,网站可能会设置一些Cookie信息,这些信息会被浏览器保存下来。下次再次访问该网站时,浏览器会将这些Cookie信息发送给服务器。 3. 缓存文件:浏览器在访问网页时会将一些资源文件(如图片、CSS文件、JavaScript文件等)缓存到本地,以减少后续加载时间。这些缓存文件可能会留下痕迹。 如果你想清除这些痕迹,可以使用Selenium WebDriver的一些方法来进行操作,比如: 1. 清除浏览历史:可以使用`driver.manage().deleteAllCookies()`方法来删除所有Cookie信息。 2. 清除特定的Cookie信息:可以使用`driver.manage().deleteCookieNamed("cookie_name")`方法来删除指定名称的Cookie。 3. 清除缓存文件:可以使用`driver.manage().deleteAllCookies()`方法来删除所有Cookie信息,这也会清除浏览器的缓存文件。 需要注意的是,清除这些痕迹可能会影响到测试的结果,因为某些网站可能依赖于Cookie信息或缓存文件来进行正常的操作。在执行测试之前,你可以先调用相应的清除方法,然后再开始测试

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值