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

Updated python docs/examples for browsers section #1854

Draft
wants to merge 11 commits into
base: trunk
Choose a base branch
from

Conversation

pmartinez1
Copy link
Contributor

@pmartinez1 pmartinez1 commented Aug 12, 2024

User description

Moved or added examples in python docs for browsers (for all missing examples)

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

Added and moved all relevant python code examples for the browsers section.

Motivation and Context

It will allow python users to see examples of working code so they can more easily and efficiently work with selenium.

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

Documentation, Enhancement


Description

  • Added new Python tests for Chrome, Edge, Firefox, Internet Explorer, and Safari browsers.
  • Updated documentation to include new Python code examples for various browsers.
  • Improved the visibility of Python examples in multiple languages.

Changes walkthrough 📝

Relevant files
Tests
5 files
test_chrome.py
Add Chrome browser tests for various features                       

examples/python/tests/browsers/test_chrome.py

  • Added tests for network conditions, logs, permissions, and casting.
  • +48/-0   
    test_edge.py
    Add Edge browser tests for various features                           

    examples/python/tests/browsers/test_edge.py

  • Added tests for network conditions, logs, permissions, and casting.
  • +44/-0   
    test_firefox.py
    Add Firefox browser tests for various features                     

    examples/python/tests/browsers/test_firefox.py

    • Added tests for full-page screenshots, context, and profiles.
    +30/-2   
    test_internet_explorer.py
    Add Internet Explorer browser tests for various features 

    examples/python/tests/browsers/test_internet_explorer.py

    • Added multiple tests for Internet Explorer options.
    +78/-0   
    test_safari.py
    Add Safari browser test for Technology Preview                     

    examples/python/tests/browsers/test_safari.py

    • Added a test for Safari Technology Preview.
    +1/-0     
    Documentation
    20 files
    chrome.en.md
    Update Chrome documentation with Python examples                 

    website_and_docs/content/documentation/webdriver/browsers/chrome.en.md

    • Updated Python code examples for Chrome browser.
    +4/-4     
    chrome.ja.md
    Update Chrome documentation with Python examples (Japanese)

    website_and_docs/content/documentation/webdriver/browsers/chrome.ja.md

    • Updated Python code examples for Chrome browser.
    +1/-1     
    chrome.pt-br.md
    Update Chrome documentation with Python examples (Portuguese)

    website_and_docs/content/documentation/webdriver/browsers/chrome.pt-br.md

    • Updated Python code examples for Chrome browser.
    +1/-1     
    chrome.zh-cn.md
    Update Chrome documentation with Python examples (Chinese)

    website_and_docs/content/documentation/webdriver/browsers/chrome.zh-cn.md

    • Updated Python code examples for Chrome browser.
    +1/-1     
    edge.en.md
    Update Edge documentation with Python examples                     

    website_and_docs/content/documentation/webdriver/browsers/edge.en.md

    • Updated Python code examples for Edge browser.
    +4/-4     
    edge.ja.md
    Update Edge documentation with Python examples (Japanese)

    website_and_docs/content/documentation/webdriver/browsers/edge.ja.md

    • Updated Python code examples for Edge browser.
    +1/-1     
    edge.pt-br.md
    Update Edge documentation with Python examples (Portuguese)

    website_and_docs/content/documentation/webdriver/browsers/edge.pt-br.md

    • Updated Python code examples for Edge browser.
    +1/-1     
    edge.zh-cn.md
    Update Edge documentation with Python examples (Chinese) 

    website_and_docs/content/documentation/webdriver/browsers/edge.zh-cn.md

    • Updated Python code examples for Edge browser.
    +1/-1     
    firefox.en.md
    Update Firefox documentation with Python examples               

    website_and_docs/content/documentation/webdriver/browsers/firefox.en.md

    • Updated Python code examples for Firefox browser.
    +4/-9     
    firefox.ja.md
    Update Firefox documentation with Python examples (Japanese)

    website_and_docs/content/documentation/webdriver/browsers/firefox.ja.md

    • Updated Python code examples for Firefox browser.
    +4/-9     
    firefox.pt-br.md
    Update Firefox documentation with Python examples (Portuguese)

    website_and_docs/content/documentation/webdriver/browsers/firefox.pt-br.md

    • Updated Python code examples for Firefox browser.
    +4/-9     
    firefox.zh-cn.md
    Update Firefox documentation with Python examples (Chinese)

    website_and_docs/content/documentation/webdriver/browsers/firefox.zh-cn.md

    • Updated Python code examples for Firefox browser.
    +4/-9     
    internet_explorer.en.md
    Update Internet Explorer documentation with Python examples

    website_and_docs/content/documentation/webdriver/browsers/internet_explorer.en.md

    • Updated Python code examples for Internet Explorer browser.
    +14/-71 
    internet_explorer.ja.md
    Update Internet Explorer documentation with Python examples (Japanese)

    website_and_docs/content/documentation/webdriver/browsers/internet_explorer.ja.md

    • Updated Python code examples for Internet Explorer browser.
    +14/-71 
    internet_explorer.pt-br.md
    Update Internet Explorer documentation with Python examples
    (Portuguese)

    website_and_docs/content/documentation/webdriver/browsers/internet_explorer.pt-br.md

    • Updated Python code examples for Internet Explorer browser.
    +14/-78 
    internet_explorer.zh-cn.md
    Update Internet Explorer documentation with Python examples (Chinese)

    website_and_docs/content/documentation/webdriver/browsers/internet_explorer.zh-cn.md

    • Updated Python code examples for Internet Explorer browser.
    +14/-71 
    safari.en.md
    Update Safari documentation with Python examples                 

    website_and_docs/content/documentation/webdriver/browsers/safari.en.md

    • Updated Python code examples for Safari browser.
    +1/-1     
    safari.ja.md
    Update Safari documentation with Python examples (Japanese)

    website_and_docs/content/documentation/webdriver/browsers/safari.ja.md

    • Updated Python code examples for Safari browser.
    +1/-1     
    safari.pt-br.md
    Update Safari documentation with Python examples (Portuguese)

    website_and_docs/content/documentation/webdriver/browsers/safari.pt-br.md

    • Updated Python code examples for Safari browser.
    +1/-1     
    safari.zh-cn.md
    Update Safari documentation with Python examples (Chinese)

    website_and_docs/content/documentation/webdriver/browsers/safari.zh-cn.md

    • Updated Python code examples for Safari browser.
    +1/-1     

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

    👷 Deploy request for selenium-dev pending review.

    Visit the deploys page to approve it

    Name Link
    🔨 Latest commit 3ad02cf

    @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]: 3 labels Aug 12, 2024
    Copy link
    Contributor

    codiumai-pr-agent-pro bot commented Aug 12, 2024

    PR Reviewer Guide 🔍

    (Review updated until commit cd1fc7e)

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

    Possible Bug
    The test test_logs uses a hardcoded assertion that may not always be valid. Consider parameterizing the test or handling different possible messages.

    Code Duplication
    The tests in test_edge.py are almost identical to those in test_chrome.py. Consider abstracting common functionality to a shared module or class to follow DRY principles.

    Hardcoded Path
    The test test_full_page_screenshots uses a hardcoded path for saving screenshots which might not work in all environments. Consider using a configurable path or a temporary directory.

    Platform Dependency
    Several tests use @pytest.mark.skipif to conditionally skip tests on non-Windows platforms. Ensure that this is clearly documented, and consider providing alternative tests for other platforms if possible.

    Copy link
    Contributor

    codiumai-pr-agent-pro bot commented Aug 12, 2024

    PR Code Suggestions ✨

    Latest suggestions up to cd1fc7e

    CategorySuggestion                                                                                                                                    Score
    Resource management
    Implement context management for WebDriver to handle resource cleanup

    Use context management for WebDriver to ensure that resources are properly managed
    and the browser is closed even if errors occur.

    examples/python/tests/browsers/test_internet_explorer.py [77-79]

    -driver = webdriver.Ie(options=options)
    -driver.get('https://www.selenium.dev/')
    -driver.quit()
    +with webdriver.Ie(options=options) as driver:
    +    driver.get('https://www.selenium.dev/')
     
    • Apply this suggestion
    Suggestion importance[1-10]: 10

    Why: Using context management ensures that the WebDriver is properly closed even if an error occurs, which is a best practice for resource management and prevents potential resource leaks.

    10
    Possible bug
    ✅ Correct the WebDriver instance to match the browser being tested
    Suggestion Impact:The suggestion was implemented by changing the WebDriver instance from webdriver.Chrome to webdriver.Edge in the test file.

    code diff:

    -    driver = webdriver.Chrome()
    +    driver = webdriver.Edge()

    Ensure that the driver instance is correctly using webdriver.Edge instead of
    webdriver.Chrome in the Edge test file.

    examples/python/tests/browsers/test_edge.py [143]

    -driver = webdriver.Chrome()
    +driver = webdriver.Edge()
     
    • Apply this suggestion
    Suggestion importance[1-10]: 10

    Why: This suggestion corrects a possible bug by ensuring the correct WebDriver instance is used, which is crucial for the test's accuracy.

    10
    Best practice
    Add exception handling to WebDriver operations to improve error management

    Consider handling exceptions for WebDriver initialization and method calls to ensure
    the application handles errors gracefully, especially when interacting with web
    elements or during browser operations which might fail.

    examples/python/tests/browsers/test_internet_explorer.py [77-78]

    -driver = webdriver.Ie(options=options)
    -driver.get('https://www.selenium.dev/')
    +try:
    +    driver = webdriver.Ie(options=options)
    +    driver.get('https://www.selenium.dev/')
    +except Exception as e:
    +    print(f"An error occurred: {e}")
    +finally:
    +    driver.quit()
     
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    Why: This suggestion improves the robustness of the code by ensuring that any exceptions during WebDriver operations are caught and handled gracefully, which is crucial for maintaining stability in automated tests.

    9
    Use a context manager for better resource management

    Use a context manager to ensure that resources are properly managed and the driver
    is quit even if exceptions occur.

    examples/python/tests/browsers/test_edge.py [126-131]

    -driver = webdriver.Edge()
    -...
    -driver.quit()
    +with webdriver.Edge() as driver:
    +    ...
     
    Suggestion importance[1-10]: 9

    Why: Using a context manager ensures that resources are properly managed and the driver is quit even if exceptions occur, which is a significant improvement in resource management.

    9
    Externalize configuration values like URLs and timeouts for flexibility and ease of maintenance

    To avoid hardcoding URLs and timeout values within the test functions, consider
    using external configuration files or environment variables.

    examples/python/tests/browsers/test_internet_explorer.py [75-78]

    -options.file_upload_dialog_timeout = 2000
    -driver.get('https://www.selenium.dev/')
    +import os
    +timeout = int(os.getenv('FILE_UPLOAD_TIMEOUT', '2000'))
    +url = os.getenv('TEST_URL', 'https://www.selenium.dev/')
    +options.file_upload_dialog_timeout = timeout
    +driver.get(url)
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Externalizing configuration values improves the flexibility and maintainability of the code, allowing for easier updates and environment-specific configurations without modifying the codebase.

    8
    Use pathlib for path operations to enhance cross-platform compatibility

    Instead of manually concatenating paths with strings, use pathlib.Path to join
    paths. This approach is more robust and cross-platform.

    examples/python/tests/browsers/test_firefox.py [138]

    -path_for_screenshot = str(pathlib.Path().absolute()) + 'screenshot.png'
    +path_for_screenshot = str(pathlib.Path().absolute() / 'screenshot.png')
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: This suggestion improves the robustness and cross-platform compatibility of the code by using pathlib for path operations, which is a best practice.

    8
    Handle exceptions specifically to avoid masking errors

    Handle exceptions more specifically rather than catching all exceptions, which can
    mask other issues.

    examples/python/tests/browsers/test_edge.py [157-164]

     try:
         ...
    -except:
    -    assert False, 'Exception when starting or stopping casting'
    +except SpecificException as e:
    +    assert False, f'Exception when starting or stopping casting: {str(e)}'
     
    Suggestion importance[1-10]: 7

    Why: Handling exceptions more specifically is a good practice as it avoids masking other potential issues, but the suggestion lacks the specific exception type to be caught.

    7
    Maintainability
    Refactor IE options configuration into a separate function for better modularity

    To ensure that the options modifications are encapsulated and reusable, consider
    refactoring the options configuration into a separate function or method.

    examples/python/tests/browsers/test_internet_explorer.py [73-75]

    -options = webdriver.IeOptions()
    -options.file_upload_dialog_timeout = 2000
    +def configure_ie_options():
    +    options = webdriver.IeOptions()
    +    options.file_upload_dialog_timeout = 2000
    +    return options
    +options = configure_ie_options()
     
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Encapsulating the options configuration in a separate function enhances code modularity and reusability, making the codebase easier to maintain and extend.

    7

    Previous suggestions

    Suggestions up to commit c0cabc1
    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Update the gh-codeblock paths to correctly reflect the relevant lines in the external file

    Ensure that the gh-codeblock paths are updated to reflect the correct lines of code
    in the external Python file, as the current line references may be outdated or
    incorrect.

    website_and_docs/content/documentation/webdriver/browsers/internet_explorer.en.md [101]

    -{{< gh-codeblock path="/examples/python/tests/browsers/test_internet_explorer.py#L75" >}}
    +{{< gh-codeblock path="/examples/python/tests/browsers/test_internet_explorer.py#L75-L79" >}}
     
    Suggestion importance[1-10]: 8

    Why: Ensuring that the gh-codeblock paths are accurate is important for maintaining the integrity of the documentation. This suggestion addresses a potential issue with outdated or incorrect line references.

    8
    Enhancement
    Replace external code links with inline code examples for better accessibility

    Replace the gh-codeblock shortcode with a more detailed example directly in the
    documentation rather than linking to an external file. This can improve the
    accessibility of the documentation by providing immediate visibility of the example
    code, which is especially useful for quick reference.

    website_and_docs/content/documentation/webdriver/browsers/internet_explorer.en.md [101]

    -{{< gh-codeblock path="/examples/python/tests/browsers/test_internet_explorer.py#L75" >}}
    +```python
    +from selenium import webdriver
    +options = webdriver.IeOptions()
    +options.file_upload_dialog_timeout = 2000
    +driver = webdriver.Ie(options=options)
    +driver.get("http://www.selenium.dev/")
    +driver.quit()
    +```
     
    Suggestion importance[1-10]: 7

    Why: This suggestion improves accessibility by providing immediate visibility of the example code, which is useful for quick reference. However, it may increase the length of the documentation.

    7
    Add explanatory comments within the gh-codeblock for clarity

    Add a brief description or comment within the gh-codeblock to explain what the code
    does, enhancing the understandability for readers who might not click through to the
    external file.

    website_and_docs/content/documentation/webdriver/browsers/internet_explorer.en.md [101]

     {{< gh-codeblock path="/examples/python/tests/browsers/test_internet_explorer.py#L75" >}}
    +# This code sets up a WebDriver with specific Internet Explorer options for file upload dialog timeout.
     
    Suggestion importance[1-10]: 6

    Why: Adding comments can enhance understandability, but it is a minor improvement since the code itself is already self-explanatory to some extent.

    6
    Best practice
    Suggest adding error handling in the external Python example for robustness

    Consider adding a fallback or error handling mechanism in the external Python
    example linked in the gh-codeblock to manage exceptions that may occur during the
    WebDriver session.

    website_and_docs/content/documentation/webdriver/browsers/internet_explorer.en.md [101]

     {{< gh-codeblock path="/examples/python/tests/browsers/test_internet_explorer.py#L75" >}}
    +# Note: Ensure to handle exceptions that may occur during the WebDriver session.
     
    Suggestion importance[1-10]: 5

    Why: While adding error handling is a good practice, the suggestion is vague and does not provide a concrete implementation. It is also not directly related to the documentation improvement.

    5

    @pmartinez1 pmartinez1 marked this pull request as draft August 12, 2024 20:14
    @pmartinez1 pmartinez1 marked this pull request as ready for review August 12, 2024 20:48
    Copy link
    Contributor

    Persistent review updated to latest commit cd1fc7e

    @pmartinez1
    Copy link
    Contributor Author

    Hi @harsha509,
    Do you have any suggestion on how to test in Windows? I have a mac. 3 python examples tests failed. I believed I was able to resolve one of the failures (the error was pretty clear as to what was going on), but it would be best to fully confirm.

    @pmartinez1
    Copy link
    Contributor Author

    Same failures, looks like I should not have passed in the second argument "True" in add_argument (

    ). Quite possibly the other two are failing because they are trying to use the same instance of IE? Relevant: https://www.selenium.dev/documentation/ie_driver_server/. I'll look into this more and find a way of running it myself.

    @pmartinez1
    Copy link
    Contributor Author

    Same failures, looks like I should not have passed in the second argument "True" in add_argument (


    ). Quite possibly the other two are failing because they are trying to use the same instance of IE? Relevant: https://www.selenium.dev/documentation/ie_driver_server/. I'll look into this more and find a way of running it myself.

    Resolved the failure due to the "true" argument being passed in. The other failures are due to the use of the force_create_process_api value being set. According to the docs: "Windows Registry HKLM_CURRENT_USER\Software\Microsoft\Internet Explorer\Main path should contain key TabProcGrowth with 0 value."

    I think the solution could be to have the test with force_create_process_api set to true to be skipped, as it could fail, if the value of the key above isn't set to 0. I will push that update, please let me know if it is not acceptable.

    @pmartinez1
    Copy link
    Contributor Author

    Same failures, looks like I should not have passed in the second argument "True" in add_argument (

    ). Quite possibly the other two are failing because they are trying to use the same instance of IE? Relevant: https://www.selenium.dev/documentation/ie_driver_server/. I'll look into this more and find a way of running it myself.

    Resolved the failure due to the "true" argument being passed in. The other failures are due to the use of the force_create_process_api value being set. According to the docs: "Windows Registry HKLM_CURRENT_USER\Software\Microsoft\Internet Explorer\Main path should contain key TabProcGrowth with 0 value."

    I think the solution could be to have the test with force_create_process_api set to true to be skipped, as it could fail, if the value of the key above isn't set to 0. I will push that update, please let me know if it is not acceptable.

    Updated and ready for review.

    @pmartinez1
    Copy link
    Contributor Author

    Hi @harsha509 ,

    The tests that were failing before, passed on the last run. The tests that failed, were not touched by me and they were timeouts. Could the checks be run again?

    Thanks.

    @pmartinez1
    Copy link
    Contributor Author

    Could I get a review please?

    @pmartinez1 pmartinez1 marked this pull request as draft September 23, 2024 18:51
    @pmartinez1
    Copy link
    Contributor Author

    Looks like this has been failing its checks consistently. Updating to a draft, so I can figure out the issue. I'll update this PR once I have seen them all pass consistently.

    @VietND96
    Copy link
    Member

    VietND96 commented Nov 7, 2024

    Could you please resolve conflict in this 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]: 3
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    3 participants