Skip to content

[py]: add chrome and edge network conditions example and docs #2054

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

Merged
merged 3 commits into from
Nov 11, 2024

Conversation

navin772
Copy link
Member

@navin772 navin772 commented Nov 11, 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

Add chrome and edge network conditions example and docs.
Method used - set_network_conditions

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

Tests, Documentation


Description

  • Added tests for setting network conditions in both Chrome and Edge browsers using Python.
  • Configured network conditions with specific latency and throughput values.
  • Updated documentation to include Python examples for setting network conditions in Chrome and Edge across multiple languages.

Changes walkthrough 📝

Relevant files
Tests
2 files
test_chrome.py
Add network conditions test for Chrome browser                     

examples/python/tests/browsers/test_chrome.py

  • Added a test for setting network conditions in Chrome.
  • Configured network conditions with latency and throughput.
  • Verified the network conditions are correctly set.
  • +19/-0   
    test_edge.py
    Add network conditions test for Edge browser                         

    examples/python/tests/browsers/test_edge.py

  • Added a test for setting network conditions in Edge.
  • Configured network conditions with latency and throughput.
  • Verified the network conditions are correctly set.
  • +18/-0   
    Documentation
    8 files
    chrome.en.md
    Update Chrome network conditions documentation in English

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

    • Updated Python example for network conditions in Chrome.
    +3/-3     
    chrome.ja.md
    Update Chrome network conditions documentation in Japanese

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

    • Updated Python example for network conditions in Chrome.
    +3/-3     
    chrome.pt-br.md
    Update Chrome network conditions documentation in Portuguese

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

    • Updated Python example for network conditions in Chrome.
    +3/-3     
    chrome.zh-cn.md
    Update Chrome network conditions documentation in Chinese

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

    • Updated Python example for network conditions in Chrome.
    +3/-3     
    edge.en.md
    Update Edge network conditions documentation in English   

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

    • Updated Python example for network conditions in Edge.
    +3/-3     
    edge.ja.md
    Update Edge network conditions documentation in Japanese 

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

    • Updated Python example for network conditions in Edge.
    +3/-3     
    edge.pt-br.md
    Update Edge network conditions documentation in Portuguese

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

    • Updated Python example for network conditions in Edge.
    +3/-3     
    edge.zh-cn.md
    Update Edge network conditions documentation in Chinese   

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

    • Updated Python example for network conditions in Edge.
    +3/-3     

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

    Copy link

    netlify bot commented Nov 11, 2024

    👷 Deploy request for selenium-dev pending review.

    Visit the deploys page to approve it

    Name Link
    🔨 Latest commit 1287c2f

    Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

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

    Resource Cleanup
    The test should use a try-finally block to ensure driver.quit() is called even if the test fails

    Resource Cleanup
    The test should use a try-finally block to ensure driver.quit() is called even if the test fails

    Test Validation
    The test only verifies that conditions are set but not that they actually affect network behavior

    Copy link
    Contributor

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Best practice
    Add error handling for network conditions setup to improve test robustness

    Add error handling around network conditions setup to gracefully handle cases where
    the browser doesn't support the feature or network conditions can't be set.

    examples/python/tests/browsers/test_chrome.py [135-140]

    -driver.set_network_conditions(**network_conditions)
    +try:
    +    driver.set_network_conditions(**network_conditions)
    +    driver.get("https://www.selenium.dev")
    +    assert driver.get_network_conditions() == network_conditions
    +except Exception as e:
    +    print(f"Failed to set network conditions: {e}")
    +    driver.quit()
    +    raise
     
    -driver.get("https://www.selenium.dev")
    -
    -# check whether the network conditions are set
    -assert driver.get_network_conditions() == network_conditions
    -
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Adding error handling is crucial for test reliability, as network condition setup may fail in different environments or browser versions. The suggestion properly handles cleanup in error cases.

    8
    Reset modified browser settings to their default state after test completion

    Clean up network conditions after the test by resetting them to default values
    before quitting the driver.

    examples/python/tests/browsers/test_chrome.py [140-142]

     assert driver.get_network_conditions() == network_conditions
     
    +# Reset network conditions to default
    +driver.set_network_conditions(offline=False, latency=0, download_throughput=-1, upload_throughput=-1)
     driver.quit()
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Resetting browser settings after test execution is a good practice to prevent side effects on subsequent tests and maintain test isolation.

    7
    Maintainability
    Define configuration values as named constants to improve code readability and maintenance

    Use constants for network condition values to improve maintainability and make the
    configuration more explicit.

    examples/python/tests/browsers/test_chrome.py [129-134]

    +LATENCY_MS = 20
    +BANDWIDTH_KBPS = 2000
     network_conditions = {
         "offline": False,
    -    "latency": 20,  # 20 ms of latency
    -    "download_throughput": 2000 * 1024 / 8,  # 2000 kbps
    -    "upload_throughput": 2000 * 1024 / 8,    # 2000 kbps
    +    "latency": LATENCY_MS,
    +    "download_throughput": BANDWIDTH_KBPS * 1024 / 8,
    +    "upload_throughput": BANDWIDTH_KBPS * 1024 / 8,
     }
    • Apply this suggestion
    Suggestion importance[1-10]: 5

    Why: Using named constants improves code readability and makes it easier to modify network condition values in the future, though the impact is moderate as the current values are already well-commented.

    5

    💡 Need additional feedback ? start a PR chat

    Copy link
    Member

    @harsha509 harsha509 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 @navin772 !

    @harsha509 harsha509 merged commit 69ddd84 into SeleniumHQ:trunk Nov 11, 2024
    8 of 9 checks passed
    @navin772 navin772 deleted the network_conditions branch November 11, 2024 17:32
    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.

    2 participants