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

Conversation

pallavigitwork
Copy link
Member

@pallavigitwork pallavigitwork commented Nov 5, 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

added java code for cookies

Motivation and Context

moving code to test

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

Tests, Documentation


Description

  • Refactored the Java test class CookiesTest to include a license header and manage the WebDriver instance lifecycle within each test method.
  • Removed inheritance from BaseChromeTest and initialized WebDriver directly in the test class.
  • Updated documentation across multiple languages (English, Japanese, Portuguese, Chinese) to replace inline Java code examples with references to the Java test file, improving maintainability and consistency.

Changes walkthrough 📝

Relevant files
Tests
CookiesTest.java
Refactor and enhance Java cookie tests with license header

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

  • Added Apache License header.
  • Introduced WebDriver instance initialization within the test class.
  • Removed BaseChromeTest inheritance and managed driver lifecycle within
    each test.
  • Reformatted test methods for better readability.
  • +98/-87 
    Documentation
    cookies.en.md
    Update Java code examples to reference test file                 

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

  • Replaced inline Java code examples with references to test file.
  • Updated code block references for Java examples.
  • +10/-103
    cookies.ja.md
    Update Java code examples to reference test file (Japanese)

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

  • Replaced inline Java code examples with references to test file.
  • Updated code block references for Java examples.
  • +10/-103
    cookies.pt-br.md
    Update Java code examples to reference test file (Portuguese)

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

  • Replaced inline Java code examples with references to test file.
  • Updated code block references for Java examples.
  • +10/-103
    cookies.zh-cn.md
    Update Java code examples to reference test file (Chinese)

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

  • Replaced inline Java code examples with references to test file.
  • Updated code block references for Java examples.
  • +10/-103

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

    Copy link

    netlify bot commented Nov 5, 2024

    👷 Deploy request for selenium-dev pending review.

    Visit the deploys page to approve it

    Name Link
    🔨 Latest commit 0650803

    Copy link
    Contributor

    qodo-merge-pro bot commented Nov 5, 2024

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

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

    Resource Management
    The WebDriver instance is created for each test method but not properly closed. Consider using @beforeeach and @AfterEach annotations to manage the WebDriver lifecycle.

    Test Isolation
    The WebDriver instance is shared across all test methods, which may lead to test interdependence. Each test should have its own WebDriver instance to ensure proper isolation.

    Error Handling
    There's no explicit error handling or assertions in the test methods. Consider adding appropriate assertions and error handling to improve test robustness.

    Copy link
    Contributor

    qodo-merge-pro bot commented Nov 5, 2024

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Enhancement
    Add assertions to verify cookie addition and value

    Add assertions to verify that the cookie was successfully added and has the expected
    value.

    examples/java/src/test/java/dev/selenium/interactions/CookiesTest.java [28-33]

     @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();
    +    
    +    // Verify the cookie was added successfully
    +    Cookie addedCookie = driver.manage().getCookieNamed("key");
    +    Assertions.assertNotNull(addedCookie, "Cookie should not be null");
    +    Assertions.assertEquals("value", addedCookie.getValue(), "Cookie value should match");
     }
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    Why: Adding assertions to verify the cookie's presence and value significantly improves the test's effectiveness by ensuring the addCookie operation works as expected.

    9
    Best practice
    Implement proper test setup and teardown methods for WebDriver management

    Move the WebDriver initialization to a @beforeeach method and close it in an
    @AfterEach method to avoid redundant code and ensure proper setup and teardown for
    each test.

    examples/java/src/test/java/dev/selenium/interactions/CookiesTest.java [27-34]

    -WebDriver driver = new ChromeDriver();
    +private WebDriver driver;
    +
    +@BeforeEach
    +public void setUp() {
    +    driver = new ChromeDriver();
    +}
    +
    +@AfterEach
    +public void tearDown() {
    +    if (driver != null) {
    +        driver.quit();
    +    }
    +}
    +
     @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();
     }
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Using @beforeeach and @AfterEach methods for WebDriver setup and teardown improves code organization, reduces duplication, and ensures consistent test environment across all test methods.

    8
    Implement try-with-resources for automatic WebDriver closure

    Use try-with-resources to automatically close the WebDriver after each test,
    ensuring proper resource management.

    examples/java/src/test/java/dev/selenium/interactions/CookiesTest.java [27-34]

    -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();
    +    try (WebDriver driver = new ChromeDriver()) {
    +        driver.get("https://www.selenium.dev/selenium/web/blank.html");
    +        // Add cookie into current browser context
    +        driver.manage().addCookie(new Cookie("key", "value"));
    +    }
     }
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Using try-with-resources ensures proper resource management and automatic closure of the WebDriver, reducing the risk of resource leaks and improving code reliability.

    7
    Use a constant for frequently used URLs to improve maintainability

    Use a constant for the URL to avoid duplication and make it easier to update if
    needed.

    examples/java/src/test/java/dev/selenium/interactions/CookiesTest.java [30]

    +private static final String BLANK_PAGE_URL = "https://www.selenium.dev/selenium/web/blank.html";
    +
     @Test
     public void addCookie() {
    -    driver.get("https://www.selenium.dev/selenium/web/blank.html");
    +    driver.get(BLANK_PAGE_URL);
         // Add cookie into current browser context
         driver.manage().addCookie(new Cookie("key", "value"));
    -    driver.quit();
     }
    • Apply this suggestion
    Suggestion importance[1-10]: 5

    Why: Using a constant for the URL improves code maintainability and reduces the risk of typos, but has a relatively minor impact on the overall functionality of the tests.

    5

    💡 Need additional feedback ? start a PR chat

    @harsha509 harsha509 merged commit 33d821a into SeleniumHQ:trunk Nov 5, 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