Skip to content

moved python examples for Relative Locators #1962

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

Closed
wants to merge 2 commits into from

Conversation

shbenzer
Copy link
Contributor

@shbenzer shbenzer commented Sep 23, 2024

User description

moved python examples for relative locators

Description

moved python examples for relative locators in all languages

Motivation and Context

increase comprehensiveness of website

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

enhancement, documentation


Description

  • Added Python test examples demonstrating the use of relative locators in Selenium.
  • Updated documentation in multiple languages (English, Japanese, Portuguese, Chinese) to include links to the new Python test examples.
  • Enhanced the comprehensiveness of the website by integrating Python examples directly into the documentation using gh-codeblock.

Changes walkthrough 📝

Relevant files
Enhancement
test_locators.py
Add Python tests for relative locators demonstration         

examples/python/tests/elements/test_locators.py

  • Added multiple test functions demonstrating relative locators.
  • Utilized locate_with for different relative positions like above,
    below, near, etc.
  • Each test function opens a web page and interacts with elements using
    relative locators.
  • +52/-0   
    Documentation
    locators.en.md
    Update English documentation with Python example links     

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

  • Updated Python code examples to use gh-codeblock for better
    integration.
  • Enhanced documentation with direct references to Python test file
    lines.
  • +13/-13 
    locators.ja.md
    Update Japanese documentation with Python example links   

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

  • Updated Python code examples to use gh-codeblock.
  • Improved documentation with direct references to Python test file
    lines.
  • +18/-13 
    locators.pt-br.md
    Update Portuguese documentation with Python example links

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

  • Updated Python code examples to use gh-codeblock.
  • Enhanced documentation with direct references to Python test file
    lines.
  • +18/-13 
    locators.zh-cn.md
    Update Chinese documentation with Python example links     

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

  • Updated Python code examples to use gh-codeblock.
  • Improved documentation with direct references to Python test file
    lines.
  • +18/-13 

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

    Copy link

    netlify bot commented Sep 23, 2024

    👷 Deploy request for selenium-dev pending review.

    Visit the deploys page to approve it

    Name Link
    🔨 Latest commit fed1c3b

    @qodo-merge-pro qodo-merge-pro bot added documentation Improvements or additions to documentation enhancement New feature or request labels Sep 23, 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 after use, which may lead to resource leaks.

    Error Handling
    There is no error handling or assertions in the test functions, which may lead to silent failures.

    Copy link
    Contributor

    PR Code Suggestions ✨

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

    Use a context manager to ensure the WebDriver is properly closed after each test
    function, even if an exception occurs.

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

     def testAbove():
    -    driver = webdriver.Chrome()
    -    driver.get("https://www.selenium.dev/selenium/web/inputs.html")
    -    
    -    password_input = driver.find_element(By.NAME, "password-input")
    -    email = locate_with(By.TAG_NAME, "input").above(password_input)
    -    email.send_keys("test@test.com")
    +    with webdriver.Chrome() as driver:
    +        driver.get("https://www.selenium.dev/selenium/web/inputs.html")
    +        
    +        password_input = driver.find_element(By.NAME, "password-input")
    +        email = locate_with(By.TAG_NAME, "input").above(password_input)
    +        email.send_keys("test@test.com")
     
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    Why: This suggestion is crucial for ensuring that the WebDriver is properly closed after each test, preventing resource leaks and potential issues with subsequent tests. It addresses a best practice in resource management.

    9
    Use explicit wait to ensure element presence before interaction

    Use an explicit wait instead of an implicit wait to ensure the element is present
    before interacting with it.

    examples/python/tests/elements/test_locators.py [14-20]

    +from selenium.webdriver.support.ui import WebDriverWait
    +from selenium.webdriver.support import expected_conditions as EC
    +
     def testBelow():
    -    driver = webdriver.Chrome()
    -    driver.get("https://www.selenium.dev/selenium/web/inputs.html")
    -    
    -    password_input = driver.find_element(By.NAME, "password_input")
    -    search = locate_with(By.TAG_NAME, "input").below(password_input)
    -    search.send_keys("XXXXXXXXXXXXX")
    +    with webdriver.Chrome() as driver:
    +        driver.get("https://www.selenium.dev/selenium/web/inputs.html")
    +        
    +        password_input = driver.find_element(By.NAME, "password_input")
    +        search = WebDriverWait(driver, 10).until(
    +            EC.presence_of_element_located((locate_with(By.TAG_NAME, "input").below(password_input)))
    +        )
    +        search.send_keys("XXXXXXXXXXXXX")
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Using explicit waits improves test reliability by ensuring elements are present before interaction, which is a best practice in Selenium testing. This change enhances the robustness of the test.

    8
    Add assertions to verify the expected behavior after element interactions

    Add assertions to verify the expected behavior after interacting with the elements.

    examples/python/tests/elements/test_locators.py [46-53]

     def testAboveAndBelow():
    -    driver = webdriver.Chrome()
    -    driver.get("https://www.selenium.dev/selenium/web/inputs.html")
    -    
    -    password_input = driver.find_element(By.NAME, "password_input")
    -    number_input = driver.find_element(By.NAME, "number_input")
    -    email = locate_with(By.TAG_NAME, "input").above(password_input).below(number_input)
    -    email.send_keys("test@test.com")
    +    with webdriver.Chrome() as driver:
    +        driver.get("https://www.selenium.dev/selenium/web/inputs.html")
    +        
    +        password_input = driver.find_element(By.NAME, "password_input")
    +        number_input = driver.find_element(By.NAME, "number_input")
    +        email = locate_with(By.TAG_NAME, "input").above(password_input).below(number_input)
    +        email.send_keys("test@test.com")
    +        
    +        assert email.get_attribute("value") == "test@test.com"
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Adding assertions is a best practice in testing to verify that the code behaves as expected. This suggestion improves the test's effectiveness by ensuring the intended outcome is achieved.

    8
    Enhancement
    Use a more specific locator strategy for improved reliability

    Use a more specific locator strategy for the password input field to improve
    reliability.

    examples/python/tests/elements/test_locators.py [18]

    -password_input = driver.find_element(By.NAME, "password_input")
    +password_input = driver.find_element(By.CSS_SELECTOR, "input[type='password'][name='password_input']")
     
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: A more specific locator strategy can improve the reliability of element selection, reducing the chance of selecting the wrong element. This is a useful enhancement for test accuracy.

    7

    💡 Need additional feedback ? start a PR chat

    @pmartinez1
    Copy link
    Contributor

    @shbenzer
    Copy link
    Contributor Author

    shbenzer commented Sep 24, 2024

    Kind of a dup of https://github.com/SeleniumHQ/seleniumhq.github.io/pull/1903/files?

    I honestly forgot yours included relative locators - I'll close my pr.

    @shbenzer shbenzer closed this Sep 24, 2024
    @shbenzer shbenzer deleted the relative-locators branch September 24, 2024 17:10
    @pmartinez1
    Copy link
    Contributor

    Kind of a dup of https://github.com/SeleniumHQ/seleniumhq.github.io/pull/1903/files?

    I honestly forgot yours included relative locators - I'll close my pr.

    I'm trying to help out by updating/moving python as much of the python docs. Would it be useful for me to create an "issue" so others are aware/to avoid dups?

    @shbenzer
    Copy link
    Contributor Author

    I'm trying to help out by updating/moving python as much of the python docs. Would it be useful for me to create an "issue" so others are aware/to avoid dups?

    That's great!

    I don't think creating an issue is necessary though, duplicates are rather rare (though granted I did just do it lol). But you could put a message in the selenium-documentation slack about what you're doing to give people a heads-up and let us know when you've put out a new 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 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