Best Selenium code snippet using org.openqa.selenium.Cookie.isSecure
Source:CookieTest.java
...64 Assert.assertEquals("test", cookie.getValue());65 Assert.assertEquals(".localhost", cookie.getDomain());66 Assert.assertEquals("/", cookie.getPath());67 Assert.assertTrue(((cookie.getExpiry()) != null));68 Assert.assertEquals(false, cookie.isSecure());69 org.openqa.selenium.Cookie cookie2 = driver.manage().getCookieNamed("test2");70 Assert.assertEquals("test2", cookie2.getName());71 Assert.assertEquals("test2", cookie2.getValue());72 Assert.assertEquals(".localhost", cookie2.getDomain());73 Assert.assertEquals("/", cookie2.getPath());74 Assert.assertEquals(false, cookie2.isSecure());75 Assert.assertTrue(((cookie2.getExpiry()) == null));76 }77 @Test78 public void gettingAllCookiesOnANonCookieSettingPage() {79 server.setHttpHandler("GET", CookieTest.EMPTY_CALLBACK);80 goToPage();81 Assert.assertEquals(0, getCookies().length);82 }83 @Test84 public void deletingAllCookies() {85 server.setHttpHandler("GET", CookieTest.COOKIE_SETTING_CALLBACK);86 goToPage();87 driver.manage().deleteAllCookies();88 Assert.assertEquals(0, getCookies().length);89 }90 @Test91 public void deletingOneCookie() {92 server.setHttpHandler("GET", CookieTest.COOKIE_SETTING_CALLBACK);93 goToPage();94 driver.manage().deleteCookieNamed("test");95 org.openqa.selenium.Cookie[] cookies = getCookies();96 Assert.assertEquals(1, cookies.length);97 Assert.assertEquals("test2", cookies[0].getName());98 }99 @Test100 public void addingACookie() {101 server.setHttpHandler("GET", CookieTest.EMPTY_CALLBACK);102 goToPage();103 driver.manage().addCookie(new org.openqa.selenium.Cookie("newCookie", "newValue", ".localhost", "/", null, false, false));104 org.openqa.selenium.Cookie[] cookies = getCookies();105 Assert.assertEquals(1, cookies.length);106 Assert.assertEquals("newCookie", cookies[0].getName());107 Assert.assertEquals("newValue", cookies[0].getValue());108 Assert.assertEquals(".localhost", cookies[0].getDomain());109 Assert.assertEquals("/", cookies[0].getPath());110 Assert.assertEquals(false, cookies[0].isSecure());111 Assert.assertEquals(false, cookies[0].isHttpOnly());112 }113 @Test114 public void modifyingACookie() {115 server.setHttpHandler("GET", CookieTest.COOKIE_SETTING_CALLBACK);116 goToPage();117 driver.manage().addCookie(new org.openqa.selenium.Cookie("test", "newValue", "localhost", "/", null, false));118 org.openqa.selenium.Cookie[] cookies = getCookies();119 Assert.assertEquals(2, cookies.length);120 Assert.assertEquals("test", cookies[1].getName());121 Assert.assertEquals("newValue", cookies[1].getValue());122 Assert.assertEquals(".localhost", cookies[1].getDomain());123 Assert.assertEquals("/", cookies[1].getPath());124 Assert.assertEquals(false, cookies[1].isSecure());125 Assert.assertEquals("test2", cookies[0].getName());126 Assert.assertEquals("test2", cookies[0].getValue());127 Assert.assertEquals(".localhost", cookies[0].getDomain());128 Assert.assertEquals("/", cookies[0].getPath());129 Assert.assertEquals(false, cookies[0].isSecure());130 }131 @Test132 public void shouldRetainCookieInfo() {133 server.setHttpHandler("GET", CookieTest.EMPTY_CALLBACK);134 goToPage();135 // Added cookie (in a sub-path - allowed)136 org.openqa.selenium.Cookie addedCookie = // < now + 100sec137 new org.openqa.selenium.Cookie.Builder("fish", "cod").expiresOn(new Date(((System.currentTimeMillis()) + (100 * 1000)))).path("/404").domain("localhost").build();138 driver.manage().addCookie(addedCookie);139 // Search cookie on the root-path and fail to find it140 org.openqa.selenium.Cookie retrieved = driver.manage().getCookieNamed("fish");141 Assert.assertNull(retrieved);142 // Go to the "/404" sub-path (to find the cookie)143 goToPage("404");144 retrieved = driver.manage().getCookieNamed("fish");145 Assert.assertNotNull(retrieved);146 // Check that it all matches147 Assert.assertEquals(addedCookie.getName(), retrieved.getName());148 Assert.assertEquals(addedCookie.getValue(), retrieved.getValue());149 Assert.assertEquals(addedCookie.getExpiry(), retrieved.getExpiry());150 Assert.assertEquals(addedCookie.isSecure(), retrieved.isSecure());151 Assert.assertEquals(addedCookie.getPath(), retrieved.getPath());152 Assert.assertTrue(retrieved.getDomain().contains(addedCookie.getDomain()));153 }154 @Test(expected = InvalidCookieDomainException.class)155 public void shouldNotAllowToCreateCookieOnDifferentDomain() {156 goToPage();157 // Added cookie (in a sub-path)158 org.openqa.selenium.Cookie addedCookie = // < now + 100sec159 new org.openqa.selenium.Cookie.Builder("fish", "cod").expiresOn(new Date(((System.currentTimeMillis()) + (100 * 1000)))).path("/404").domain("github.com").build();160 driver.manage().addCookie(addedCookie);161 }162 @Test163 public void shouldAllowToDeleteCookiesEvenIfNotSet() {164 WebDriver d = getDriver();165 d.get("https://github.com/");166 // Clear all cookies167 Assert.assertTrue(((d.manage().getCookies().size()) > 0));168 d.manage().deleteAllCookies();169 Assert.assertEquals(d.manage().getCookies().size(), 0);170 // All cookies deleted, call deleteAllCookies again. Should be a no-op.171 d.manage().deleteAllCookies();172 d.manage().deleteCookieNamed("non_existing_cookie");173 Assert.assertEquals(d.manage().getCookies().size(), 0);174 }175 @Test176 public void shouldAllowToSetCookieThatIsAlreadyExpired() {177 WebDriver d = getDriver();178 d.get("https://github.com/");179 // Clear all cookies180 Assert.assertTrue(((d.manage().getCookies().size()) > 0));181 d.manage().deleteAllCookies();182 Assert.assertEquals(d.manage().getCookies().size(), 0);183 // Added cookie that expires in the past184 org.openqa.selenium.Cookie addedCookie = // < now - 1 second185 new org.openqa.selenium.Cookie.Builder("expired", "yes").expiresOn(new Date(((System.currentTimeMillis()) - 1000))).build();186 d.manage().addCookie(addedCookie);187 org.openqa.selenium.Cookie cookie = d.manage().getCookieNamed("expired");188 Assert.assertNull(cookie);189 }190 @Test(expected = Exception.class)191 public void shouldThrowExceptionIfAddingCookieBeforeLoadingAnyUrl() {192 // NOTE: At the time of writing, this test doesn't pass with FirefoxDriver.193 // ChromeDriver is fine instead.194 String xval = "123456789101112";// < detro: I buy you a beer if you guess what am I quoting here195 WebDriver d = getDriver();196 // Set cookie, without opening any page: should throw an exception197 d.manage().addCookie(new org.openqa.selenium.Cookie("x", xval));198 }199 @Test200 public void shouldBeAbleToCreateCookieViaJavascriptOnGoogle() {201 String ckey = "cookiekey";202 String cval = "cookieval";203 WebDriver d = getDriver();204 d.get("http://www.google.com");205 JavascriptExecutor js = ((JavascriptExecutor) (d));206 // Of course, no cookie yet(!)207 org.openqa.selenium.Cookie c = d.manage().getCookieNamed(ckey);208 Assert.assertNull(c);209 // Attempt to create cookie on multiple Google domains210 js.executeScript((((((((((((((((((((((("javascript:(" + (("function() {" + " cook = document.cookie;") + " begin = cook.indexOf('")) + ckey) + "=');") + " var val;") + " if (begin !== -1) {") + " var end = cook.indexOf(\";\",begin);") + " if (end === -1)") + " end=cook.length;") + " val=cook.substring(begin+11,end);") + " }") + " val = ['") + cval) + "'];") + " if (val) {") + " var d=Array('com','co.jp','ca','fr','de','co.uk','it','es','com.br');") + " for (var i = 0; i < d.length; i++) {") + " document.cookie = '") + ckey) + "='+val+';path=/;domain=.google.'+d[i]+'; ';") + " }") + " }") + "})();"));211 c = d.manage().getCookieNamed(ckey);212 Assert.assertNotNull(c);213 Assert.assertEquals(cval, c.getValue());214 // Set cookie as empty215 js.executeScript(((((("javascript:(" + ((("function() {" + " var d = Array('com','co.jp','ca','fr','de','co.uk','it','cn','es','com.br');") + " for(var i = 0; i < d.length; i++) {") + " document.cookie='")) + ckey) + "=;path=/;domain=.google.'+d[i]+'; ';") + " }") + "})();"));216 c = d.manage().getCookieNamed(ckey);217 Assert.assertNotNull(c);218 Assert.assertEquals("", c.getValue());219 }220 @Test221 public void addingACookieWithDefaults() {222 server.setHttpHandler("GET", CookieTest.EMPTY_CALLBACK);223 goToPage();224 long startTime = new Date().getTime();225 driver.manage().addCookie(new org.openqa.selenium.Cookie("newCookie", "newValue"));226 org.openqa.selenium.Cookie[] cookies = getCookies();227 Assert.assertEquals(1, cookies.length);228 Assert.assertEquals("newCookie", cookies[0].getName());229 Assert.assertEquals("newValue", cookies[0].getValue());230 Assert.assertEquals(".localhost", cookies[0].getDomain());231 Assert.assertEquals("/", cookies[0].getPath());232 Assert.assertEquals(false, cookies[0].isSecure());233 Assert.assertEquals(false, cookies[0].isHttpOnly());234 // expiry > 19 years in the future235 Assert.assertTrue(((startTime + 599184000000L) <= (cookies[0].getExpiry().getTime())));236 }237}...
Source:Browser.java
...48 }49 /**50 * @deprecated Use {@link #addCookie(AddCookieParameter)} instead51 */52 public void addCookie(String name, String value, boolean isSecure, boolean isHttpOnly) {53 addCookie(new AddCookieParameter(name, value, isSecure, isHttpOnly));54 }55 public void addCookie(AddCookieParameter parameterObject) {56 Cookie cookie = new Cookie.Builder(parameterObject.name, parameterObject.value)57 .parameterObject.isSecure(parameterObject.isSecure)58 .parameterObject.isHttpOnly(parameterObject.isHttpOnly)59 .build();60 this.driver.manage().addCookie(cookie);61 }62 /**63 * Switches to new browser window for browsing.64 */65 public void switchToNewWindow() {66 String curWin = driver.getWindowHandle();67 for (String handle : driver.getWindowHandles()) {68 if (!handle.equals(curWin) && !windowHandles.contains(curWin)) {69 windowHandles.push(curWin);70 driver.switchTo().window(handle);71 break;...
Source:TecAdminSeleniumTest.java
...89 String val;90 if (!(val = token.nextToken()).equals("null")) {91 expiry = new Date(val);92 }93 final Boolean isSecure = new Boolean(token.nextToken()).booleanValue();94 final Cookie ck = new Cookie(name, value, domain, path, expiry, isSecure);95 System.out.println(ck);96 webDriver.manage().addCookie(ck); // This will add the stored cookie to your current session97 }98 }99100 Buffreader.close();101102 } catch (final Exception ex) {103 ex.printStackTrace();104 }105 }106107 private static void createCookies(WebDriver webDriver) {108 // create file named Cookies to store Login Information109 final File file = new File("Cookies.data");110 try {111 // Delete old file if exists112 file.delete();113 file.createNewFile();114 final FileWriter fileWrite = new FileWriter(file);115 final BufferedWriter Bwrite = new BufferedWriter(fileWrite);116 // loop for getting the cookie information117118 // loop for getting the cookie information119 for (final Cookie ck : webDriver.manage().getCookies()) {120 Bwrite.write(ck.getName() + ";" + ck.getValue() + ";" + ck.getDomain() + ";" + ck.getPath() + ";"121 + ck.getExpiry() + ";" + ck.isSecure());122 Bwrite.newLine();123 }124 Bwrite.close();125 fileWrite.close();126127 } catch (final Exception ex) {128 ex.printStackTrace();129 }130131 }132133 public static void login(final WebDriver driver, final WebDriverWait wait, final String url) {134135 // Get URL
...
Source:WebDriver81CookiesTest.java
...92 Cookie aNewCookie = new Cookie( aCookie.getName(),93 String.valueOf(42),94 aCookie.getDomain(),95 aCookie.getPath(),96 aCookie.getExpiry(),aCookie.isSecure());97 98 driver.manage().deleteCookie(aCookie);99 driver.manage().addCookie(aNewCookie);100 101 searchBox.clear();102 searchBox.sendKeys("New Cookie Test");103 searchButton.click();104 105 aCookie = driver.manage().getCookieNamed("seleniumSimplifiedSearchNumVisits");106 assertEquals("This should be my first visit", "43", aCookie.getValue());107 108 }109 110 @Test111 public void webDriverUpdateCookieBuilder() {112 113 searchBox.clear();114 searchBox.sendKeys("Cookie Test");115 searchButton.click();116 117 refreshSearchPage();118 119 Cookie aCookie = driver.manage().getCookieNamed("seleniumSimplifiedSearchNumVisits");120 assertEquals("This should be my first visit", "1", aCookie.getValue());121 122 // Clone Cookie and set Value to what is wanted123 Cookie aNewCookie = new Cookie.Builder( aCookie.getName(), String.valueOf(29))124 .domain(aCookie.getDomain())125 .path(aCookie.getPath())126 .expiresOn(aCookie.getExpiry())127 .isSecure(aCookie.isSecure()).build();128 129 driver.manage().deleteCookie(aCookie);130 driver.manage().addCookie(aNewCookie);131 132 searchBox.clear();133 searchBox.sendKeys("Another Cookie Test");134 searchButton.click();135 136 aCookie = driver.manage().getCookieNamed("seleniumSimplifiedSearchNumVisits");137 assertEquals("This should be my first visit", "30", aCookie.getValue());138 139 }140 141 @AfterClass
...
Source:TestSeleniumTry.java
...51 +cookie.getValue()+";"52 +cookie.getDomain()+";"53 +cookie.getPath()+";"54 +cookie.getExpiry()+";"55 +cookie.isSecure());56 bufferwriter.newLine();57 }58 bufferwriter.flush();59 bufferwriter.close();60 filewriter.close(); 61 62 }63 catch(Exception ex)64 {65 ex.printStackTrace();66 }67 driver.quit();68 69
...
Source:FacebookLogin.java
...38 // loop for getting the cookie information39 // loop for getting the cookie information40 for (Cookie ck : driver.manage().getCookies()) {41 Bwrite.write((ck.getName() + ";" + ck.getValue() + ";" + ck.getDomain() + ";" + ck.getPath() + ";"42 + ck.getExpiry() + ";" + ck.isSecure()));43 Bwrite.newLine();44 }45 Bwrite.close();46 fileWrite.close();47 } catch (Exception ex) {48 ex.printStackTrace();49 }50 driver.close();51 }52 @Test53 public void writeCookie() {54 WebDriver driver;55 WebDriverManager.firefoxdriver().setup();56// WebDriverManager.chromedriver().setup();57 driver = new FirefoxDriver();58 driver.manage().window().maximize();59 try {60 File file = new File("Cookies_facebook.data");61 FileReader fileReader = new FileReader(file);62 BufferedReader Buffreader = new BufferedReader(fileReader);63 String strline;64 while ((strline = Buffreader.readLine()) != null) {65 StringTokenizer token = new StringTokenizer(strline, ";");66 while (token.hasMoreTokens()) {67 String name = token.nextToken();68 String value = token.nextToken();69 String domain = token.nextToken();70 String path = token.nextToken();71 Date expiry = null;72 String val;73 if (!(val = token.nextToken()).equals("null")) {74 DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");75// Sun Feb 05 11:54:21 IST 202376 expiry = formatter.parse(val);77// expiry = new Date(val);78 }79 Boolean isSecure = new Boolean(token.nextToken()).booleanValue();80 Cookie ck = new Cookie(name, value, domain, path, expiry, isSecure);81 System.out.println(ck);82 driver.get("https://www.facebook.com/");83 driver.manage().addCookie(ck); // This will add the stored cookie to your current session84 }85 }86 } catch (Exception ex) {87 ex.printStackTrace();88 }89 driver.navigate().refresh();90// driver.quit();91 }92}...
Source:selenium.java
...42 new Cookie.Builder(cookie.getName(), "A_SHOW_MINICART")43 .domain(cookie.getDomain())44 .expiresOn(cookie.getExpiry())45 .path(cookie.getPath())46 .isSecure(cookie.isSecure())47 .build()48 );49 50 */51 }52}...
Source:LoadCookieInfo.java
...35 String dt;36 if(!(dt=str.nextToken()).equals("null")){37 expiry = new Date(dt);38 }39 boolean isSecure = new Boolean(str.nextToken()).booleanValue();40 Cookie ck = new Cookie(name,value,domain,path,expiry,isSecure);41 driver.manage().addCookie(ck);42 }43 }44 br.close();45 }catch(Exception ex){46 ex.printStackTrace();47 }48 driver.get("http://www.facebook.com");49 }50 }...
isSecure
Using AI Code Generation
1public class CookieExample {2 public static void main(String[] args) {3 WebDriver driver = new FirefoxDriver();4 Cookie cookie = driver.manage().getCookieNamed("PREF");5 System.out.println("Is Cookie Secure: " + cookie.isSecure());6 driver.quit();7 }8}
isSecure
Using AI Code Generation
1package com.seleniumcookbook.examples.cookies;2import org.openqa.selenium.Cookie;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.firefox.FirefoxDriver;5import org.testng.annotations.AfterMethod;6import org.testng.annotations.BeforeMethod;7import org.testng.annotations.Test;8public class CookieTest {9WebDriver driver;10public void setUp() throws Exception {11driver = new FirefoxDriver();12}13public void tearDown() throws Exception {14driver.quit();15}16public void testCookie() {17Cookie cookie = new Cookie("test", "test", "www.google.com", "/", null);18driver.manage().addCookie(cookie);19Cookie newCookie = driver.manage().getCookieNamed("test");20System.out.println("Cookie is secure: " + newCookie.isSecure());21}22}
isSecure
Using AI Code Generation
1import org.openqa.selenium.Cookie;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.chrome.ChromeDriver;4public class CookieSecure {5 public static void main(String[] args) {6 System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");7 WebDriver driver = new ChromeDriver();8 Cookie cookie = driver.manage().getCookieNamed("NID");9 boolean isSecure = cookie.isSecure();10 System.out.println("Is Cookie Secure? " + isSecure);11 driver.close();12 }13}
isSecure
Using AI Code Generation
1Cookie cookie = driver.manage().getCookieNamed("cookie_name");2boolean isSecure = cookie.isSecure();3System.out.println("Is Cookie Secure? " + isSecure);4Cookie cookie = driver.manage().getCookieNamed("cookie_name");5boolean isHttpOnly = cookie.isHttpOnly();6System.out.println("Is Cookie Http Only? " + isHttpOnly);7Cookie cookie = driver.manage().getCookieNamed("cookie_name");8Date expiryDate = cookie.getExpiry();9System.out.println("Cookie Expiry Date: " + expiryDate);10Cookie cookie = driver.manage().getCookieNamed("cookie_name");11String path = cookie.getPath();12System.out.println("Cookie Path: " + path);13Cookie cookie = driver.manage().getCookieNamed("cookie_name");14String domain = cookie.getDomain();15System.out.println("Cookie Domain: " + domain);16Cookie cookie = driver.manage().getCookieNamed("cookie_name");17String value = cookie.getValue();18System.out.println("Cookie Value: " + value);19Cookie cookie = driver.manage().getCookieNamed("cookie_name");20String name = cookie.getName();21System.out.println("Cookie Name: " + name);22Cookie cookie = driver.manage().getCookieNamed
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.
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.
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.
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.
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.
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.
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.
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.
LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.
Get 100 minutes of automation test minutes FREE!!