private BasicClientCookie duplicateCookie(Cookie seleniumCookie) { BasicClientCookie duplicateCookie = new BasicClientCookie(seleniumCookie.getName(), seleniumCookie.getValue()); duplicateCookie.setDomain(seleniumCookie.getDomain()); duplicateCookie.setAttribute(BasicClientCookie.DOMAIN_ATTR, seleniumCookie.getDomain()); duplicateCookie.setSecure(seleniumCookie.isSecure()); duplicateCookie.setExpiryDate(seleniumCookie.getExpiry()); duplicateCookie.setPath(seleniumCookie.getPath()); return duplicateCookie; } }
public void addCookie(Cookie cookie) { StringBuilder sb = new StringBuilder(cookie.getName()); sb.append("=").append(cookie.getValue()).append("; "); if (cookie.getPath() != null && !"".equals(cookie.getPath())) { sb.append("path=").append(cookie.getPath()).append("; "); } if (cookie.getDomain() != null && !"".equals(cookie.getDomain())) { String domain = cookie.getDomain(); int colon = domain.indexOf(":"); if (colon != -1) { domain = domain.substring(0, colon); } sb.append("domain=").append(domain).append("; "); } if (cookie.getExpiry() != null) { sb.append("expires="); sb.append(new SimpleDateFormat("EEE, d MMM yyyy hh:mm:ss z").format(cookie.getExpiry())); } executeScript("document.cookie = arguments[0]", sb.toString()); }
/** * {@inheritDoc} */ @Override public void deleteCookie(Cookie cookie) { List<org.apache.http.cookie.Cookie> cookies = cookieStore.getCookies(); String toDelete = new StringBuilder().append(cookie.getDomain().toLowerCase()) .append("\n").append(cookie.getName().toLowerCase()) .append("\n").append(cookie.getPath().toLowerCase()).toString(); for (org.apache.http.cookie.Cookie cur : cookies) { String curString = new StringBuilder().append(cur.getDomain().toLowerCase()) .append("\n").append(cur.getName().toLowerCase()) .append("\n").append(cur.getPath().toLowerCase()).toString(); if (toDelete.equals(curString)) { removeFromCookieStore(cur); } } }
public void deleteCookie(Cookie cookie) { if (cookieManager == null) { throw new UnsupportedOperationException( "Deleting cookies are not supported without the cookie-manager service"); } cookieManager.removeCookie(cookie.getDomain(), cookie.getPath(), cookie.getName()); gc(); //Date dateInPast = new Date(0); Cookie toDelete = new Cookie(cookie.getName(), //cookie.getValue(), cookie.getDomain(), cookie.getPath(), dateInPast, false); //addCookie(toDelete); }
/** * Adds a cookie to a URL domain. * * @param url to add the cookie to * @param cookie Cookie to be added */ /* package */ void addCookie(String url, Cookie cookie) { URL urlObj; try { urlObj = new URL(url); } catch (MalformedURLException e) { throw new WebDriverException("Error while adding cookie. ", e); } String domain = "http://" + urlObj.getHost() + cookie.getPath(); if (!domain.endsWith("/")) { domain = domain + "/"; } cookieManager.setCookie(domain, stringifyCookie(cookie)); }
public void addCookie(Cookie cookie) { // TODO: Numeric overflow if (cookie.getExpiry() == null) { cookie = new Cookie(cookie.getName(), cookie.getValue(), cookie.getDomain(), cookie.getPath(), new Date(new Date().getTime() + (10 * 365 * 24 * 60 * 60 * 1000)), false); } debugger.executeJavascript("document.cookie='" + cookie.toString() + "'", false); }
public Map<String, Object> toJson() { Map<String, Object> toReturn = new TreeMap<>(); if (getDomain() != null) { toReturn.put("domain", getDomain()); } if (getExpiry() != null) { toReturn.put("expiry", getExpiry()); } if (getName() != null) { toReturn.put("name", getName()); } if (getPath() != null) { toReturn.put("path", getPath()); } if (getValue() != null) { toReturn.put("value", getValue()); } toReturn.put("secure", isSecure()); toReturn.put("httpOnly", isHttpOnly()); return toReturn; }
private void deleteCookieByPath(Cookie cookie) { String path = cookie.getPath(); if (path != null) { String[] segments = cookie.getPath().split("/"); StringBuilder currentPath = new StringBuilder(); for (String segment : segments) { if ("".equals(segment)) continue; currentPath.append("/").append(segment); Cookie toDelete = new NullPathCookie(cookie.getName(), cookie.getValue(), cookie.getDomain(), currentPath.toString(), new Date(0)); recursivelyDeleteCookieByDomain(toDelete); } } Cookie toDelete = new NullPathCookie(cookie.getName(), cookie.getValue(), cookie.getDomain(), "/", new Date(0)); recursivelyDeleteCookieByDomain(toDelete); toDelete = new NullPathCookie(cookie.getName(), cookie.getValue(), cookie.getDomain(), null, new Date(0)); recursivelyDeleteCookieByDomain(toDelete); }
private void recursivelyDeleteCookieByDomain(Cookie cookie) { addCookie(cookie); int dotIndex = cookie.getDomain().indexOf('.'); if (dotIndex == 0) { String domain = cookie.getDomain().substring(1); Cookie toDelete = new NullPathCookie(cookie.getName(), cookie.getValue(), domain, cookie.getPath(), new Date(0)); recursivelyDeleteCookieByDomain(toDelete); } else if (dotIndex != -1) { String domain = cookie.getDomain().substring(dotIndex); Cookie toDelete = new NullPathCookie(cookie.getName(), cookie.getValue(), domain, cookie.getPath(), new Date(0)); recursivelyDeleteCookieByDomain(toDelete); } else { Cookie toDelete = new NullPathCookie(cookie.getName(), cookie.getValue(), "", cookie.getPath(), new Date(0)); addCookie(toDelete); } }
@RequestMapping(method = RequestMethod.GET, value = "/session/{domain}") public List<Cookie> getSession(@PathVariable String domain) { return authService.getSession(domain).stream() .map(cookie -> new Cookie.Builder() .withDomain(cookie.getDomain()) .withExpiry(cookie.getExpiry()) .withIsHttpOnly(cookie.isHttpOnly()) .withIsSecure(cookie.isSecure()) .withName(cookie.getName()) .withPath(cookie.getPath()) .withValue(cookie.getValue()) .build() ).collect(toList()); }
public MockBrowser(HttpClient httpClient) { _httpClient = httpClient; HttpConnectionManager connectionManager = httpClient.getHttpConnectionManager(); if (connectionManager instanceof MultiThreadedHttpConnectionManager) { _threadPool = Executors.newFixedThreadPool(10); } else { LOG.warn("The configured HttpClient does not use a MultiThreadedHttpConnectionManager, will only use 1 thread (instead of 10) for downloading CSS files and checking image URLs ..."); _threadPool = Executors.newFixedThreadPool(1); } HttpState httpState = new HttpState(); WebDriver driver = _webPage.getDriver(); for (org.openqa.selenium.Cookie cookie : driver.manage().getCookies()) { httpState.addCookie(new Cookie(cookie.getDomain(), cookie.getName(), cookie.getValue(), cookie.getPath(), cookie.getExpiry(), cookie.isSecure())); } _httpClient.setState(httpState); }
private com.gargoylesoftware.htmlunit.util.Cookie convertSeleniumCookieToHtmlUnit(Cookie cookie) { return new com.gargoylesoftware.htmlunit.util.Cookie( cookie.getDomain(), cookie.getName(), cookie.getValue(), cookie.getPath(), cookie.getExpiry(), cookie.isSecure(), cookie.isHttpOnly() ); }
/** * Add cookie information. * * @param cookie cookie. */ public void add(Cookie cookie) { CookieKey key = new CookieKey( cookie.getName(), cookie.getPath(), cookie.getDomain()); CookieValue value = new CookieValue( key, cookie.getValue(), cookie.getExpiry()); put(key, value); }
private void assertThatCookieContentIsIdentical(org.openqa.selenium.Cookie cookie, Cookie expectedCookie) { assertThat(cookie.getName(), is(expectedCookie.name)); assertThat(cookie.getValue(), is(expectedCookie.value)); assertThat(cookie.getDomain(), is(expectedCookie.domain)); assertThat(cookie.getPath(), is(expectedCookie.path != null ? expectedCookie.path : "/")); assertThat(cookie.getExpiry(), is(expectedCookie.expiry)); assertThat(cookie.isSecure(), is(expectedCookie.secure)); }
/** * Get all the cookies for the current domain. This is the equivalent of calling "document.cookie" and parsing the result * * @return {@link com.axway.ats.uiengine.elements.html.Cookie Cookie}s array */ @PublicAtsApi public com.axway.ats.uiengine.elements.html.Cookie[] getCookies() { Set<Cookie> cookies = webDriver.manage().getCookies(); com.axway.ats.uiengine.elements.html.Cookie[] cookiesArr = new com.axway.ats.uiengine.elements.html.Cookie[cookies.size()]; int i = 0; for (Cookie c : cookies) { cookiesArr[i++] = new com.axway.ats.uiengine.elements.html.Cookie(c.getName(), c.getValue(), c.getDomain(), c.getPath(), c.getExpiry(), c.isSecure()); } return cookiesArr; }
/** * Load in all the cookies WebDriver currently knows about so that we can mimic the browser cookie state * * @param seleniumCookieSet * @return */ private HttpState mimicCookieState(Set<org.openqa.selenium.Cookie> seleniumCookieSet) { HttpState mimicWebDriverCookieState = new HttpState(); for (org.openqa.selenium.Cookie seleniumCookie : seleniumCookieSet) { Cookie httpClientCookie = new Cookie(seleniumCookie.getDomain(), seleniumCookie.getName(), seleniumCookie.getValue(), seleniumCookie.getPath(), seleniumCookie.getExpiry(), seleniumCookie.isSecure()); mimicWebDriverCookieState.addCookie(httpClientCookie); } return mimicWebDriverCookieState; }
/** * Converts Selenium cookie to Apache http client. * @param browserCookie selenium cookie. * @return http client format. */ protected ClientCookie convertCookie(Cookie browserCookie) { BasicClientCookie cookie = new BasicClientCookie(browserCookie.getName(), browserCookie.getValue()); String domain = browserCookie.getDomain(); if (domain != null && domain.startsWith(".")) { // http client does not like domains starting with '.', it always removes it when it receives them domain = domain.substring(1); } cookie.setDomain(domain); cookie.setPath(browserCookie.getPath()); cookie.setExpiryDate(browserCookie.getExpiry()); cookie.setSecure(browserCookie.isSecure()); if (browserCookie.isHttpOnly()) { cookie.setAttribute("httponly", ""); } return cookie; } }
public void addCookie(Cookie cookie) { Page page = lastPage(); if (!(page instanceof HtmlPage)) { throw new WebDriverException("You may not set cookies on a page that is not HTML"); } // Cookies only make sense if the page is String domain = getDomainForCookie(cookie); verifyDomain(cookie, domain); webClient.getCookieManager().addCookie(new org.apache.commons.httpclient.Cookie(domain, cookie.getName(), cookie.getValue(), cookie.getPath(), cookie.getExpiry(), cookie.isSecure())); }
@Override public void addCookie(Cookie cookie) { Page page = lastPage(); if (!(page instanceof HtmlPage)) { throw new UnableToSetCookieException("You may not set cookies on a page that is not HTML"); } String domain = getDomainForCookie(); verifyDomain(cookie, domain); getWebClient().getCookieManager().addCookie( new com.gargoylesoftware.htmlunit.util.Cookie(domain, cookie.getName(), cookie.getValue(), cookie.getPath(), cookie.getExpiry(), cookie.isSecure())); }
/** * Load in all the cookies WebDriver currently knows about so that we can mimic the browser cookie state * * @param seleniumCookieSet Set<Cookie> */ private BasicCookieStore mimicCookieState(Set<Cookie> seleniumCookieSet) { BasicCookieStore copyOfWebDriverCookieStore = new BasicCookieStore(); for (Cookie seleniumCookie : seleniumCookieSet) { BasicClientCookie duplicateCookie = new BasicClientCookie(seleniumCookie.getName(), seleniumCookie.getValue()); duplicateCookie.setDomain(seleniumCookie.getDomain()); duplicateCookie.setSecure(seleniumCookie.isSecure()); duplicateCookie.setExpiryDate(seleniumCookie.getExpiry()); duplicateCookie.setPath(seleniumCookie.getPath()); copyOfWebDriverCookieStore.addCookie(duplicateCookie); } return copyOfWebDriverCookieStore; }