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

petesong/Add a Python example for the test practice #1924

Open
wants to merge 18 commits into
base: trunk
Choose a base branch
from

Conversation

PeteSong
Copy link

@PeteSong PeteSong commented Sep 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

  1. Correct the right link of Hugo Installation
  2. Add a Python example for the test practices.

Motivation and Context

For the test practices, there's no python example.

Types of changes

  • Change to the site
  • 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 server to render the site/docs locally, but failed. Since I only have minor changes in the existing pages, and I am sure it works.

PR Type

Documentation, Tests


Description

  • Updated the Hugo installation link in the README to point directly to the official Hugo site, improving clarity and accessibility.
  • Added a detailed Python example using pytest and Selenium to the test practices documentation. This example demonstrates best practices with the "ActionBot" and "LoadableComponent" patterns.
  • The example includes multiple test cases for interacting with a Todo application, showcasing various functionalities such as adding, toggling, and deleting todos.

Changes walkthrough 📝

Relevant files
Documentation
README.md
Update Hugo installation link and instructions                     

README.md

  • Updated the Hugo installation link to a more direct source.
  • Provided clearer instructions for getting started with Hugo.
  • +1/-1     
    Tests
    design_strategies.en.md
    Add Python example for test practices with Selenium           

    website_and_docs/content/documentation/test_practices/design_strategies.en.md

  • Added a comprehensive Python example using pytest and Selenium.
  • Demonstrated best practices with "ActionBot" and "LoadableComponent".
  • Included multiple test cases for a Todo application.
  • +280/-0 

    💡 PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    Use `python + pytest + selenium` to test the Todo page with the best test practices: "ActionBot" and "LoadableComponent"
    Copy link

    netlify bot commented Sep 5, 2024

    👷 Deploy request for selenium-dev pending review.

    Visit the deploys page to approve it

    Name Link
    🔨 Latest commit 3200342

    @CLAassistant
    Copy link

    CLAassistant commented Sep 5, 2024

    CLA assistant check
    All committers have signed the CLA.

    @codiumai-pr-agent-pro codiumai-pr-agent-pro bot added documentation Improvements or additions to documentation tests labels Sep 5, 2024
    Copy link
    Contributor

    PR Reviewer Guide 🔍

    ⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Key issues to review

    Code Complexity
    The added Python example is quite long and complex. Consider breaking it down into smaller, more manageable sections or separate files for better readability and maintainability.

    Error Handling
    The is_loaded method in the TodoPage class uses a bare except clause, which might catch and suppress unexpected errors. Consider using a more specific exception or at least logging the error.

    Commented Code
    There is commented out code in the hover method of the ActionBot class. Consider removing it if it's not needed or add a comment explaining why it's kept.

    Copy link
    Contributor

    codiumai-pr-agent-pro bot commented Sep 5, 2024

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Best practice
    ✅ Use a context manager for WebDriver to ensure proper resource cleanup
    Suggestion Impact:The suggestion to use a context manager for the WebDriver was implemented, ensuring proper resource cleanup.

    code diff:

    +    with webdriver.Chrome() as driver:
    +        driver.set_window_size(1024, 768)
    +        driver.implicitly_wait(0.5)
    +        yield driver

    Consider using a context manager for the WebDriver to ensure proper resource
    cleanup, even if an exception occurs during the test execution.

    website_and_docs/content/documentation/test_practices/design_strategies.en.md [399-404]

     @pytest.fixture(scope="function")
     def chrome_driver():
    -    driver = webdriver.Chrome()
    -    driver.set_window_size(1024, 768)
    -    driver.implicitly_wait(0.5)
    -    yield driver
    -    driver.quit()
    +    with webdriver.Chrome() as driver:
    +        driver.set_window_size(1024, 768)
    +        driver.implicitly_wait(0.5)
    +        yield driver
     
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    Why: This suggestion is a best practice for resource management, ensuring that the WebDriver is properly closed even if an exception occurs, which is crucial for preventing resource leaks.

    9
    Use specific exception handling instead of a bare except clause

    In the is_loaded method, consider using a more specific exception catch instead of a
    bare except clause to handle potential errors more gracefully and avoid masking
    unexpected exceptions.

    website_and_docs/content/documentation/test_practices/design_strategies.en.md [516-523]

     def is_loaded(self):
         try:
             WebDriverWait(self.driver, 10).until(
                 EC.visibility_of_element_located(self.new_todo_by)
             )
             return True
    -    except:
    +    except (NoSuchElementException, TimeoutException):
             return False
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Using specific exceptions improves error handling by avoiding the masking of unexpected exceptions, which is a significant improvement in code robustness.

    8
    ✅ Add assertions to verify initial state in test methods
    Suggestion Impact:An assertion was added to verify the initial state of the todo count in the test method, which aligns with the suggestion to enhance test reliability by ensuring the test environment is correctly set up.

    code diff:

         def test_new_todo(self, page: TodoPage):
    +        assert page.todo_count() == 0

    In the test methods, consider adding assertions to verify the initial state before
    performing actions. This helps ensure that the test environment is in the expected
    state and can catch potential setup issues.

    website_and_docs/content/documentation/test_practices/design_strategies.en.md [571-573]

     def test_new_todo(self, page: TodoPage):
    +    assert page.todo_count() == 0, "Initial todo count should be 0"
         page.new_todo("aaa")
         assert page.count_todo_items_left() == page.build_count_todo_left(1)
     
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Adding assertions for initial state verification enhances test reliability by ensuring the test environment is correctly set up, which is a good practice for test accuracy.

    7
    Enhancement
    ✅ Convert static method to instance method for consistency and flexibility
    Suggestion Impact:The static method build_count_todo_left was converted to an instance method in the TodoPage class.

    code diff:

    -    @staticmethod
    -    def build_count_todo_left(count: int) -> str:
    +    def build_count_todo_left(self, count: int) -> str:
             if count == 1:
                 return "1 item left!"
             else:

    Instead of using a static method for build_count_todo_left, consider making it an
    instance method to improve consistency with other methods in the class and allow for
    potential future customization.

    website_and_docs/content/documentation/test_practices/design_strategies.en.md [503-507]

    -@staticmethod
    -def build_count_todo_left(count: int) -> str:
    +def build_count_todo_left(self, count: int) -> str:
         if count == 1:
             return "1 item left!"
         else:
             return f"{count} items left!"
     
    • Apply this suggestion
    Suggestion importance[1-10]: 5

    Why: While converting the static method to an instance method can improve consistency, it is not critical to the functionality and mainly enhances potential future flexibility.

    5

    As suggested,
    1. Break the long and complex example down into smaller, more manageable sections or separate files for better readability and maintainability
    2. Use a `with` context manager for WebDriver to ensure proper resource cleanup
    3. Use specific exception handling instead of a bare except clause
    4. Convert static method to instance method for consistency and flexibility
    5. Add assertions to verify initial state in test methods
    6. Remove the commented code in the `hover` method
    @PeteSong PeteSong changed the title Petesong/Add a Python example for the test practice petesong/Add a Python example for the test practice Sep 5, 2024
    @PeteSong
    Copy link
    Author

    PeteSong commented Sep 5, 2024

    PR Code Suggestions ✨

    Category Suggestion                                                                                                                                    Score
    Best practice
    ✅ Use a context manager for WebDriver to ensure proper resource cleanup
    9
    Use specific exception handling instead of a bare except clause
    8
    ✅ Add assertions to verify initial state in test methods
    7
    Enhancement
    ✅ Convert static method to instance method for consistency and flexibility
    5

    Applied the suggestions.

    @PeteSong
    Copy link
    Author

    PeteSong commented Sep 5, 2024

    PR Reviewer Guide 🔍

    ⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Key issues to review

    Code ComplexityThe added Python example is quite long and complex. Consider breaking it down into smaller, more manageable sections or separate files for better readability and maintainability.

    Error HandlingThe is_loaded method in the TodoPage class uses a bare except clause, which might catch and suppress unexpected errors. Consider using a more specific exception or at least logging the error.

    Commented CodeThere is commented out code in the hover method of the ActionBot class. Consider removing it if it's not needed or add a comment explaining why it's kept.

    Applied the suggestions.

    @shbenzer
    Copy link
    Contributor

    shbenzer commented Sep 18, 2024

    Thank you for the contribution @PeteSong! Before it can be properly reviewed a few things must be changed:

    1. the example must be moved into a separate executable file like our other examples
    2. the example must be properly formatted like other examples to pull from the example file
    3. the example must be propagated to all translations
    4. lines must be kept to 100 characters per the style guide https://www.selenium.dev/documentation/about/style/

    @PeteSong
    Copy link
    Author

    Thank you for the contribution @PeteSong! Before it can be properly reviewed a few things must be changed:

    1. the example must be moved into a separate executable file like our other examples
    2. the example must be properly formatted like other examples to pull from the example file
    3. the example must be propagated to all translations
    4. lines must be kept to 100 characters per the style guide https://www.selenium.dev/documentation/about/style/

    DONE

    @shbenzer
    Copy link
    Contributor

    Thank you for the contribution @PeteSong! Before it can be properly reviewed a few things must be changed:

    1. the example must be moved into a separate executable file like our other examples
    2. the example must be properly formatted like other examples to pull from the example file
    3. the example must be propagated to all translations
    4. lines must be kept to 100 characters per the style guide https://www.selenium.dev/documentation/about/style/

    DONE

    Awesome, thank you! @harsha1502

    Copy link

    @A1exKH A1exKH left a comment

    Choose a reason for hiding this comment

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

    @PeteSong please resolve conflicts for this PR.

    @PeteSong
    Copy link
    Author

    @PeteSong please resolve conflicts for this PR.

    DONE

    Copy link

    @A1exKH A1exKH left a comment

    Choose a reason for hiding this comment

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

    LGTM.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    documentation Improvements or additions to documentation Review effort [1-5]: 4 tests
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    4 participants