Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added java code for cookies #2043

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
185 changes: 98 additions & 87 deletions examples/java/src/test/java/dev/selenium/interactions/CookiesTest.java
Original file line number Diff line number Diff line change
@@ -1,98 +1,109 @@
package dev.selenium.interactions;
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you 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.

import dev.selenium.BaseChromeTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.Set;

public class CookiesTest extends BaseChromeTest {
@Test
public void addCookie() {
try {
driver.get("https://www.selenium.dev/selenium/web/blank.html");
// Add cookie into current browser context
driver.manage().addCookie(new Cookie("key", "value"));
} finally {
driver.quit();
}
}
@Test
public void getNamedCookie() {
try {
driver.get("https://www.selenium.dev/selenium/web/blank.html");
// Add cookie into current browser context
driver.manage().addCookie(new Cookie("foo", "bar"));
// Get cookie details with named cookie 'foo'
Cookie cookie = driver.manage().getCookieNamed("foo");
Assertions.assertEquals(cookie.getValue(), "bar");
} finally {
driver.quit();
}
}
public class CookiesTest {

WebDriver driver = new ChromeDriver();
@Test
public void addCookie() {
driver.get("https://www.selenium.dev/selenium/web/blank.html");
// Add cookie into current browser context
driver.manage().addCookie(new Cookie("key", "value"));
driver.quit();
}
@Test
public void getNamedCookie() {

driver.get("https://www.selenium.dev/selenium/web/blank.html");
// Add cookie into current browser context
driver.manage().addCookie(new Cookie("foo", "bar"));
// Get cookie details with named cookie 'foo'
Cookie cookie = driver.manage().getCookieNamed("foo");
Assertions.assertEquals(cookie.getValue(), "bar");

driver.quit();
}


@Test
public void getAllCookies() {
try {
driver.get("https://www.selenium.dev/selenium/web/blank.html");
// Add cookies into current browser context
driver.manage().addCookie(new Cookie("test1", "cookie1"));
driver.manage().addCookie(new Cookie("test2", "cookie2"));
// Get cookies
Set<Cookie> cookies = driver.manage().getCookies();
for (Cookie cookie : cookies) {
if (cookie.getName().equals("test1")) {
Assertions.assertEquals(cookie.getValue(), "cookie1");
}
@Test
public void getAllCookies() {
driver.get("https://www.selenium.dev/selenium/web/blank.html");
// Add cookies into current browser context
driver.manage().addCookie(new Cookie("test1", "cookie1"));
driver.manage().addCookie(new Cookie("test2", "cookie2"));
// Get cookies
Set<Cookie> cookies = driver.manage().getCookies();
for (Cookie cookie : cookies) {
if (cookie.getName().equals("test1")) {
Assertions.assertEquals(cookie.getValue(), "cookie1");
}

if (cookie.getName().equals("test2")) {
Assertions.assertEquals(cookie.getValue(), "cookie2");
}
}
} finally {
driver.quit();
}
}
if (cookie.getName().equals("test2")) {
Assertions.assertEquals(cookie.getValue(), "cookie2");
}
}
driver.quit();
}


@Test
public void deleteCookieNamed() {
try {
driver.get("https://www.selenium.dev/selenium/web/blank.html");
driver.manage().addCookie(new Cookie("test1", "cookie1"));
// delete cookie named
driver.manage().deleteCookieNamed("test1");
} finally {
driver.quit();
}
}
@Test
public void deleteCookieNamed() {

driver.get("https://www.selenium.dev/selenium/web/blank.html");
driver.manage().addCookie(new Cookie("test1", "cookie1"));
// delete cookie named
driver.manage().deleteCookieNamed("test1");
driver.quit();
}

@Test
public void deleteCookieObject() {
try {
driver.get("https://www.selenium.dev/selenium/web/blank.html");
Cookie cookie = new Cookie("test2", "cookie2");
driver.manage().addCookie(cookie);
/*
Selenium Java bindings also provides a way to delete
cookie by passing cookie object of current browsing context
*/
driver.manage().deleteCookie(cookie);
} finally {
driver.quit();
}
}
@Test
public void deleteCookieObject() {
driver.get("https://www.selenium.dev/selenium/web/blank.html");
Cookie cookie = new Cookie("test2", "cookie2");
driver.manage().addCookie(cookie);
/*
Selenium Java bindings also provides a way to delete
cookie by passing cookie object of current browsing context
*/
driver.manage().deleteCookie(cookie);
driver.quit();
}

@Test
public void deleteAllCookies() {
try {
driver.get("https://www.selenium.dev/selenium/web/blank.html");
// Add cookies into current browser context
driver.manage().addCookie(new Cookie("test1", "cookie1"));
driver.manage().addCookie(new Cookie("test2", "cookie2"));
// Delete All cookies
driver.manage().deleteAllCookies();
} finally {
driver.quit();
}
}
@Test
public void deleteAllCookies() {

driver.get("https://www.selenium.dev/selenium/web/blank.html");
// Add cookies into current browser context
driver.manage().addCookie(new Cookie("test1", "cookie1"));
driver.manage().addCookie(new Cookie("test2", "cookie2"));
// Delete All cookies
driver.manage().deleteAllCookies();

driver.quit();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,8 @@ e.g. http://example.com/some404page)

{{< tabpane langEqualsHeader=true >}}
{{< badge-examples >}}
{{< tab header="Java" >}}
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class addCookie {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
try {
driver.get("http://www.example.com");

// Adds the cookie into current browser context
driver.manage().addCookie(new Cookie("key", "value"));
} finally {
driver.quit();
}
}
}
{{< tab header="Java" text=true >}}
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/CookiesTest.java#L30-L32" >}}
{{< /tab >}}
{{< tab header="Python" >}}
from selenium import webdriver
Expand Down Expand Up @@ -116,25 +101,8 @@ It returns the serialized cookie data matching with the cookie name among all as

{{< tabpane langEqualsHeader=true >}}
{{< badge-examples >}}
{{< tab header="Java" >}}
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class getCookieNamed {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
try {
driver.get("http://www.example.com");
driver.manage().addCookie(new Cookie("foo", "bar"));

// Get cookie details with named cookie 'foo'
Cookie cookie1 = driver.manage().getCookieNamed("foo");
System.out.println(cookie1);
} finally {
driver.quit();
}
}
}
{{< tab header="Java" text=true >}}
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/CookiesTest.java#L38-L42" >}}
{{< /tab >}}
{{< tab header="Python" >}}
from selenium import webdriver
Expand Down Expand Up @@ -217,28 +185,8 @@ If browser is no longer available it returns error.

{{< tabpane langEqualsHeader=true >}}
{{< badge-examples >}}
{{< tab header="Java" >}}
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.Set;

public class getAllCookies {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
try {
driver.get("http://www.example.com");
// Add few cookies
driver.manage().addCookie(new Cookie("test1", "cookie1"));
driver.manage().addCookie(new Cookie("test2", "cookie2"));

// Get All available cookies
Set<Cookie> cookies = driver.manage().getCookies();
System.out.println(cookies);
} finally {
driver.quit();
}
}
}
{{< tab header="Java" text=true >}}
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/CookiesTest.java#L52-L66" >}}
{{< /tab >}}
{{< tab header="Python" >}}
from selenium import webdriver
Expand Down Expand Up @@ -323,32 +271,8 @@ It deletes the cookie data matching with the provided cookie name.

{{< tabpane langEqualsHeader=true >}}
{{< badge-examples >}}
{{< tab header="Java" >}}
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class deleteCookie {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
try {
driver.get("http://www.example.com");
driver.manage().addCookie(new Cookie("test1", "cookie1"));
Cookie cookie1 = new Cookie("test2", "cookie2");
driver.manage().addCookie(cookie1);

// delete a cookie with name 'test1'
driver.manage().deleteCookieNamed("test1");

/*
Selenium Java bindings also provides a way to delete
cookie by passing cookie object of current browsing context
*/
driver.manage().deleteCookie(cookie1);
} finally {
driver.quit();
}
}
}
{{< tab header="Java" text=true >}}
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/CookiesTest.java#L74-L77" >}}
{{< /tab >}}
{{< tab header="Python" >}}
from selenium import webdriver
Expand Down Expand Up @@ -439,25 +363,8 @@ It deletes all the cookies of the current browsing context.

{{< tabpane langEqualsHeader=true >}}
{{< badge-examples >}}
{{< tab header="Java" >}}
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class deleteAllCookies {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
try {
driver.get("http://www.example.com");
driver.manage().addCookie(new Cookie("test1", "cookie1"));
driver.manage().addCookie(new Cookie("test2", "cookie2"));

// deletes all cookies
driver.manage().deleteAllCookies();
} finally {
driver.quit();
}
}
}
{{< tab header="Java" text=true >}}
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/CookiesTest.java#L100-L105" >}}
{{< /tab >}}
{{< tab header="Python" >}}
from selenium import webdriver
Expand Down
Loading
Loading