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

Closed
wants to merge 5 commits into from
Closed

Added Python Examples to Organizing and Executing Selenium Code #1869

wants to merge 5 commits into from

Conversation

shbenzer
Copy link
Contributor

@shbenzer shbenzer commented Aug 18, 2024

User description

Added Execution Example 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 for execution

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 using_selenium_tests.py to manage WebDriver lifecycle.
  • Updated English and Japanese documentation to include Python setup and teardown examples.
  • Enhanced documentation to provide clearer guidance for Python Selenium tests.

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 the WebDriver and open a URL.
  • Added teardown function to quit the WebDriver.
  • +8/-0     
    Documentation
    using_selenium.en.md
    Update English documentation with Python examples               

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

  • Added Python setup and teardown code examples.
  • Updated documentation to include Python-specific instructions.
  • +10/-0   
    using_selenium.ja.md
    Update Japanese documentation with Python 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 Python-specific
    instructions.
  • +10/-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 7c16668

    @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 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.

    Missing Context
    The added Python setup and teardown examples lack explanatory text. Consider adding brief descriptions to explain the purpose and usage of these functions.

    Untranslated Content
    The added Python setup and teardown examples in the Japanese version are not translated. Consider translating the headers and any explanatory text to Japanese for consistency.

    Copy link
    Contributor

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Best practice
    Use a context manager for WebDriver to ensure proper resource management

    Consider using a context manager (with statement) for the WebDriver to ensure proper
    resource management and automatic closing of the browser, even if an exception
    occurs.

    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: Using a context manager is a best practice for resource management, ensuring that the WebDriver is properly closed even if an exception occurs, which enhances code reliability and safety.

    9
    Enhancement
    Add error handling and logging to setup and teardown functions

    Consider adding error handling and logging to the setup and teardown functions to
    improve robustness and debugging capabilities.

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

    +import logging
    +
     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:
    +        logging.error(f"Error during setup: {e}")
    +        raise
     
     def teardown(driver):
    -    driver.quit()
    +    try:
    +        driver.quit()
    +    except Exception as e:
    +        logging.error(f"Error during teardown: {e}")
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Adding error handling and logging improves robustness and aids in debugging by providing clear error messages, which is beneficial for maintaining and troubleshooting the code.

    8
    Possible issue
    Add a wait mechanism to ensure the page is fully loaded before proceeding

    Consider adding a timeout or wait mechanism in the setup function to ensure the page
    is fully loaded before proceeding with the test.

    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.ID, "message"))
    +    )
         return driver
     
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Implementing a wait mechanism ensures that the test does not proceed until the page is fully loaded, which can prevent flaky tests and improve test reliability.

    7
    Maintainability
    Parameterize the URL and browser type in the setup function for flexibility

    Consider parameterizing the URL and browser type in the setup function to make it
    more flexible and reusable across different tests.

    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")
    +def setup(browser_type='chrome', url='https://www.selenium.dev/selenium/web/web-form.html'):
    +    if browser_type.lower() == 'chrome':
    +        driver = webdriver.Chrome()
    +    elif browser_type.lower() == 'firefox':
    +        driver = webdriver.Firefox()
    +    else:
    +        raise ValueError(f"Unsupported browser type: {browser_type}")
    +    driver.get(url)
         return driver
     
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Parameterizing the URL and browser type increases the flexibility and reusability of the setup function, making it adaptable for different testing scenarios without modifying the code.

    7

    @diemol
    Copy link
    Member

    diemol commented Aug 19, 2024

    Is this PR duplicating #1868?

    @shbenzer
    Copy link
    Contributor Author

    @diemol It seems that each of my 4 PRs includes the previous in it

    @diemol
    Copy link
    Member

    diemol commented Aug 19, 2024

    OK, so please either split them or have a single one.

    For the future, it is better to have separate change sets.

    @shbenzer
    Copy link
    Contributor Author

    @diemol Would you like me to cancel all of these prs and merge them into one?

    @diemol
    Copy link
    Member

    diemol commented Aug 19, 2024

    #1868 was merged.

    @diemol
    Copy link
    Member

    diemol commented Aug 19, 2024

    Is that not needed in the other files?

    @shbenzer
    Copy link
    Contributor Author

    I'm just ensuring that the merge will be easier at the end - I didn't remove any commits from the pr

    @diemol
    Copy link
    Member

    diemol commented Aug 19, 2024

    Why are only the Japanese and English files changed?

    @shbenzer
    Copy link
    Contributor Author

    I'll fix the remaining two prs

    @shbenzer shbenzer closed this Aug 19, 2024
    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