Skip to content

Refined python example in Organizing and Executing Selenium Code #1890

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 3 commits into from
Aug 26, 2024

Conversation

shbenzer
Copy link
Contributor

@shbenzer shbenzer commented Aug 24, 2024

User description

Refined the Python example in Organizing and Executing Selenium Code

Description

integrated setup and teardown into using_selenium_tests.py::test_eight_components()
changed example lines referencing the file in all translations of Organizing and Executing Selenium Code (using_selenium.md)

Motivation and Context

increases readability and understanding for new users

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

  • Refactored the Python Selenium test script to include setup and teardown functions, enhancing readability and maintainability.
  • Removed erroneous badge-code tags from multiple documentation files across different languages.
  • Updated documentation to reflect changes in the Python example, ensuring consistency in code references.

Changes walkthrough 📝

Relevant files
Enhancement
1 files
using_selenium_tests.py
Refactor Python Selenium test with setup and teardown       

examples/python/tests/getting_started/using_selenium_tests.py

  • Integrated setup and teardown functions in test_eight_components.
  • Improved code readability by refactoring.
  • +2/-4     
    Documentation
    16 files
    selenium_manager.en.md
    Remove erroneous badge-code tags from documentation           

    website_and_docs/content/documentation/selenium_manager.en.md

    • Removed erroneous badge-code tags.
    +0/-1     
    selenium_manager.ja.md
    Remove erroneous badge-code tags from documentation           

    website_and_docs/content/documentation/selenium_manager.ja.md

    • Removed erroneous badge-code tags.
    +0/-1     
    selenium_manager.pt-br.md
    Remove erroneous badge-code tags from documentation           

    website_and_docs/content/documentation/selenium_manager.pt-br.md

    • Removed erroneous badge-code tags.
    +0/-1     
    selenium_manager.zh-cn.md
    Remove erroneous badge-code tags from documentation           

    website_and_docs/content/documentation/selenium_manager.zh-cn.md

    • Removed erroneous badge-code tags.
    +0/-1     
    _index.en.md
    Remove erroneous badge-code tags from documentation           

    website_and_docs/content/documentation/webdriver/drivers/_index.en.md

    • Removed erroneous badge-code tags.
    +0/-4     
    _index.ja.md
    Remove erroneous badge-code tags from documentation           

    website_and_docs/content/documentation/webdriver/drivers/_index.ja.md

    • Removed erroneous badge-code tags.
    +0/-4     
    _index.pt-br.md
    Remove erroneous badge-code tags from documentation           

    website_and_docs/content/documentation/webdriver/drivers/_index.pt-br.md

    • Removed erroneous badge-code tags.
    +0/-4     
    _index.zh-cn.md
    Remove erroneous badge-code tags from documentation           

    website_and_docs/content/documentation/webdriver/drivers/_index.zh-cn.md

    • Removed erroneous badge-code tags.
    +0/-4     
    first_script.en.md
    Remove erroneous badge-code tags from documentation           

    website_and_docs/content/documentation/webdriver/getting_started/first_script.en.md

    • Removed erroneous badge-code tags.
    +0/-1     
    first_script.ja.md
    Remove erroneous badge-code tags from documentation           

    website_and_docs/content/documentation/webdriver/getting_started/first_script.ja.md

    • Removed erroneous badge-code tags.
    +0/-1     
    first_script.pt-br.md
    Remove erroneous badge-code tags from documentation           

    website_and_docs/content/documentation/webdriver/getting_started/first_script.pt-br.md

    • Removed erroneous badge-code tags.
    +0/-1     
    first_script.zh-cn.md
    Remove erroneous badge-code tags from documentation           

    website_and_docs/content/documentation/webdriver/getting_started/first_script.zh-cn.md

    • Removed erroneous badge-code tags.
    +0/-1     
    using_selenium.en.md
    Update code block references in documentation                       

    website_and_docs/content/documentation/webdriver/getting_started/using_selenium.en.md

    • Updated code block references to match refactored Python example.
    +3/-4     
    using_selenium.ja.md
    Update code block references in documentation                       

    website_and_docs/content/documentation/webdriver/getting_started/using_selenium.ja.md

    • Updated code block references to match refactored Python example.
    +3/-4     
    using_selenium.pt-br.md
    Update code block references in documentation                       

    website_and_docs/content/documentation/webdriver/getting_started/using_selenium.pt-br.md

    • Updated code block references to match refactored Python example.
    +3/-4     
    using_selenium.zh-cn.md
    Update code block references in documentation                       

    website_and_docs/content/documentation/webdriver/getting_started/using_selenium.zh-cn.md

    • Updated code block references to match refactored Python example.
    +3/-4     

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

    Copy link

    netlify bot commented Aug 24, 2024

    👷 Deploy request for selenium-dev pending review.

    Visit the deploys page to approve it

    Name Link
    🔨 Latest commit 622e83c

    @qodo-merge-pro qodo-merge-pro bot added documentation Improvements or additions to documentation enhancement New feature or request Review effort [1-5]: 2 labels Aug 24, 2024
    Copy link
    Contributor

    PR Reviewer Guide 🔍

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Key issues to review

    Code Refactoring
    The test function has been refactored to use setup and teardown functions, which improves code organization and reusability. However, the changes should be reviewed to ensure they maintain the original functionality and follow best practices for Selenium testing.

    Copy link
    Contributor

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Add a wait condition before asserting to ensure the page has loaded completely

    Consider adding a wait condition before asserting the message text to ensure the
    page has loaded completely and the message is present.

    examples/python/tests/getting_started/using_selenium_tests.py [19-21]

    -message = driver.find_element(by=By.ID, value="message")
    +from selenium.webdriver.support.ui import WebDriverWait
    +from selenium.webdriver.support import expected_conditions as EC
    +
    +message = WebDriverWait(driver, 10).until(
    +    EC.presence_of_element_located((By.ID, "message"))
    +)
     value = message.text
     assert value == "Received!"
     
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    Why: Adding a wait condition is crucial for ensuring that the element is present before interacting with it, which prevents flaky tests and improves test reliability.

    9
    Best practice
    Use a context manager for better resource management and automatic cleanup

    Consider using a context manager (with statement) for the driver to ensure proper
    resource management and automatic cleanup.

    examples/python/tests/getting_started/using_selenium_tests.py [6-23]

    -driver = setup()
    +with setup() as driver:
    +    title = driver.title
    +    assert title == "Web form"
    +    
    +    ...
     
    -title = driver.title
    -assert title == "Web form"
    -
    -...
    -
    -teardown(driver)
    -
    Suggestion importance[1-10]: 8

    Why: Using a context manager ensures that resources are properly managed and cleaned up automatically, which is a best practice for managing external resources like web drivers.

    8
    Error handling
    Add error handling to setup and teardown functions for improved robustness

    Consider adding error handling to the setup and teardown functions to gracefully
    handle any exceptions that may occur during driver initialization or cleanup.

    examples/python/tests/getting_started/using_selenium_tests.py [25-31]

     def setup():
    -    driver = webdriver.Chrome()
    -    driver.get("https://www.selenium.dev/selenium/web/web-form.html")
    -    return driver
    +    try:
    +        driver = webdriver.Chrome()
    +        driver.get("https://www.selenium.dev/selenium/web/web-form.html")
    +        return driver
    +    except Exception as e:
    +        print(f"Error during setup: {e}")
    +        raise
     
     def teardown(driver):
    -    driver.quit()
    +    try:
    +        driver.quit()
    +    except Exception as e:
    +        print(f"Error during teardown: {e}")
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Implementing error handling in setup and teardown functions enhances the robustness of the code by gracefully handling exceptions and providing informative error messages.

    8
    Maintainability
    Use more descriptive variable names for improved code readability

    Consider using more descriptive variable names for clarity. For example, rename
    'value' to 'message_text' to better represent its content.

    examples/python/tests/getting_started/using_selenium_tests.py [19-21]

    -message = driver.find_element(by=By.ID, value="message")
    -value = message.text
    -assert value == "Received!"
    +message_element = driver.find_element(by=By.ID, value="message")
    +message_text = message_element.text
    +assert message_text == "Received!"
     
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: More descriptive variable names improve code readability and maintainability, making it easier for others to understand the code's purpose and functionality.

    7

    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.

    Thank you @shbenzer !

    @harsha509 harsha509 merged commit 0d45fb9 into SeleniumHQ:trunk Aug 26, 2024
    9 checks passed
    @hbbert
    Copy link

    hbbert commented Jun 26, 2025

    示例中的地址 访问不了。

    @shbenzer
    Copy link
    Contributor Author

    shbenzer commented Jun 26, 2025

    示例中的地址 访问不了。

    Screenshot 2025-06-26 at 11 07 36 AM https://www.selenium.dev/selenium/web/web-form.html works for me

    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 enhancement New feature or request Review effort [1-5]: 2
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    3 participants