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 Selenium Manager Code Examples #1870

Merged
merged 4 commits into from
Aug 19, 2024
Merged

Added Selenium Manager Code Examples #1870

merged 4 commits into from
Aug 19, 2024

Conversation

shbenzer
Copy link
Contributor

@shbenzer shbenzer commented Aug 18, 2024

User description

Added coding examples for selenium manager to the documentation

Description

added comparison examples in examples/python/tests/selenium_manager/usage.py to provide examples of setup before and after selenium manager
added documentation for those examples to Selenium Manager Documentation

Motivation and Context

Documentation was needed

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 Python code examples for setting up and tearing down Selenium tests.
  • Documented the implementation of Selenium Manager in scripts across multiple languages.
  • Enhanced the documentation with examples of using Selenium Manager for setup.

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 Chrome driver.
  • Added teardown function to quit the driver.
  • +8/-0     
    Documentation
    usage.py
    Add setup examples for Selenium Manager usage                       

    examples/python/tests/selenium_manager/usage.py

  • Added example for setup without Selenium Manager.
  • Added example for setup with Selenium Manager.
  • +12/-0   
    selenium_manager.en.md
    Document Selenium Manager implementation in scripts           

    website_and_docs/content/documentation/selenium_manager.en.md

  • Added section on implementing Selenium Manager in scripts.
  • Included Python code examples for setup with and without Selenium
    Manager.
  • +23/-0   
    selenium_manager.ja.md
    Document Selenium Manager implementation in scripts (Japanese)

    website_and_docs/content/documentation/selenium_manager.ja.md

  • Added section on implementing Selenium Manager in scripts.
  • Included Python code examples for setup with and without Selenium
    Manager.
  • +23/-0   
    selenium_manager.pt-br.md
    Document Selenium Manager implementation in scripts (Portuguese)

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

  • Added section on implementing Selenium Manager in scripts.
  • Included Python code examples for setup with and without Selenium
    Manager.
  • +23/-0   
    selenium_manager.zh-cn.md
    Document Selenium Manager implementation in scripts (Chinese)

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

  • Added section on implementing Selenium Manager in scripts.
  • Included Python code examples for setup with and without Selenium
    Manager.
  • +23/-0   
    using_selenium.en.md
    Add Python setup and teardown examples                                     

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

    • Added Python code examples for setup and teardown.
    +9/-0     
    using_selenium.ja.md
    Add Python setup and teardown examples (Japanese)               

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

    • Added Python code examples for setup and teardown.
    +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 Preview for selenium-dev ready!

    Name Link
    🔨 Latest commit bd1d6de
    🔍 Latest deploy log https://app.netlify.com/sites/selenium-dev/deploys/66c34a46d273cf0008fdf79a
    😎 Deploy Preview https://deploy-preview-1870--selenium-dev.netlify.app
    📱 Preview on mobile
    Toggle QR Code...

    QR Code

    Use your smartphone camera to open QR code link.

    To edit notification comments on pull requests, go to your Netlify site configuration.

    @codiumai-pr-agent-pro codiumai-pr-agent-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

    Hardcoded Path
    The setup_without_selenium_manager function uses a hardcoded path for the Chrome executable, which may not be portable across different systems.

    Incomplete Documentation
    The added documentation for implementing Selenium Manager in scripts only includes examples for Python. Other language tabs are empty, which may confuse users.

    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 driver closure.

    examples/python/tests/selenium_manager/usage.py [10-12]

    +from contextlib import contextmanager
    +
    +@contextmanager
     def setup_with_selenium_manager():
         driver = webdriver.Chrome()
    -    return driver
    +    try:
    +        yield driver
    +    finally:
    +        driver.quit()
     
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    Why: This suggestion enhances resource management by ensuring the WebDriver is properly closed even if an exception occurs, which is a best practice for managing external resources.

    9
    Use a context manager in the setup function to ensure proper resource cleanup

    Consider using a context manager (with statement) in the setup function to ensure
    the driver is properly closed, even if an exception occurs.

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

    +from contextlib import contextmanager
    +
    +@contextmanager
     def setup():
         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()
     
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    Why: Implementing a context manager in the setup function ensures that the WebDriver is closed properly, even if an error occurs, which improves resource management and code reliability.

    9
    Maintainability
    Remove redundant teardown function when using a context manager

    The teardown function is redundant if you implement a context manager in the setup
    function. Consider removing it to avoid potential double-closing of the driver.

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

    -def teardown(driver):
    -    driver.quit()
    +# Remove this function if using a context manager in setup
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: The teardown function becomes unnecessary with the use of a context manager, reducing redundancy and potential errors from double-closing the WebDriver.

    8
    Enhancement
    Use a configurable path for the Chrome executable in the setup function

    In the setup_without_selenium_manager function, consider using a relative or
    configurable path for the Chrome executable instead of a hardcoded path.

    examples/python/tests/selenium_manager/usage.py [5-8]

    +import os
    +
     def setup_without_selenium_manager():
    -    chrome_service = Service(executable_path='path/to/chrome.exe')
    -    driver = webdriver.Chrome(chrome_service)
    +    chrome_path = os.environ.get('CHROME_PATH', 'path/to/chrome.exe')
    +    chrome_service = Service(executable_path=chrome_path)
    +    driver = webdriver.Chrome(service=chrome_service)
         return driver
     
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: This suggestion improves flexibility and portability by allowing the Chrome executable path to be configured via an environment variable, which is beneficial for different environments or setups.

    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.

    Thank you, @shbenzer1

    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.

    Thank you, @shbenzer!

    @diemol diemol merged commit bc67d34 into SeleniumHQ:trunk Aug 19, 2024
    12 checks passed
    selenium-ci added a commit that referenced this pull request Aug 19, 2024
    * added setup and teardown python examples to Organizing and Executing Selenium Code
    
    * added examples for selenium manager to documentation
    
    [deploy site] bc67d34
    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