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; } }
cookieMap.put("domain", cookie.getDomain()); cookieMap.put("path", cookie.getPath()); cookieMap.put("secure", cookie.isSecure()); cookieMap.put("expiry", cookie.getExpiry()); parsedParameter = new JSONObject(cookieMap).toString();
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; }
@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()); }
private org.apache.http.cookie.Cookie convert(Cookie in) { BasicClientCookie out = new BasicClientCookie(in.getName(), in.getValue()); String domainStr = null; if (StringUtils.isEmpty(in.getDomain())) { String urlStr = context.item().engine.get().getLocation(); try { URL url = new URL(urlStr); domainStr = url.getHost(); } catch (MalformedURLException e) { Matcher matcher = domain.matcher(urlStr); if (matcher.matches()) { domainStr = matcher.group(1); } } } out.setDomain(domainStr == null ? in.getDomain() : domainStr); if (in.getExpiry() != null) { out.setExpiryDate(in.getExpiry()); } out.setPath(in.getPath()); out.setSecure(in.isSecure()); out.setValue(in.getValue()); out.setVersion(1); return out; }
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() ); }
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; }
/** * 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; } }
/** * 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; }
@Test public void shouldSetCookies() { //given ArgumentCaptor<org.openqa.selenium.Cookie> cookieCaptor = ArgumentCaptor.forClass(org.openqa.selenium.Cookie.class); Cookie cookieOne = new Cookie("someName", "someValue", "someDomain", "somePath", new Date(10000L), true); Cookie cookieTwo = new Cookie("someOtherName", "someOtherValue", "someOtherDomain", "someOtherPath", new Date(10000000000L), false); //when testee.setCookies(ImmutableList.of(cookieOne, cookieTwo)); //then verify(webDriverOptionsMock, times(2)).addCookie(cookieCaptor.capture()); List<org.openqa.selenium.Cookie> capturedCookies = cookieCaptor.getAllValues(); assertEquals("someName", capturedCookies.get(0).getName()); assertEquals("someValue", capturedCookies.get(0).getValue()); assertEquals("someDomain", capturedCookies.get(0).getDomain()); assertEquals("somePath", capturedCookies.get(0).getPath()); assertEquals(new Date(10000L), capturedCookies.get(0).getExpiry()); Assert.assertTrue(capturedCookies.get(0).isSecure()); assertEquals("someOtherName", capturedCookies.get(1).getName()); assertEquals("someOtherValue", capturedCookies.get(1).getValue()); assertEquals("someOtherDomain", capturedCookies.get(1).getDomain()); assertEquals("someOtherPath", capturedCookies.get(1).getPath()); assertEquals(new Date(10000000000L), capturedCookies.get(1).getExpiry()); assertFalse(capturedCookies.get(1).isSecure()); }
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; }