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 python locators docs #1903

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

Conversation

pmartinez1
Copy link
Contributor

@pmartinez1 pmartinez1 commented Aug 27, 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

This PR moves the code to the test_locators.py file in the python/tests directory.

Motivation and Context

Helps the code be more organized, instead of having stand alone code that has not been tested.

Types of changes

  • [x ] 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

  • [x ] I have read the contributing document.
  • [x ] I have used hugo to render the site/docs locally and I am sure it works.

PR Type

Tests, Documentation


Description

  • Added comprehensive test cases for various Selenium locators in Python, including class name, CSS selector, ID, name, link text, partial link text, tag name, XPath, and relative locators.
  • Updated Python documentation across multiple languages to reference the new test file for locator examples, enhancing consistency and maintainability.

Changes walkthrough 📝

Relevant files
Tests
test_locators.py
Add test cases for various Selenium locators in Python     

examples/python/tests/elements/test_locators.py

  • Added multiple test functions for different locator strategies.
  • Included tests for class name, CSS selector, ID, name, link text,
    partial link text, tag name, and XPath.
  • Added a test for relative locators with a skip marker.
  • +84/-0   
    Documentation
    locators.en.md
    Update Python locator examples to reference test file       

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

  • Updated Python code examples to reference new test file.
  • Replaced inline code with links to the test file.
  • +30/-40 
    locators.ja.md
    Update Python locator examples to reference test file       

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

  • Updated Python code examples to reference new test file.
  • Replaced inline code with links to the test file.
  • +30/-40 
    locators.pt-br.md
    Update Python locator examples to reference test file       

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

  • Updated Python code examples to reference new test file.
  • Replaced inline code with links to the test file.
  • +30/-40 
    locators.zh-cn.md
    Update Python locator examples to reference test file       

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

  • Updated Python code examples to reference new test file.
  • Replaced inline code with links to the test file.
  • +30/-40 

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

    👷 Deploy request for selenium-dev pending review.

    Visit the deploys page to approve it

    Name Link
    🔨 Latest commit 1aac7de

    @codiumai-pr-agent-pro codiumai-pr-agent-pro bot added documentation Improvements or additions to documentation tests Review effort [1-5]: 2 labels Aug 27, 2024
    Copy link
    Contributor

    PR Reviewer Guide 🔍

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

    Resource Management
    The WebDriver instances are not properly closed using a context manager or try-finally block, which may lead to resource leaks.

    Test Isolation
    The tests are not properly isolated, as they share the same WebDriver instance across multiple test functions.

    Copy link
    Contributor

    PR Code Suggestions ✨

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

    Consider using a context manager (with statement) for the WebDriver to ensure proper
    cleanup of resources, even if an exception occurs.

    examples/python/tests/elements/test_locators.py [7-11]

    -driver = webdriver.Chrome()
    -driver.get("https://www.selenium.dev/")
    +with webdriver.Chrome() as driver:
    +    driver.get("https://www.selenium.dev/")
    +    driver.find_element(By.CLASS_NAME, "td-home")
     
    -driver.find_element(By.CLASS_NAME, "td-home")
    -
    -driver.quit()
    -
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    Why: Using a context manager for WebDriver is a best practice that ensures resources are properly cleaned up even if an exception occurs, improving code reliability and maintainability.

    9
    Improve code block formatting for consistency and readability

    Use consistent formatting for the Python code blocks across all examples. Add a
    blank line after the opening triple backticks and before the closing triple
    backticks to improve readability.

    website_and_docs/content/documentation/webdriver/elements/locators.en.md [84-85]

     {{< tab header="Python" text=true >}}
    +
     {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L10" >}}
     
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: The suggestion improves readability by adding a blank line in the code block, which is a minor but beneficial enhancement for consistency across examples.

    7
    Enhancement
    Add an explicit wait before finding elements to improve test reliability

    Consider adding an explicit wait before finding elements to ensure the page has
    loaded completely, improving test reliability.

    examples/python/tests/elements/test_locators.py [7-11]

    +from selenium.webdriver.support.ui import WebDriverWait
    +from selenium.webdriver.support import expected_conditions as EC
    +
     driver = webdriver.Chrome()
     driver.get("https://www.selenium.dev/")
     
    -driver.find_element(By.CLASS_NAME, "td-home")
    +element = WebDriverWait(driver, 10).until(
    +    EC.presence_of_element_located((By.CLASS_NAME, "td-home"))
    +)
     
     driver.quit()
     
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    Why: Implementing an explicit wait ensures that elements are only searched for once they are present, which significantly improves the reliability of the tests by reducing flakiness due to timing issues.

    9
    Error handling
    Add error handling for potential exceptions during element location

    Add error handling to catch and handle potential exceptions that may occur during
    element location, such as NoSuchElementException.

    examples/python/tests/elements/test_locators.py [7-11]

    +from selenium.common.exceptions import NoSuchElementException
    +
     driver = webdriver.Chrome()
     driver.get("https://www.selenium.dev/")
     
    -driver.find_element(By.CLASS_NAME, "td-home")
    +try:
    +    driver.find_element(By.CLASS_NAME, "td-home")
    +except NoSuchElementException:
    +    print("Element not found")
    +finally:
    +    driver.quit()
     
    -driver.quit()
    -
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Adding error handling for exceptions like NoSuchElementException enhances the robustness of the test by allowing graceful handling of errors, which is crucial for reliable test execution.

    8
    Consistency
    Synchronize Python code examples across different language versions of the documentation

    Ensure that the Python code examples are consistent across all language versions of
    the documentation. Update the Japanese version to match the changes made in the
    English version.

    website_and_docs/content/documentation/webdriver/elements/locators.ja.md [81-82]

     {{< tab header="Python" text=true >}}
    +
     {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L10" >}}
     
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Ensuring consistency across language versions is important for maintaining uniformity in documentation, although it is a minor change.

    7
    Align Python code examples in the Portuguese-Brazilian documentation with the English version

    Update the Portuguese-Brazilian version of the documentation to match the changes
    made in the English version, ensuring consistency across all language versions.

    website_and_docs/content/documentation/webdriver/elements/locators.pt-br.md [84-85]

     {{< tab header="Python" text=true >}}
    +
     {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L10" >}}
     
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: The suggestion promotes consistency across different language versions, which is beneficial for users accessing the documentation in various languages, though it is a minor change.

    7
    Maintainability
    Parameterize test functions to reduce code duplication and improve maintainability

    Consider parameterizing the test functions to reduce code duplication and make it
    easier to add new test cases for different locator types.

    examples/python/tests/elements/test_locators.py [6-19]

    -def test_find_by_classname():
    -    driver = webdriver.Chrome()
    -    driver.get("https://www.selenium.dev/")
    +@pytest.mark.parametrize("locator, value", [
    +    (By.CLASS_NAME, "td-home"),
    +    (By.CSS_SELECTOR, "#announcement-banner"),
    +    # Add more locator types and values here
    +])
    +def test_find_element(locator, value):
    +    with webdriver.Chrome() as driver:
    +        driver.get("https://www.selenium.dev/")
    +        driver.find_element(locator, value)
     
    -    driver.find_element(By.CLASS_NAME, "td-home")
    -
    -    driver.quit()
    -
    -def test_find_by_css_selector():
    -    driver = webdriver.Chrome()
    -    driver.get("https://www.selenium.dev/")
    -
    -    driver.find_element(By.CSS_SELECTOR, "#announcement-banner")
    -
    -    driver.quit()
    -
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Parameterizing test functions reduces code duplication and enhances maintainability, making it easier to add new test cases and manage existing ones, although it is not as critical as resource management or error handling.

    7

    @pmartinez1
    Copy link
    Contributor Author

    Could I get a review please?

    Copy link
    Contributor

    @shbenzer shbenzer 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 for the contribution @pmartinez1 ! A few changes must be made before it can be properly reviewed:

    pytest import is unnecessary, pytest will not run a function if it is not prefixed by test unless explicitly called in a test function. Line 70 can be removed as well

    @pmartinez1
    Copy link
    Contributor Author

    Thank you for the contribution @pmartinez1 ! A few changes must be made before it can be properly reviewed:

    pytest import is unnecessary, pytest will not run a function if it is not prefixed by test unless explicitly called in a test function. Line 70 can be removed as well

    Thanks for pointing that out. I've updated the PR.

    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 Review effort [1-5]: 2 tests
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    2 participants