From 06508037a13e618ca956726b57925d3e5a796178 Mon Sep 17 00:00:00 2001 From: Pallavi Date: Tue, 5 Nov 2024 18:56:41 +0530 Subject: [PATCH] added java code for cookies --- .../selenium/interactions/CookiesTest.java | 185 ++++++++++-------- .../webdriver/interactions/cookies.en.md | 113 +---------- .../webdriver/interactions/cookies.ja.md | 113 +---------- .../webdriver/interactions/cookies.pt-br.md | 113 +---------- .../webdriver/interactions/cookies.zh-cn.md | 113 +---------- 5 files changed, 138 insertions(+), 499 deletions(-) diff --git a/examples/java/src/test/java/dev/selenium/interactions/CookiesTest.java b/examples/java/src/test/java/dev/selenium/interactions/CookiesTest.java index c8dfca72a7c2..9d07903ceef8 100644 --- a/examples/java/src/test/java/dev/selenium/interactions/CookiesTest.java +++ b/examples/java/src/test/java/dev/selenium/interactions/CookiesTest.java @@ -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 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 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(); + } } diff --git a/website_and_docs/content/documentation/webdriver/interactions/cookies.en.md b/website_and_docs/content/documentation/webdriver/interactions/cookies.en.md index bb9bc511a65c..febd84a7f859 100644 --- a/website_and_docs/content/documentation/webdriver/interactions/cookies.en.md +++ b/website_and_docs/content/documentation/webdriver/interactions/cookies.en.md @@ -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 @@ -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 @@ -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 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 @@ -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 @@ -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 diff --git a/website_and_docs/content/documentation/webdriver/interactions/cookies.ja.md b/website_and_docs/content/documentation/webdriver/interactions/cookies.ja.md index 9087c05e10e4..8045d4b258d3 100644 --- a/website_and_docs/content/documentation/webdriver/interactions/cookies.ja.md +++ b/website_and_docs/content/documentation/webdriver/interactions/cookies.ja.md @@ -24,23 +24,8 @@ Cookieの追加では、一連の定義済みのシリアル化可能なJSONオ サイトとの対話を開始する前にCookieを事前設定しようとしていて、ホームページが大きい場合/代替の読み込みに時間がかかる場合は、サイトで小さいページを見つけることです。(通常、たとえば http://example.com/some404page のような、404ページは小さいです。) {{< tabpane langEqualsHeader=true >}} - {{< 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 @@ -112,25 +97,8 @@ fun main() { 関連付けられているすべてのCookieの中で、Cookie名と一致するシリアル化されたCookieデータを返します。 {{< tabpane langEqualsHeader=true >}} - {{< 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 @@ -212,28 +180,8 @@ fun main() { ブラウザが使用できなくなった場合、エラーが返されます。 {{< tabpane langEqualsHeader=true >}} - {{< 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 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 @@ -317,32 +265,8 @@ fun main() { 指定されたCookie名と一致するCookieデータを削除します。 {{< tabpane langEqualsHeader=true >}} - {{< 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 @@ -432,25 +356,8 @@ fun main() { 現在のブラウジングコンテキストの全てのCookieを削除します。 {{< tabpane langEqualsHeader=true >}} - {{< 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 diff --git a/website_and_docs/content/documentation/webdriver/interactions/cookies.pt-br.md b/website_and_docs/content/documentation/webdriver/interactions/cookies.pt-br.md index 81c16683e632..abe55b6fda61 100644 --- a/website_and_docs/content/documentation/webdriver/interactions/cookies.pt-br.md +++ b/website_and_docs/content/documentation/webdriver/interactions/cookies.pt-br.md @@ -26,23 +26,8 @@ uma alternativa é encontrar uma página menor no site (normalmente a página 40 por exemplo http://example.com/some404page) {{< tabpane langEqualsHeader=true >}} - {{< 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 @@ -114,25 +99,8 @@ fun main() { Retorna os dados do cookie serializado correspondentes ao nome do cookie entre todos os cookies associados. {{< tabpane langEqualsHeader=true >}} - {{< 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 @@ -214,28 +182,8 @@ Retorna 'dados de cookie serializados com sucesso' para o contexto de navegaçã Se o navegador não estiver mais disponível, ele retornará um erro. {{< tabpane langEqualsHeader=true >}} - {{< 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 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 @@ -319,32 +267,8 @@ fun main() { Exclui os dados do cookie que correspondem ao nome do cookie fornecido. {{< tabpane langEqualsHeader=true >}} - {{< 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 @@ -434,25 +358,8 @@ fun main() { Exclui todos os cookies do contexto de navegação atual. {{< tabpane langEqualsHeader=true >}} - {{< 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 diff --git a/website_and_docs/content/documentation/webdriver/interactions/cookies.zh-cn.md b/website_and_docs/content/documentation/webdriver/interactions/cookies.zh-cn.md index c1c238959167..aa820280b589 100644 --- a/website_and_docs/content/documentation/webdriver/interactions/cookies.zh-cn.md +++ b/website_and_docs/content/documentation/webdriver/interactions/cookies.zh-cn.md @@ -22,23 +22,8 @@ WebDriver API提供了一种使用内置的方法与Cookie进行交互: 例如 http://example.com/some404page) {{< tabpane langEqualsHeader=true >}} - {{< 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 @@ -110,25 +95,8 @@ fun main() { 此方法返回与cookie名称匹配的序列化cookie数据中所有关联的cookie. {{< tabpane langEqualsHeader=true >}} - {{< 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 @@ -209,28 +177,8 @@ fun main() { 此方法会针对当前访问上下文返回“成功的序列化cookie数据”. 如果浏览器不再可用, 则返回错误. {{< tabpane langEqualsHeader=true >}} - {{< 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 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 @@ -314,32 +262,8 @@ fun main() { 此方法删除与提供的cookie名称匹配的cookie数据. {{< tabpane langEqualsHeader=true >}} - {{< 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 @@ -429,25 +353,8 @@ fun main() { 此方法删除当前访问上下文的所有cookie. {{< tabpane langEqualsHeader=true >}} - {{< 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