Skip to content

Commit

Permalink
[py] add code examples for cookies (#2073)
Browse files Browse the repository at this point in the history
[py] add code for cookies

Co-authored-by: Sri Harsha <[email protected]>
  • Loading branch information
Delta456 and harsha509 authored Nov 27, 2024
1 parent 3afb742 commit 96a94db
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 256 deletions.
69 changes: 69 additions & 0 deletions examples/python/tests/interactions/test_cookies.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,71 @@
from selenium import webdriver


def add_cookie():
driver = webdriver.Chrome()
driver.get("http://www.example.com")

# Adds the cookie into current browser context
driver.add_cookie({"name": "key", "value": "value"})


def get_named_cookie():
driver = webdriver.Chrome()
driver.get("http://www.example.com")

# Adds the cookie into current browser context
driver.add_cookie({"name": "foo", "value": "bar"})

# Get cookie details with named cookie 'foo'
print(driver.get_cookie("foo"))


def get_all_cookies():
driver = webdriver.Chrome()

driver.get("http://www.example.com")

driver.add_cookie({"name": "test1", "value": "cookie1"})
driver.add_cookie({"name": "test2", "value": "cookie2"})

# Get all available cookies
print(driver.get_cookies())

def delete_cookie():
driver = webdriver.Chrome()

driver.get("http://www.example.com")

driver.add_cookie({"name": "test1", "value": "cookie1"})
driver.add_cookie({"name": "test2", "value": "cookie2"})

# Delete cookie with name 'test1'
driver.delete_cookie("test1")


def delete_all_cookies():
driver = webdriver.Chrome()

driver.get("http://www.example.com")

driver.add_cookie({"name": "test1", "value": "cookie1"})
driver.add_cookie({"name": "test2", "value": "cookie2"})

# Delete all cookies
driver.delete_all_cookies()


def same_side_cookie_attr():
driver = webdriver.Chrome()

driver.get("http://www.example.com")

# Adds the cookie into current browser context with sameSite 'Strict' (or) 'Lax'
driver.add_cookie({"name": "foo", "value": "value", "sameSite": "Strict"})
driver.add_cookie({"name": "foo1", "value": "value", "sameSite": "Lax"})

cookie1 = driver.get_cookie("foo")
cookie2 = driver.get_cookie("foo1")

print(cookie1)
print(cookie2)
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,7 @@ e.g. http://example.com/some404page)
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/CookiesTest.java#L30-L32" >}}
{{< /tab >}}
{{< tab header="Python" >}}
from selenium import webdriver

driver = webdriver.Chrome()

driver.get("http://www.example.com")

# Adds the cookie into current browser context
driver.add_cookie({"name": "key", "value": "value"})
{{< gh-codeblock path="examples/python/tests/interactions/test_cookies.py#L5-L9" >}}
{{< /tab >}}
{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/CookiesTest.cs#L32-L34" >}}
Expand Down Expand Up @@ -87,18 +80,7 @@ It returns the serialized cookie data matching with the cookie name among all as
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/CookiesTest.java#L38-L42" >}}
{{< /tab >}}
{{< tab header="Python" >}}
from selenium import webdriver

driver = webdriver.Chrome()

# Navigate to url
driver.get("http://www.example.com")

# Adds the cookie into current browser context
driver.add_cookie({"name": "foo", "value": "bar"})

# Get cookie details with named cookie 'foo'
print(driver.get_cookie("foo"))
{{< gh-codeblock path="examples/python/tests/interactions/test_cookies.py#L13-L20" >}}
{{< /tab >}}
{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/CookiesTest.cs#L40-L44" >}}
Expand Down Expand Up @@ -151,18 +133,7 @@ If browser is no longer available it returns error.
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/CookiesTest.java#L52-L66" >}}
{{< /tab >}}
{{< tab header="Python" >}}
from selenium import webdriver

driver = webdriver.Chrome()

# Navigate to url
driver.get("http://www.example.com")

driver.add_cookie({"name": "test1", "value": "cookie1"})
driver.add_cookie({"name": "test2", "value": "cookie2"})

# Get all available cookies
print(driver.get_cookies())
{{< gh-codeblock path="examples/python/tests/interactions/test_cookies.py#L24-L32" >}}
{{< /tab >}}
{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/CookiesTest.cs#L51-L64" >}}
Expand Down Expand Up @@ -217,16 +188,7 @@ It deletes the cookie data matching with the provided cookie name.
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/CookiesTest.java#L74-L77" >}}
{{< /tab >}}
{{< tab header="Python" >}}
from selenium import webdriver
driver = webdriver.Chrome()

# Navigate to url
driver.get("http://www.example.com")
driver.add_cookie({"name": "test1", "value": "cookie1"})
driver.add_cookie({"name": "test2", "value": "cookie2"})

# Delete a cookie with name 'test1'
driver.delete_cookie("test1")
{{< gh-codeblock path="examples/python/tests/interactions/test_cookies.py#L35-L43" >}}
{{< /tab >}}
{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/CookiesTest.cs#L70-L73" >}}
Expand Down Expand Up @@ -284,16 +246,7 @@ It deletes all the cookies of the current browsing context.
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/CookiesTest.java#L100-L105" >}}
{{< /tab >}}
{{< tab header="Python" >}}
from selenium import webdriver
driver = webdriver.Chrome()

# Navigate to url
driver.get("http://www.example.com")
driver.add_cookie({"name": "test1", "value": "cookie1"})
driver.add_cookie({"name": "test2", "value": "cookie2"})

# Deletes all cookies
driver.delete_all_cookies()
{{< gh-codeblock path="examples/python/tests/interactions/test_cookies.py#L47-L55" >}}
{{< /tab >}}
{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/CookiesTest.cs#L92-L97" >}}
Expand Down Expand Up @@ -381,18 +334,7 @@ public class cookieTest {
}
{{< /tab >}}
{{< tab header="Python" >}}
from selenium import webdriver

driver = webdriver.Chrome()

driver.get("http://www.example.com")
# Adds the cookie into current browser context with sameSite 'Strict' (or) 'Lax'
driver.add_cookie({"name": "foo", "value": "value", 'sameSite': 'Strict'})
driver.add_cookie({"name": "foo1", "value": "value", 'sameSite': 'Lax'})
cookie1 = driver.get_cookie('foo')
cookie2 = driver.get_cookie('foo1')
print(cookie1)
print(cookie2)
{{< gh-codeblock path="examples/python/tests/interactions/test_cookies.py#L59-L71" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
using OpenQA.Selenium;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,7 @@ Cookieの追加では、一連の定義済みのシリアル化可能なJSONオ
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/CookiesTest.java#L30-L32" >}}
{{< /tab >}}
{{< tab header="Python" >}}
from selenium import webdriver

driver = webdriver.Chrome()

driver.get("http://www.example.com")

# Adds the cookie into current browser context
driver.add_cookie({"name": "key", "value": "value"})
{{< gh-codeblock path="examples/python/tests/interactions/test_cookies.py#L5-L9" >}}
{{< /tab >}}
{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/CookiesTest.cs#L32-L34" >}}
Expand Down Expand Up @@ -83,18 +76,7 @@ fun main() {
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/CookiesTest.java#L38-L42" >}}
{{< /tab >}}
{{< tab header="Python" >}}
from selenium import webdriver

driver = webdriver.Chrome()

# Navigate to url
driver.get("http://www.example.com")

# Adds the cookie into current browser context
driver.add_cookie({"name": "foo", "value": "bar"})

# Get cookie details with named cookie 'foo'
print(driver.get_cookie("foo"))
{{< gh-codeblock path="examples/python/tests/interactions/test_cookies.py#L13-L20" >}}
{{< /tab >}}
{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/CookiesTest.cs#L40-L44" >}}
Expand Down Expand Up @@ -146,18 +128,7 @@ fun main() {
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/CookiesTest.java#L52-L66" >}}
{{< /tab >}}
{{< tab header="Python" >}}
from selenium import webdriver

driver = webdriver.Chrome()

# Navigate to url
driver.get("http://www.example.com")

driver.add_cookie({"name": "test1", "value": "cookie1"})
driver.add_cookie({"name": "test2", "value": "cookie2"})

# Get all available cookies
print(driver.get_cookies())
{{< gh-codeblock path="examples/python/tests/interactions/test_cookies.py#L24-L32" >}}
{{< /tab >}}
{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/CookiesTest.cs#L51-L64" >}}
Expand Down Expand Up @@ -211,16 +182,7 @@ fun main() {
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/CookiesTest.java#L74-L77" >}}
{{< /tab >}}
{{< tab header="Python" >}}
from selenium import webdriver
driver = webdriver.Chrome()

# Navigate to url
driver.get("http://www.example.com")
driver.add_cookie({"name": "test1", "value": "cookie1"})
driver.add_cookie({"name": "test2", "value": "cookie2"})

# Delete a cookie with name 'test1'
driver.delete_cookie("test1")
{{< gh-codeblock path="examples/python/tests/interactions/test_cookies.py#L35-L43" >}}
{{< /tab >}}
{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/CookiesTest.cs#L70-L73" >}}
Expand Down Expand Up @@ -277,16 +239,7 @@ fun main() {
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/CookiesTest.java#L100-L105" >}}
{{< /tab >}}
{{< tab header="Python" >}}
from selenium import webdriver
driver = webdriver.Chrome()

# Navigate to url
driver.get("http://www.example.com")
driver.add_cookie({"name": "test1", "value": "cookie1"})
driver.add_cookie({"name": "test2", "value": "cookie2"})

# Deletes all cookies
driver.delete_all_cookies()
{{< gh-codeblock path="examples/python/tests/interactions/test_cookies.py#L47-L55" >}}
{{< /tab >}}
{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/CookiesTest.cs#L92-L97" >}}
Expand Down Expand Up @@ -371,18 +324,7 @@ public class cookieTest {
}
{{< /tab >}}
{{< tab header="Python" >}}
from selenium import webdriver

driver = webdriver.Chrome()

driver.get("http://www.example.com")
# Adds the cookie into current browser context with sameSite 'Strict' (or) 'Lax'
driver.add_cookie({"name": "foo", "value": "value", 'sameSite': 'Strict'})
driver.add_cookie({"name": "foo1", "value": "value", 'sameSite': 'Lax'})
cookie1 = driver.get_cookie('foo')
cookie2 = driver.get_cookie('foo1')
print(cookie1)
print(cookie2)
{{< gh-codeblock path="examples/python/tests/interactions/test_cookies.py#L59-L71" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
using OpenQA.Selenium;
Expand Down
Loading

0 comments on commit 96a94db

Please sign in to comment.