How to use isHttpOnly method of org.openqa.selenium.Cookie.Builder class

Best Selenium code snippet using org.openqa.selenium.Cookie.Builder.isHttpOnly

Source:CookieImplementationTest.java Github

copy

Full Screen

...314 public void testRetainsHttpOnlyFlag() {315 Cookie addedCookie =316 new Cookie.Builder("fish", "cod")317 .path("/common/animals")318 .isHttpOnly(true)319 .build();320 addCookieOnServerSide(addedCookie);321 Cookie retrieved = driver.manage().getCookieNamed("fish");322 assertNotNull(retrieved);323 assertTrue(retrieved.isHttpOnly());324 }325 @Ignore(ANDROID)326 @Test327 public void testSettingACookieThatExpiredInThePast() {328 long expires = System.currentTimeMillis() - 1000;329 Cookie cookie = new Cookie.Builder("expired", "yes").expiresOn(new Date(expires)).build();330 driver.manage().addCookie(cookie);331 cookie = driver.manage().getCookieNamed("fish");332 assertNull(333 "Cookie expired before it was set, so nothing should be returned: " + cookie, cookie);334 }335 @Test336 public void testCanSetCookieWithoutOptionalFieldsSet() {337 String key = generateUniqueKey();338 String value = "foo";339 Cookie cookie = new Cookie(key, value);340 assertCookieIsNotPresentWithName(key);341 driver.manage().addCookie(cookie);342 assertCookieHasValue(key, value);343 }344 @Test345 public void testDeleteNotExistedCookie() {346 String key = generateUniqueKey();347 assertCookieIsNotPresentWithName(key);348 driver.manage().deleteCookieNamed(key);349 }350 @Ignore(value = {ANDROID, CHROME, FIREFOX, IE, OPERA, OPERA_MOBILE, PHANTOMJS, SAFARI})351 @Test352 public void testShouldDeleteOneOfTheCookiesWithTheSameName() {353 driver.get(domainHelper.getUrlForFirstValidHostname("/common/animals"));354 Cookie cookie1 = new Cookie.Builder("fish", "cod")355 .domain(domainHelper.getHostName()).path("/common/animals").build();356 Cookie cookie2 = new Cookie.Builder("fish", "tune")357 .domain(domainHelper.getHostName()).path("/common/").build();358 WebDriver.Options options = driver.manage();359 options.addCookie(cookie1);360 options.addCookie(cookie2);361 assertEquals(driver.manage().getCookies().size(), 2);362 driver.manage().deleteCookie(cookie1);363 assertEquals(driver.manage().getCookies().size(), 1);364 Cookie retrieved = driver.manage().getCookieNamed("fish");365 assertNotNull("Cookie was null", retrieved);366 assertEquals(cookie2, retrieved);367 }368 private String generateUniqueKey() {369 return String.format("key_%d", random.nextInt());370 }371 private void assertNoCookiesArePresent() {372 Set<Cookie> cookies = driver.manage().getCookies();373 assertTrue("Cookies were not empty, present: " + cookies,374 cookies.isEmpty());375 String documentCookie = getDocumentCookieOrNull();376 if (documentCookie != null) {377 assertEquals("Cookies were not empty", "", documentCookie);378 }379 }380 private void assertSomeCookiesArePresent() {381 assertFalse("Cookies were empty",382 driver.manage().getCookies().isEmpty());383 String documentCookie = getDocumentCookieOrNull();384 if (documentCookie != null) {385 assertNotSame("Cookies were empty", "", documentCookie);386 }387 }388 private void assertCookieIsNotPresentWithName(final String key) {389 assertNull("Cookie was present with name " + key, driver.manage().getCookieNamed(key));390 String documentCookie = getDocumentCookieOrNull();391 if (documentCookie != null) {392 assertThat("Cookie was present with name " + key,393 documentCookie,394 not(containsString(key + "=")));395 }396 }397 private void assertCookieIsPresentWithName(final String key) {398 assertNotNull("Cookie was not present with name " + key, driver.manage().getCookieNamed(key));399 String documentCookie = getDocumentCookieOrNull();400 if (documentCookie != null) {401 assertThat("Cookie was not present with name " + key + ", got: " + documentCookie,402 documentCookie,403 containsString(key + "="));404 }405 }406 private void assertCookieHasValue(final String key, final String value) {407 assertEquals("Cookie had wrong value",408 value,409 driver.manage().getCookieNamed(key).getValue());410 String documentCookie = getDocumentCookieOrNull();411 if (documentCookie != null) {412 assertThat("Cookie was present with name " + key,413 documentCookie,414 containsString(key + "=" + value));415 }416 }417 private String getDocumentCookieOrNull() {418 if (!(driver instanceof JavascriptExecutor)) {419 return null;420 }421 try {422 return (String) ((JavascriptExecutor) driver).executeScript("return document.cookie");423 } catch (UnsupportedOperationException e) {424 return null;425 }426 }427 private Date someTimeInTheFuture() {428 return new Date(System.currentTimeMillis() + 100000);429 }430 private void openAnotherPage() {431 driver.get(domainHelper.getUrlForFirstValidHostname("simpleTest.html"));432 }433 private void deleteAllCookiesOnServerSide() {434 driver.get(cookiePage + "?action=deleteAll");435 }436 private void addCookieOnServerSide(Cookie cookie) {437 StringBuilder url = new StringBuilder(cookiePage);438 url.append("?action=add");439 url.append("&name=").append(cookie.getName());440 url.append("&value=").append(cookie.getValue());441 if (cookie.getDomain() != null) {442 url.append("&domain=").append(cookie.getDomain());443 }444 if (cookie.getPath() != null) {445 url.append("&path=").append(cookie.getPath());446 }447 if (cookie.getExpiry() != null) {448 url.append("&expiry=").append(cookie.getExpiry().getTime());449 }450 if (cookie.isSecure()) {451 url.append("&secure=").append(cookie.isSecure());452 }453 if (cookie.isHttpOnly()) {454 url.append("&httpOnly=").append(cookie.isHttpOnly());455 }456 driver.get(url.toString());457 }458}...

Full Screen

Full Screen

Source:WebDriverService.java Github

copy

Full Screen

...78 cookieBuilder.append(cookie.getExpiry().toString()).append(SEPERATOR);79 else80 cookieBuilder.append(SEPERATOR);81 cookieBuilder.append(cookie.isSecure()).append(SEPERATOR);82 cookieBuilder.append(cookie.isHttpOnly());83 bufferedWriter.write(cookieBuilder.toString());84 }85 bufferedWriter.close();86 } catch (IOException e) {87 //logging88 }89 }90 public void copyCookiesToDriver() {91 try {92 BufferedReader bufferedReader = new BufferedReader(new FileReader(COOKIE_FILE));93 String line;94 while ((line = bufferedReader.readLine()) != null) {95 String[] split = line.split(SEPERATOR);96 if (split.length != 7)...

Full Screen

Full Screen

Source:SeleniumCookieConverterTest.java Github

copy

Full Screen

...55 .append(b.getName(), a.getName())56 .append(b.getPath(), a.getPath())57 .append(b.getValue(), a.getValue())58 .append(b.isSecure(), a.isSecure())59 .append(b.isHttpOnly(), a.isHttpOnly());60 return eq.isEquals();61 }62 private static DeserializableCookie toDeserializableCookie(org.apache.http.cookie.Cookie reference) {63 DeserializableCookie.Builder c = DeserializableCookie.builder(reference.getName(), reference.getValue());64 c.setComment(reference.getComment());65 c.setDomain(reference.getDomain());66 Date refExpiryDate = reference.getExpiryDate();67 if (refExpiryDate != null) {68 c.expiry(refExpiryDate.toInstant());69 }70 c.setPath(reference.getPath());71 c.setSecure(reference.isSecure());72 return c.build();73 }...

Full Screen

Full Screen

Source:SessionUtil.java Github

copy

Full Screen

...41 String domain = m.getString("domain");42 String path = m.getString("path");43 Boolean secure = m.getBoolean("secure");44 Boolean httpOnly = m.getBoolean("httpOnly");45 builder.domain(domain).path(path).isSecure(secure).isHttpOnly(httpOnly);46 Long expiry = m.getLong("expiry");47 if (expiry != null) {48 builder.expiresOn(new Date(expiry));49 }50 Cookie cookie = builder.build();51 driver.manage().addCookie(cookie);52 } catch (Exception e) {53 throw new RuntimeException(e);54 }55 });56 JSONArray lc = session.getJSONArray("lc");57 WebStorage webStorage = (WebStorage) new Augmenter().augment(driver);58 LocalStorage localStorage = webStorage.getLocalStorage();59 lc.forEach(o -> {60 try {61 JSONObject item = (JSONObject) o;62 String key = item.getString("key");63 String value = item.getString("value");64 localStorage.setItem(key, value);65 } catch (Exception e) {66 throw new RuntimeException(e);67 }68 });69 }70 public static JSONObject getSessionV2(WebDriver driver) {71 JSONObject session = new JSONObject();72 Set<Cookie> set = driver.manage().getCookies();73 session.put("cookie", set);74 JSONObject lc = new JSONObject();75 WebStorage webStorage = (WebStorage) new Augmenter().augment(driver);76 LocalStorage localStorage = webStorage.getLocalStorage();77 localStorage.keySet().forEach(c -> {78 lc.put(c, localStorage.getItem(c));79 });80 session.put("lc", lc);81 return session;82 }83 public static void setSessionV2(WebDriver driver, JSONObject session) {84 JSONArray cookies = session.getJSONArray("cookie");85 cookies.forEach(o -> {86 try {87 JSONObject m = (JSONObject) o;88 Cookie.Builder builder = new Cookie.Builder(m.getString("name"), m.getString("value"));89 String domain = m.getString("domain");90 String path = m.getString("path");91 Boolean secure = m.getBoolean("secure");92 Boolean httpOnly = m.getBoolean("httpOnly");93 builder.domain(domain).path(path).isSecure(secure).isHttpOnly(httpOnly);94 Long expiry = m.getLong("expiry");95 if (expiry != null) {96 builder.expiresOn(new Date(expiry));97 }98 Cookie cookie = builder.build();99 driver.manage().addCookie(cookie);100 } catch (Exception e) {101 throw new RuntimeException(e);102 }103 });104 JSONObject lc = session.getJSONObject("lc");105 WebStorage webStorage = (WebStorage) new Augmenter().augment(driver);106 LocalStorage localStorage = webStorage.getLocalStorage();107 lc.forEach((k, v) -> {...

Full Screen

Full Screen

Source:Cookie.java Github

copy

Full Screen

...43 }44 public long getExpires() {45 return expires;46 }47 public boolean isHttpOnly() {48 return httpOnly;49 }50 public boolean isSecure() {51 return secure;52 }53 public Cookie(String name, String value, String domain, String path, long expires,54 Boolean httpOnly, Boolean secure) {55 this.name = requireNonNull(name, "'name' is required for Cookie");56 this.value = requireNonNull(value, "'value' is required for Cookie");57 this.domain = requireNonNull(domain, "'domain' is required for Cookie");58 this.path = requireNonNull(path, "'path' is required for Cookie");59 this.expires = expires;60 this.httpOnly = httpOnly;61 this.secure = secure;62 }63 org.openqa.selenium.Cookie asSeleniumCookie() {64 return new org.openqa.selenium.Cookie.Builder(name, value).domain(domain).path(path)65 .expiresOn(new Date(expires)).isSecure(secure).isHttpOnly(httpOnly).build();66 }67 public static Cookie fromSeleniumCookie(org.openqa.selenium.Cookie cookie) {68 return new Cookie(cookie.getName(), cookie.getValue(), cookie.getDomain(), cookie.getPath(),69 cookie.getExpiry() != null ? cookie.getExpiry().getTime() : 0,70 cookie.isHttpOnly(), cookie.isSecure());71 }72 public static Cookie parseCookie(JsonInput input) {73 String name = null;74 String value = null;75 String domain = null;76 String path = null;77 long expires = 0;78 boolean httpOnly = false;79 boolean secure = false;80 input.beginObject();81 while (input.hasNext()) {82 switch (input.nextName()) {83 case "name":84 name = input.nextString();...

Full Screen

Full Screen

Source:CookieAdapter.java Github

copy

Full Screen

...23 }24 if(cookie.getExpiryDate() != null){25 newCookie.expiresOn(cookie.getExpiryDate());26 }27 if(cookie.isHttpOnly()){28 newCookie.isHttpOnly(cookie.isHttpOnly());29 }30 if(cookie.getMaxAge() != -1){31 Calendar currentDate = Calendar.getInstance();32 currentDate.setTimeZone(TimeZone.getTimeZone("GMT"));33 currentDate.add(Calendar.SECOND, cookie.getMaxAge());34 newCookie.expiresOn(currentDate.getTime());35 }36 if(cookie.getPath() != null){37 newCookie.path(cookie.getPath());38 }39 if(cookie.isSecured()){40 newCookie.isSecure(cookie.isSecured());41 }42 if(cookie.getVersion() != -1){43 System.out.println("NOTE: The cookie taken from RestAssured has a version attribute of '" + cookie.getVersion() + "'. Selenium doesn't support the addition of version so this has been discarded");44 }45 return newCookie.build();46 }47 public Cookie convertToRestAssured(org.openqa.selenium.Cookie cookie) {48 Cookie.Builder newCookie = new Cookie.Builder(cookie.getName(), cookie.getValue());49 if(cookie.getDomain() != null){50 newCookie.setDomain(cookie.getDomain());51 }52 if(cookie.getExpiry() != null){53 if(expiryType.equals(ExpiryType.EXPIRY)){54 newCookie.setExpiryDate(cookie.getExpiry());55 } else if(expiryType.equals(ExpiryType.MAXAGE)){56 Calendar futureDate = Calendar.getInstance();57 futureDate.setTime(cookie.getExpiry());58 Calendar currentDate = Calendar.getInstance();59 long end = TimeUnit.MILLISECONDS.toSeconds(futureDate.getTimeInMillis());60 long start = TimeUnit.MILLISECONDS.toSeconds(currentDate.getTimeInMillis());61 int result = toIntExact(end - start);62 newCookie.setMaxAge(result);63 }64 }65 if(cookie.isHttpOnly()){66 newCookie.setHttpOnly(cookie.isHttpOnly());67 }68 if(cookie.isSecure()){69 newCookie.setSecured(cookie.isSecure());70 }71 if(cookie.getPath() != null){72 newCookie.setPath(cookie.getPath());73 }74 return newCookie.build();75 }76}...

Full Screen

Full Screen

Source:AddCookie.java Github

copy

Full Screen

...57 .path(path)58 .domain(domain)59 .isSecure(secure)60 .expiresOn(expiry)61 .isHttpOnly(httpOnly)62 .build();63 }64 65 private boolean getBooleanFromRaw(String key) {66 if (rawCookie.containsKey(key)) {67 Object value = rawCookie.get(key);68 if ((value instanceof Boolean)) {69 return ((Boolean)value).booleanValue();70 }71 if ((value instanceof String)) {72 return ((String)value).equalsIgnoreCase("true");73 }74 }75 return false;...

Full Screen

Full Screen

Source:SeleniumCookieConverter.java Github

copy

Full Screen

...21 .domain(cookie.getDomain())22 .expiry(expiryInstant)23 .path(cookie.getPath())24 .secure(cookie.isSecure())25 .httpOnly(cookie.isHttpOnly()).build();26 return d;27 }28 @Override29 protected org.openqa.selenium.Cookie doBackward(DeserializableCookie d) {30 org.openqa.selenium.Cookie c = new org.openqa.selenium.Cookie(d.getName(), d.getValue(), d.getDomain(), d.getPath(), d.getExpiryAsDate(), d.isSecure(), d.isHttpOnly());31 return c;32 }33}...

Full Screen

Full Screen

isHttpOnly

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.Cookie;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.chrome.ChromeDriver;4import org.openqa.selenium.chrome.ChromeOptions;5import java.util.Date;6public class CookieBuilderDemo {7 public static void main(String[] args) {8 System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");9 ChromeOptions options = new ChromeOptions();10 options.addArguments("start-maximized");11 WebDriver driver = new ChromeDriver(options);12 Cookie.Builder builder = new Cookie.Builder("testCookie",

Full Screen

Full Screen

isHttpOnly

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.Cookie;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.chrome.ChromeDriver;4public class CookieHttpOnly {5 public static void main(String[] args) {6 System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");7 WebDriver driver = new ChromeDriver();8 driver.manage().addCookie(new Cookie.Builder("name", "value")9 .domain("the-internet.herokuapp.com")10 .isHttpOnly(true)11 .build());12 driver.quit();13 }14}

Full Screen

Full Screen

Selenium 4 Tutorial:

LambdaTest’s Selenium 4 tutorial is covering every aspects of Selenium 4 testing with examples and best practices. Here you will learn basics, such as how to upgrade from Selenium 3 to Selenium 4, to some advanced concepts, such as Relative locators and Selenium Grid 4 for Distributed testing. Also will learn new features of Selenium 4, such as capturing screenshots of specific elements, opening a new tab or window on the browser, and new protocol adoptions.

Chapters:

  1. Upgrading From Selenium 3 To Selenium 4?: In this chapter, learn in detail how to update Selenium 3 to Selenium 4 for Java binding. Also, learn how to upgrade while using different build tools such as Maven or Gradle and get comprehensive guidance for upgrading Selenium.

  2. What’s New In Selenium 4 & What’s Being Deprecated? : Get all information about new implementations in Selenium 4, such as W3S protocol adaption, Optimized Selenium Grid, and Enhanced Selenium IDE. Also, learn what is deprecated for Selenium 4, such as DesiredCapabilites and FindsBy methods, etc.

  3. Selenium 4 With Python: Selenium supports all major languages, such as Python, C#, Ruby, and JavaScript. In this chapter, learn how to install Selenium 4 for Python and the features of Python in Selenium 4, such as Relative locators, Browser manipulation, and Chrom DevTool protocol.

  4. Selenium 4 Is Now W3C Compliant: JSON Wireframe protocol is retiring from Selenium 4, and they are adopting W3C protocol to learn in detail about the advantages and impact of these changes.

  5. How To Use Selenium 4 Relative Locator? : Selenium 4 came with new features such as Relative Locators that allow constructing locators with reference and easily located constructors nearby. Get to know its different use cases with examples.

  6. Selenium Grid 4 Tutorial For Distributed Testing: Selenium Grid 4 allows you to perform tests over different browsers, OS, and device combinations. It also enables parallel execution browser testing, reads up on various features of Selenium Grid 4 and how to download it, and runs a test on Selenium Grid 4 with best practices.

  7. Selenium Video Tutorials: Binge on video tutorials on Selenium by industry experts to get step-by-step direction from automating basic to complex test scenarios with Selenium.

Selenium 101 certifications:

LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.

Run Selenium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in Cookie.Builder

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful