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

Update elements finders python docs #2099

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from

Conversation

pmartinez1
Copy link
Contributor

@pmartinez1 pmartinez1 commented Dec 13, 2024

User description

Thanks for contributing to the Selenium site and documentation!
A PR well described will help maintainers to review and merge it quickly

Before submitting your PR, please check our contributing guidelines.
Avoid large PRs, and help reviewers by making them as simple and short as possible.

Description

Moving code examples from the .md files to the .py files for elements/finders

Motivation and Context

Keeps code in a single place.

Types of changes

  • Change to the site (I have double-checked the Netlify deployment, and my changes look good)
  • [ x] 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

  • Added comprehensive test suite for Selenium element finders in Python:

    • Basic element finding methods
    • Shadow DOM interaction
    • Optimized locators
    • Multiple elements finding
    • Parent-child element relationships
    • Active element handling
  • Updated documentation across multiple languages:

    • Replaced inline Python code examples with references to actual test implementations
    • Synchronized changes across English, Japanese, Portuguese and Chinese versions
    • Maintained consistent documentation structure
  • Improved code organization by:

    • Moving code examples from markdown files to dedicated test files
    • Ensuring consistent examples across all language versions
    • Adding proper test setup/teardown patterns

Changes walkthrough 📝

Relevant files
Tests
test_finders.py
Add comprehensive test suite for Selenium element finders

examples/python/tests/elements/test_finders.py

  • Added test functions for various element finding methods in Selenium
  • Implemented tests for basic finders, shadow DOM, optimized locators,
    and active elements
  • Added examples for finding multiple elements and working with parent
    elements
  • Each test includes proper setup/teardown with driver initialization
    and quit
  • +91/-0   
    Documentation
    finders.en.md
    Update Python code examples in element finders documentation

    website_and_docs/content/documentation/webdriver/elements/finders.en.md

  • Updated Python code examples to reference actual test implementations
  • Replaced inline code with references to test file examples
  • Maintained documentation structure while updating code blocks
  • +16/-64 
    finders.ja.md
    Sync Japanese documentation with updated Python examples 

    website_and_docs/content/documentation/webdriver/elements/finders.ja.md

  • Updated Python code examples to match English version
  • Replaced inline code with references to test implementations
  • +16/-64 
    finders.pt-br.md
    Sync Portuguese documentation with updated Python examples

    website_and_docs/content/documentation/webdriver/elements/finders.pt-br.md

  • Updated Python code examples to match English version
  • Replaced inline code with references to test implementations
  • +16/-59 
    finders.zh-cn.md
    Sync Chinese documentation with updated Python examples   

    website_and_docs/content/documentation/webdriver/elements/finders.zh-cn.md

  • Updated Python code examples to match English version
  • Replaced inline code with references to test implementations
  • +16/-64 

    💡 PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    Copy link

    netlify bot commented Dec 13, 2024

    Deploy Preview for selenium-dev ready!

    Name Link
    🔨 Latest commit fee6557
    🔍 Latest deploy log https://app.netlify.com/sites/selenium-dev/deploys/675ba051a49dd60008d90fa2
    😎 Deploy Preview https://deploy-preview-2099--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.

    Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Resource Management
    Multiple test functions create and use WebDriver instances without proper cleanup in case of test failures. Consider using test fixtures or try-finally blocks to ensure driver.quit() is always called.

    Error Handling
    Tests lack proper error handling and assertions for element existence before interacting with them. Should add try-catch blocks or explicit waits where needed.

    Code Duplication
    Driver initialization and cleanup code is duplicated across all test functions. Should be refactored into setup/teardown methods.

    Copy link
    Contributor

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    General
    Ensure proper resource cleanup by using try-finally blocks when managing browser sessions

    Add proper cleanup by using try-finally block to ensure driver.quit() is called even
    if test fails

    examples/python/tests/elements/test_finders.py [5-14]

     def test_basic_finders():
         driver = webdriver.Chrome()
    -    driver.get('https://www.selenium.dev/')
    -    
    -    body_on_page = driver.find_element(By.CLASS_NAME, 'td-home')
    -    container_on_page = body_on_page.find_element(By.CLASS_NAME, 'container-fluid')
    -    
    -    assert container_on_page.is_displayed()
    -    
    -    driver.quit()
    +    try:
    +        driver.get('https://www.selenium.dev/')
    +        
    +        body_on_page = driver.find_element(By.CLASS_NAME, 'td-home')
    +        container_on_page = body_on_page.find_element(By.CLASS_NAME, 'container-fluid')
    +        
    +        assert container_on_page.is_displayed()
    +    finally:
    +        driver.quit()
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Adding try-finally blocks is crucial for proper resource management, ensuring browser sessions are always closed even if tests fail. This prevents resource leaks and hanging browser processes.

    8
    Possible issue
    Add validation for shadow root existence to prevent null pointer exceptions

    Add error handling for find_element calls to handle cases when elements are not
    found

    examples/python/tests/elements/test_finders.py [20-22]

     def test_evaluating_shadow_DOM():
         driver = webdriver.Chrome()
    -    driver.implicitly_wait(5)
    -    driver.get('https://www.selenium.dev/selenium/web/shadowRootPage.html')
    -    
    -    custom_element = driver.find_element(By.TAG_NAME, 'custom-checkbox-element')
    -    shadow_dom_input = custom_element.shadow_root.find_element(By.CSS_SELECTOR, 'input[type=checkbox]')
    +    try:
    +        driver.implicitly_wait(5)
    +        driver.get('https://www.selenium.dev/selenium/web/shadowRootPage.html')
    +        
    +        custom_element = driver.find_element(By.TAG_NAME, 'custom-checkbox-element')
    +        if not custom_element.shadow_root:
    +            raise ValueError("Shadow root not found")
    +        shadow_dom_input = custom_element.shadow_root.find_element(By.CSS_SELECTOR, 'input[type=checkbox]')
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Validating shadow root existence before accessing it prevents potential null pointer exceptions that could occur if the shadow DOM is not properly initialized, improving test reliability.

    7

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    1 participant