SeleniumHQ / seleniumhq.github.io

Official Selenium website and documentation
https://selenium.dev
Apache License 2.0
1.09k stars 1.28k forks source link

Refined python example in Organizing and Executing Selenium Code #1890

Closed shbenzer closed 2 weeks ago

shbenzer commented 3 weeks ago

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

Checklist


PR Type

enhancement, documentation


Description


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

    netlify[bot] commented 3 weeks ago

    Deploy request for selenium-dev pending review.

    Visit the deploys page to approve it

    Name Link
    Latest commit 622e83cb3a03b94d08a266216b589a6cb25e2d4e
    codiumai-pr-agent-pro[bot] commented 3 weeks ago

    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.
    codiumai-pr-agent-pro[bot] commented 3 weeks ago

    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]](https://github.com/SeleniumHQ/seleniumhq.github.io/pull/1890/files#diff-aa8c2cd4492a49444ef99b560ecda82475b27fc52ef5804d99b186c4a8c1bddcR19-R21) ```diff -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]](https://github.com/SeleniumHQ/seleniumhq.github.io/pull/1890/files#diff-aa8c2cd4492a49444ef99b560ecda82475b27fc52ef5804d99b186c4a8c1bddcR6-R23) ```diff -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]](https://github.com/SeleniumHQ/seleniumhq.github.io/pull/1890/files#diff-aa8c2cd4492a49444ef99b560ecda82475b27fc52ef5804d99b186c4a8c1bddcR25-R31) ```diff 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]](https://github.com/SeleniumHQ/seleniumhq.github.io/pull/1890/files#diff-aa8c2cd4492a49444ef99b560ecda82475b27fc52ef5804d99b186c4a8c1bddcR19-R21) ```diff -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