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 Setup and Teardown Python Examples to Organizing and Executing Selenium Code #1868

Merged
merged 3 commits into from
Aug 19, 2024

Conversation

shbenzer
Copy link
Contributor

@shbenzer shbenzer commented Aug 18, 2024

User description

Added Setup and Teardown Python Examples to Organizing and Executing Selenium Code

Description

Updated english and japanese versions of Organizing and Executing Selenium Code page to include lines of python code
updated examples/python/getting_started/using_selenium_tests.py to include setup and teardown functions

Motivation and Context

needed documentation

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

documentation, enhancement


Description

  • Added setup and teardown functions in Python Selenium test examples to improve code organization.
  • Updated English and Japanese documentation to include new Python setup and teardown examples.
  • Enhanced documentation to provide clearer guidance on organizing and executing Selenium code.

Changes walkthrough 📝

Relevant files
Enhancement
using_selenium_tests.py
Add setup and teardown functions for Selenium tests           

examples/python/tests/getting_started/using_selenium_tests.py

  • Added setup function to initialize WebDriver and open a URL.
  • Added teardown function to quit the WebDriver.
  • +8/-0     
    Documentation
    using_selenium.en.md
    Update English documentation with Python setup/teardown examples

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

  • Added Python setup and teardown code examples.
  • Updated documentation to include new code snippets.
  • +9/-0     
    using_selenium.ja.md
    Update Japanese documentation with Python setup/teardown examples

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

  • Added Python setup and teardown code examples.
  • Updated Japanese documentation to include new code snippets.
  • +9/-0     

    💡 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 18, 2024

    👷 Deploy request for selenium-dev pending review.

    Visit the deploys page to approve it

    Name Link
    🔨 Latest commit 2ce458d

    @codiumai-pr-agent-pro codiumai-pr-agent-pro bot added documentation Improvements or additions to documentation enhancement New feature or request labels Aug 18, 2024
    Copy link
    Contributor

    PR Reviewer Guide 🔍

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

    Code Duplication
    The driver.quit() call is duplicated in both the test function and the teardown function. Consider removing it from the test function to avoid redundancy.

    Unused Import
    The webdriver module is imported but not used in the visible part of the code. Ensure it's used or remove the import if unnecessary.

    Copy link
    Contributor

    PR Code Suggestions ✨

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

    Consider using a context manager for the WebDriver to ensure proper resource
    management and automatic cleanup.

    examples/python/tests/getting_started/using_selenium_tests.py [27-33]

    -def setup():
    +from contextlib import contextmanager
    +
    +@contextmanager
    +def managed_driver():
         driver = webdriver.Chrome()
    -    driver.get("https://www.selenium.dev/selenium/web/web-form.html")
    -    return driver
    +    try:
    +        driver.get("https://www.selenium.dev/selenium/web/web-form.html")
    +        yield driver
    +    finally:
    +        driver.quit()
     
    -def teardown(driver):
    -    driver.quit()
    -
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    Why: The suggestion to use a context manager is excellent for ensuring proper resource management and automatic cleanup, which is a best practice in managing WebDriver instances.

    9
    Error handling
    Add error handling to the setup function for better exception management

    Add error handling to the setup function to catch and handle potential exceptions
    during WebDriver initialization or page loading.

    examples/python/tests/getting_started/using_selenium_tests.py [27-30]

     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
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Adding error handling improves robustness by catching potential exceptions during WebDriver initialization or page loading, which is crucial for debugging and maintaining the code.

    8
    Reliability
    Use WebDriverWait to ensure the page is fully loaded before proceeding

    Consider using a WebDriverWait in the setup function to ensure the page is fully
    loaded before returning the driver.

    examples/python/tests/getting_started/using_selenium_tests.py [27-30]

    +from selenium.webdriver.support.ui import WebDriverWait
    +from selenium.webdriver.support import expected_conditions as EC
    +from selenium.webdriver.common.by import By
    +
     def setup():
         driver = webdriver.Chrome()
         driver.get("https://www.selenium.dev/selenium/web/web-form.html")
    +    WebDriverWait(driver, 10).until(
    +        EC.presence_of_element_located((By.TAG_NAME, "body"))
    +    )
         return driver
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Using WebDriverWait ensures the page is fully loaded before returning the driver, which increases the reliability of the test by preventing premature interactions with the page.

    8
    Performance
    Add a timeout for page loading to handle potential network issues

    Consider adding a timeout parameter to the driver.get() method to handle potential
    network issues or slow-loading pages.

    examples/python/tests/getting_started/using_selenium_tests.py [29]

    +driver.set_page_load_timeout(10)  # Set timeout to 10 seconds
     driver.get("https://www.selenium.dev/selenium/web/web-form.html")
     
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Introducing a timeout for page loading is a good practice to handle network issues or slow-loading pages, enhancing the performance and reliability of the test setup.

    7

    Copy link
    Member

    @diemol diemol left a comment

    Choose a reason for hiding this comment

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

    Can you also modify the other translated files? Only the English and Japanese were modified.

    image

    @diemol
    Copy link
    Member

    diemol commented Aug 19, 2024

    It seems these changes are also part of #1871. Can you leave each PR with a single set of changes?

    @shbenzer
    Copy link
    Contributor Author

    It seems that all my merges got tangled up together - if you want me to update this I'd have to include the other merges alongside it @diemol

    @diemol
    Copy link
    Member

    diemol commented Aug 19, 2024

    Whatever is easier. Maybe for the next PRs, a single set of changes is preferred.

    @shbenzer
    Copy link
    Contributor Author

    Sounds good, I'll give it a quick update and push. One moment

    @shbenzer
    Copy link
    Contributor Author

    Done

    @diemol diemol merged commit 85d7b19 into SeleniumHQ:trunk Aug 19, 2024
    9 checks passed
    selenium-ci added a commit that referenced this pull request Aug 19, 2024
    …Selenium Code (#1868)
    
    * added setup and teardown python examples to Organizing and Executing Selenium Code
    
    * added setup and teardown to other languages
    
    [deploy site] 85d7b19
    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.

    2 participants