Skip to content

[java] move samesite cookie code to file #2132

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

Merged
merged 2 commits into from
Jan 21, 2025

Conversation

Delta456
Copy link
Member

@Delta456 Delta456 commented Jan 16, 2025

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

Moved samesite cookie code to file

Motivation and Context

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 test for SameSite cookie handling in Java.

  • Moved SameSite cookie example code to a dedicated file.

  • Updated documentation to reference the new example file.

  • Simplified Java code snippets in multiple language-specific docs.


Changes walkthrough 📝

Relevant files
Tests
CookiesTest.java
Add SameSite cookie handling test in Java                               

examples/java/src/test/java/dev/selenium/interactions/CookiesTest.java

  • Added a new test method sameSiteCookie.
  • Demonstrated usage of SameSite attribute for cookies.
  • Included examples for "Strict" and "Lax" SameSite policies.
  • +16/-0   
    Documentation
    cookies.en.md
    Update English docs to reference SameSite cookie example 

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

  • Replaced inline Java example with a reference to the new file.
  • Linked to the SameSite cookie example in the Java test file.
  • +2/-20   
    cookies.ja.md
    Update Japanese docs to reference SameSite cookie example

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

  • Replaced inline Java example with a reference to the new file.
  • Linked to the SameSite cookie example in the Java test file.
  • +2/-20   
    cookies.pt-br.md
    Update Portuguese docs to reference SameSite cookie example

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

  • Replaced inline Java example with a reference to the new file.
  • Linked to the SameSite cookie example in the Java test file.
  • +2/-20   
    cookies.zh-cn.md
    Update Chinese docs to reference SameSite cookie example 

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

  • Replaced inline Java example with a reference to the new file.
  • Linked to the SameSite cookie example in the Java test file.
  • +2/-20   

    Need help?
  • Type /help how to ... in the comments thread for any question about Qodo Merge usage.
  • Check out the documentation for more information.
  • Copy link

    netlify bot commented Jan 16, 2025

    👷 Deploy request for selenium-dev pending review.

    Visit the deploys page to approve it

    Name Link
    🔨 Latest commit 321ac1c

    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 Leak

    The test method calls driver.quit() directly without using try-finally block, which could leave resources unclosed if an exception occurs during test execution

    		  public void sameSiteCookie() {
    		    driver.get("http://www.example.com");
    
         	    Cookie cookie = new Cookie.Builder("key", "value").sameSite("Strict").build();
                Cookie cookie1 = new Cookie.Builder("key", "value").sameSite("Lax").build();
    
                driver.manage().addCookie(cookie);
                driver.manage().addCookie(cookie1);
    
                System.out.println(cookie.getSameSite());
                System.out.println(cookie1.getSameSite());
    
    			driver.quit();
    		  }
    Test Validation

    The test method does not include any assertions to validate that the cookies were actually set with correct SameSite attributes

    		  public void sameSiteCookie() {
    		    driver.get("http://www.example.com");
    
         	    Cookie cookie = new Cookie.Builder("key", "value").sameSite("Strict").build();
                Cookie cookie1 = new Cookie.Builder("key", "value").sameSite("Lax").build();
    
                driver.manage().addCookie(cookie);
                driver.manage().addCookie(cookie1);
    
                System.out.println(cookie.getSameSite());
                System.out.println(cookie1.getSameSite());
    
    			driver.quit();
    		  }

    Copy link
    Contributor

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    General
    Replace debug print statements with proper test assertions for validation

    Replace System.out.println with proper test assertions to validate the SameSite
    attributes.

    examples/java/src/test/java/dev/selenium/interactions/CookiesTest.java [120-121]

    -System.out.println(cookie.getSameSite());
    -System.out.println(cookie1.getSameSite());
    +assertEquals("Strict", cookie.getSameSite());
    +assertEquals("Lax", cookie1.getSameSite());
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    Why: Converting print statements to assertions is essential for proper test validation, as it transforms a manual inspection into automated verification. This significantly improves the test's reliability and maintainability.

    9
    Ensure proper resource cleanup by wrapping WebDriver operations in a try-finally block

    Add try-finally block to ensure driver cleanup even if test fails, consistent with
    other test methods in the class.

    examples/java/src/test/java/dev/selenium/interactions/CookiesTest.java [111-124]

     @Test
     public void sameSiteCookie() {
    -  driver.get("http://www.example.com");
    -  Cookie cookie = new Cookie.Builder("key", "value").sameSite("Strict").build();
    -  Cookie cookie1 = new Cookie.Builder("key", "value").sameSite("Lax").build();
    -  driver.manage().addCookie(cookie);
    -  driver.manage().addCookie(cookie1);
    -  System.out.println(cookie.getSameSite());
    -  System.out.println(cookie1.getSameSite());
    -  driver.quit();
    +  try {
    +    driver.get("http://www.example.com");
    +    Cookie cookie = new Cookie.Builder("key", "value").sameSite("Strict").build();
    +    Cookie cookie1 = new Cookie.Builder("key", "value").sameSite("Lax").build();
    +    driver.manage().addCookie(cookie);
    +    driver.manage().addCookie(cookie1);
    +    System.out.println(cookie.getSameSite());
    +    System.out.println(cookie1.getSameSite());
    +  } finally {
    +    driver.quit();
    +  }
     }
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Adding a try-finally block is crucial for ensuring proper resource cleanup, especially for WebDriver instances, preventing resource leaks even if the test fails. This is a significant improvement for test reliability.

    8

    Copy link
    Member

    @harsha509 harsha509 left a comment

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    LGTM!

    @harsha509 harsha509 merged commit 17c6ff9 into SeleniumHQ:trunk Jan 21, 2025
    8 of 9 checks passed
    selenium-ci added a commit that referenced this pull request Jan 21, 2025
    @Delta456 Delta456 deleted the java_cookie branch March 19, 2025 10:54
    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