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

#2550 Implement downloading files via CDP #2567

Merged
merged 15 commits into from Feb 2, 2024

Conversation

britka
Copy link
Contributor

@britka britka commented Nov 27, 2023

Proposed changes

Add new download method CDP. It works similar to FOLDER approach but now this process is controlled by browser.
To use this download method you need to use snippet

$("#someId").download(DownloadOptions.using(FileDownloadMode.CDP);

Note

This method works only for Chromium based browsers

For more information see:

Checklist

  • [*] Checkstyle and unit tests are passed locally with my changes by running gradlew check chrome_headless firefox_headless command
  • [*] I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Copy link
Member

@BorisOsipov BorisOsipov left a comment

Choose a reason for hiding this comment

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

Please add integration tests as in integration.FileDownloadToFolderTest

Copy link
Member

@BorisOsipov BorisOsipov left a comment

Choose a reason for hiding this comment

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

Please add integration tests as in integration.FileDownloadToFolderTest

@britka
Copy link
Contributor Author

britka commented Nov 28, 2023

Add tests to integration.FileDownloadToFolderWithCdpTest

@asolntsev asolntsev linked an issue Dec 1, 2023 that may be closed by this pull request
@asolntsev asolntsev added this to the 7.0.4 milestone Dec 1, 2023
@asolntsev
Copy link
Member

@britka Few tests failed in Firefox:

FileDownloadToFolderWithCdpTest > downloadLargeFile() FAILED
    java.lang.IllegalArgumentException: The browser you selected "firefox" doesn't have Chrome Devtools protocol functionality.
        at com.codeborne.selenide.impl.DownloadFileToFolderCdp.initDevTools(DownloadFileToFolderCdp.java:78)
        at com.codeborne.selenide.impl.DownloadFileToFolderCdp.prepareDownloadWithCdp(DownloadFileToFolderCdp.java:83)
        at com.codeborne.selenide.impl.DownloadFileToFolderCdp.download(DownloadFileToFolderCdp.java:49)
        at com.codeborne.selenide.commands.DownloadFile.execute(DownloadFile.java:75)
        at com.codeborne.selenide.commands.DownloadFile.execute(DownloadFile.java:29)
        at com.codeborne.selenide.commands.Commands.execute(Commands.java:161)
        at integration.FileDownloadToFolderWithCdpTest.downloadLargeFile(FileDownloadToFolderWithCdpTest.java:265)

You can add assumeThat(isFirefox()).isFalse() or something similar.

@britka
Copy link
Contributor Author

britka commented Dec 1, 2023

Ok. I'll add that

@britka
Copy link
Contributor Author

britka commented Dec 4, 2023

@asolntsev @BorisOsipov I will revert some changes.
In our case we should give file to user anyway. So I suggest to do the next
Variant 1.
When User chose FOLDER method we should check if it is chromium based browser then we apply CDP method
otherwise we continue using FOLDER .

Variant 2.
The same but for CDP method.

What do you think?

@BorisOsipov
Copy link
Member

@britka

When User chose FOLDER method we should check if it is chromium based browser then we apply CDP method
otherwise we continue using FOLDER .

If the user has chosen FOLDER, we should use the FOLDER method and not confuse the user with some of our predictions about how we can make the file available to them.

Copy link

sonarcloud bot commented Dec 5, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@britka
Copy link
Contributor Author

britka commented Dec 5, 2023

@BorisOsipov agree.

@britka
Copy link
Contributor Author

britka commented Dec 5, 2023

@asolntsev I fixed the tests according to your comments

@asolntsev asolntsev modified the milestones: 7.0.4, 7.1.0 Dec 7, 2023
}

private void initDevTools(Driver driver) {
if (driver.browser().isChromium()) {
Copy link
Member

Choose a reason for hiding this comment

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

I would replace driver.browser().isChromium() by driver.getWebDriver() instanceof HasDevTools

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@asolntsev I disagree with you. For example, FirefoxDriver is also implementing HasDevTools interface. But it doesn't support this functionality. It uses devtools v.85

Copy link
Member

Choose a reason for hiding this comment

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

I just tried - Firefox does download the file. Only command setDownloadBehavior doesn't work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I prefer to leave current implementation if you do not mind

Copy link
Member

Choose a reason for hiding this comment

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

I don't understand why we forbid this functionality in Firefox if it works.

Browser.downloadProgress(),
e -> {
downloadComplete.set(e.getState() == COMPLETED);
log.debug("Download is in progress");
Copy link
Member

Choose a reason for hiding this comment

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

I would log "Download is completed" if e.getState() == COMPLETED`.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I check this in other piece of code.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, but this log is lying. It says Download is in progress, but the download is actually completed.

@britka britka requested a review from asolntsev January 17, 2024 07:53
@asolntsev asolntsev self-assigned this Jan 18, 2024
Copy link
Member

@asolntsev asolntsev left a comment

Choose a reason for hiding this comment

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

LGTM

@britka Checkstyle warning:

Unused import at FileDownloadToFolderWithCdpTest.java:14:8

@britka
Copy link
Contributor Author

britka commented Jan 22, 2024

LGTM

@britka Checkstyle warning:

Unused import at FileDownloadToFolderWithCdpTest.java:14:8

@asolntsev Fixed unused imports

Copy link

sonarcloud bot commented Jan 22, 2024

Quality Gate Passed Quality Gate passed

Kudos, no new issues were introduced!

0 New issues
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@asolntsev asolntsev modified the milestones: 7.0.5, 7.1.0 Jan 25, 2024
@asolntsev asolntsev merged commit 72ba3ee into selenide:main Feb 2, 2024
11 of 13 checks passed
asolntsev added a commit that referenced this pull request Feb 3, 2024
…e it got "completed"

it seems that CDP continues sending events "inProgress" (14 of 14 bytes) even AFTER it already sent the "completed" event. We will just ignore them.
asolntsev added a commit that referenced this pull request Feb 3, 2024
…for N ms

this feature ("increment timout") was implemented only in FOLDER mode, now in CDP as well.
asolntsev added a commit that referenced this pull request Feb 3, 2024
CDP may send "in progress" events about multiple files simultaneously.
We need to track of all them, and pick the right file using given FileFilter.
asolntsev added a commit that referenced this pull request Feb 3, 2024
e.g. when Chrome is running in Selenoid
asolntsev added a commit that referenced this pull request Feb 3, 2024
…e it got "completed"

it seems that CDP continues sending events "inProgress" (14 of 14 bytes) even AFTER it already sent the "completed" event. We will just ignore them.
asolntsev added a commit that referenced this pull request Feb 3, 2024
…for N ms

this feature ("increment timout") was implemented only in FOLDER mode, now in CDP as well.
asolntsev added a commit that referenced this pull request Feb 3, 2024
CDP may send "in progress" events about multiple files simultaneously.
We need to track of all them, and pick the right file using given FileFilter.
asolntsev added a commit that referenced this pull request Feb 3, 2024
e.g. when Chrome is running in Selenoid
asolntsev added a commit that referenced this pull request Feb 6, 2024
asolntsev added a commit that referenced this pull request Feb 7, 2024
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.

Implement downloading files via CDP or BiDi
4 participants