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

Handle case where Browser.WindowID is not found and improve error handling. #63

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

cleitonleonel
Copy link

Description

Improved error handling when retrieving Browser.WindowID. Now, if Browser.WindowID is not found, the code properly handles cases where pages are not attached or targetId is missing, preventing unexpected failures..

Changes

Handle missing Browser.WindowID gracefully.
Added a new method that gets the window ID based on the target ID.
Added extra validation for missing targetId and improved error messages.

Testing

Tests passing, test_browser and test_chrome.

This PR addresses issue #57 by fix bug Browser.WindowID wasn't found.

@thalissonvs thalissonvs added the bug Something isn't working label Mar 13, 2025
@thalissonvs thalissonvs linked an issue Mar 13, 2025 that may be closed by this pull request
@thalissonvs thalissonvs requested a review from Copilot March 13, 2025 00:37
@codecov-commenter
Copy link

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 44.44444% with 15 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pydoll/browser/base.py 40.00% 12 Missing ⚠️
pydoll/commands/browser.py 50.00% 3 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Flag Coverage Δ
tests 96.23% <44.44%> (-0.95%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
pydoll/browser/page.py 94.35% <100.00%> (+0.03%) ⬆️
pydoll/commands/browser.py 91.42% <50.00%> (-8.58%) ⬇️
pydoll/browser/base.py 89.93% <40.00%> (-7.27%) ⬇️
🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot

This comment was marked as outdated.

Repository owner deleted a comment from Copilot bot Mar 13, 2025
@thalissonvs
Copy link
Owner

Hi @cleitonleonel , first of all, thanks for your contribution!

Sorry for not have a contrib file. You have to run the lint to check the code format: poetry run task lint.
Also, you'll have to write new test cases to the modifications you've done

Comment on lines 258 to 272
pages = await self.get_targets()
valid_page = next(
(page for page in pages if page.get('type') == 'page' and page.get('attached')),
None
)

if not valid_page:
raise RuntimeError("No valid attached browser page found.")

target_id = valid_page.get('targetId')
if not target_id:
raise RuntimeError("Valid page found but missing 'targetId'.")

command = BrowserCommands.get_window_id_by_target(target_id)
response = await self._execute_command(command)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can refactor this method to simplify the tests. For example, you can create a method to get the valid page and validate before returns. Also, you can pass BrowserCommands.get_window_id_by_target(target_id) direct to the _execute_command method, since there's no other use for the variable command

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That way we can have a more clean implementation and easy to follow

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's my suggestion:

if response.get('error'):
    target_id = self._get_valid_target_id() # this will raise the exceptions and do the validations
    response = await self._execute_command(
        BrowserCommands.get_window_id_by_target(target_id)
    )

This way you can write tests more easily

Comment on lines 274 to 278
try:
return response['result']['windowId']
except KeyError:
error_message = response.get('error', {}).get('message', "Unknown error")
raise RuntimeError(f"Failed to get window ID: {error_message}")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to avoid try/except block always as possible, i would suggest something like this:

if (window_id := response.get('result', {}).get('windowId')) is not None:
    return window_id
    
error_data = response.get('error', {})
raise RuntimeError('error message')

what do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Browser.WindowID wasn't found
3 participants