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

[py] add code examples for cookies #2073

Merged
merged 2 commits into from
Nov 27, 2024
Merged

Conversation

Delta456
Copy link
Contributor

@Delta456 Delta456 commented Nov 24, 2024

User description

Thanks for contributing to the Selenium site and documentation!
A PR well described will help maintainers to review and merge it quickly

Before submitting your PR, please check our contributing guidelines.
Avoid large PRs, and help reviewers by making them as simple and short as possible.

Description

Moves code example for cookie to a file

Motivation and Context

It is easier to read and also navigate

Types of changes

  • Change to the site (I have double-checked the Netlify deployment, and my changes look good)
  • Code example added (and I also added the example to all translated languages)
  • Improved translation
  • Added new translation (and I also added a notice to each document missing translation)

Checklist

  • I have read the contributing document.
  • I have used hugo to render the site/docs locally and I am sure it works.

PR Type

enhancement, documentation


Description

  • Added a new Python test file test_cookies.py with functions demonstrating cookie interactions such as adding, retrieving, and deleting cookies.
  • Updated documentation in multiple languages (English, Japanese, Portuguese, Chinese) to replace inline Python code with references to the new example file.
  • Enhanced documentation readability and maintainability by centralizing code examples.

Changes walkthrough 📝

Relevant files
Enhancement
test_cookies.py
Add Python examples for cookie interactions                           

examples/python/tests/interactions/test_cookies.py

  • Added multiple functions to demonstrate cookie interactions.
  • Functions include adding, retrieving, and deleting cookies.
  • Demonstrated use of sameSite cookie attribute.
  • +69/-0   
    Documentation
    cookies.en.md
    Update English documentation with Python code references 

    website_and_docs/content/documentation/webdriver/interactions/cookies.en.md

  • Replaced inline Python code with references to example file.
  • Updated code block references for cookie interactions.
  • +6/-64   
    cookies.ja.md
    Update Japanese documentation with Python code references

    website_and_docs/content/documentation/webdriver/interactions/cookies.ja.md

  • Replaced inline Python code with references to example file.
  • Updated code block references for cookie interactions.
  • +6/-64   
    cookies.pt-br.md
    Update Portuguese documentation with Python code references

    website_and_docs/content/documentation/webdriver/interactions/cookies.pt-br.md

  • Replaced inline Python code with references to example file.
  • Updated code block references for cookie interactions.
  • +6/-64   
    cookies.zh-cn.md
    Update Chinese documentation with Python code references 

    website_and_docs/content/documentation/webdriver/interactions/cookies.zh-cn.md

  • Replaced inline Python code with references to example file.
  • Updated code block references for cookie interactions.
  • +6/-64   

    💡 PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    Copy link

    netlify bot commented Nov 24, 2024

    👷 Deploy request for selenium-dev pending review.

    Visit the deploys page to approve it

    Name Link
    🔨 Latest commit c9727de

    Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Resource Management
    WebDriver instances are not properly closed after use. This could lead to resource leaks and browser processes remaining open.

    Error Handling
    No error handling or validation for cookie operations that could fail, such as getting non-existent cookies or network issues

    Copy link
    Contributor

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    General
    Ensure proper cleanup of browser resources by closing WebDriver instances after use

    Close the WebDriver instance after each test function to prevent resource leaks and
    ensure proper cleanup

    examples/python/tests/interactions/test_cookies.py [4-9]

     def add_cookie():
         driver = webdriver.Chrome()
    -    driver.get("http://www.example.com")
    -    driver.add_cookie({"name": "key", "value": "value"})
    +    try:
    +        driver.get("http://www.example.com")
    +        driver.add_cookie({"name": "key", "value": "value"})
    +    finally:
    +        driver.quit()
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    Why: Not closing WebDriver instances can lead to memory leaks and browser processes remaining open. Adding proper cleanup with try/finally ensures resources are always released.

    9
    Handle potential failures in cookie operations to improve code robustness

    Add error handling for cookie operations to handle cases where the cookie cannot be
    added or retrieved

    examples/python/tests/interactions/test_cookies.py [12-20]

     def get_named_cookie():
         driver = webdriver.Chrome()
    -    driver.get("http://www.example.com")
    -    driver.add_cookie({"name": "foo", "value": "bar"})
    -    print(driver.get_cookie("foo"))
    +    try:
    +        driver.get("http://www.example.com")
    +        driver.add_cookie({"name": "foo", "value": "bar"})
    +        cookie = driver.get_cookie("foo")
    +        if cookie is None:
    +            print("Cookie 'foo' not found")
    +        else:
    +            print(cookie)
    +    finally:
    +        driver.quit()
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Adding error handling for cookie operations makes the code more robust by gracefully handling cases where cookies cannot be added or retrieved, preventing potential crashes.

    7

    💡 Need additional feedback ? start a PR chat

    @harsha509 harsha509 merged commit 96a94db into SeleniumHQ:trunk Nov 27, 2024
    9 checks passed
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    2 participants