-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add JavaScript examples for Remote WebDriver docs (#2562) #2829
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d7212bf
Add JavaScript examples for Remote WebDriver docs
diemol d0a4e81
Address Qodo review findings on Remote WebDriver JS examples
diemol 31d16da
Merge branch 'trunk' into js-remote-webdriver-examples
diemol c9c1843
Fix jar reference after 4.49.0 bump and harden grid readiness polling
diemol 94c2a46
Guard grid readiness polling against duplicate/unhandled retry events
diemol File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| const { spawn } = require('node:child_process') | ||
| const http = require('node:http') | ||
| const path = require('node:path') | ||
| const portprober = require('selenium-webdriver/net/portprober') | ||
|
|
||
| const SERVER_JAR = path.join(__dirname, '..', '..', '..', 'selenium-server-4.49.0.jar') | ||
|
|
||
| function waitForServer(url, timeout) { | ||
| const deadline = Date.now() + timeout | ||
|
|
||
| return new Promise((resolve, reject) => { | ||
| function retryOrFail() { | ||
| if (Date.now() > deadline) { | ||
| reject(new Error(`Selenium server did not start within ${timeout}ms`)) | ||
| return | ||
| } | ||
| setTimeout(attempt, 200) | ||
| } | ||
|
|
||
| function attempt() { | ||
| let settled = false | ||
| const retryOnce = () => { | ||
| if (settled) return | ||
| settled = true | ||
| retryOrFail() | ||
| } | ||
|
|
||
| const req = http.get(url, { timeout: 2000 }, (res) => { | ||
| let body = '' | ||
| res.on('data', (chunk) => (body += chunk)) | ||
| res.on('error', retryOnce) | ||
| res.on('aborted', retryOnce) | ||
| res.on('end', () => { | ||
| if (settled) return | ||
| try { | ||
| const { value } = JSON.parse(body) | ||
| if (res.statusCode === 200 && value && value.ready) { | ||
| settled = true | ||
| resolve() | ||
| return | ||
| } | ||
| } catch (err) { | ||
| // Not valid JSON yet (e.g. server still booting) - fall through to retry. | ||
| } | ||
| retryOnce() | ||
| }) | ||
| }) | ||
| req.on('timeout', () => { | ||
| req.destroy() | ||
| retryOnce() | ||
| }) | ||
| req.on('error', retryOnce) | ||
| } | ||
| attempt() | ||
| }) | ||
| } | ||
|
|
||
| async function startGrid() { | ||
| const port = await portprober.findFreePort() | ||
| const child = spawn('java', [ | ||
| '-jar', | ||
| SERVER_JAR, | ||
| 'standalone', | ||
| '--port', | ||
| String(port), | ||
| '--selenium-manager', | ||
| 'true', | ||
| '--enable-managed-downloads', | ||
| 'true', | ||
| '--log-level', | ||
| 'WARNING', | ||
| ]) | ||
|
|
||
| const url = `http://localhost:${port}` | ||
| try { | ||
| await waitForServer(`${url}/status`, 60000) | ||
| } catch (err) { | ||
| child.kill() | ||
| throw err | ||
| } | ||
|
|
||
| return { url, process: child } | ||
| } | ||
|
|
||
| function stopGrid(child) { | ||
| if (child) { | ||
| child.kill() | ||
| } | ||
| } | ||
|
|
||
| module.exports = { startGrid, stopGrid } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| const { Browser, Builder, By } = require('selenium-webdriver') | ||
| const Chrome = require('selenium-webdriver/chrome') | ||
| const remote = require('selenium-webdriver/remote') | ||
| const assert = require('node:assert') | ||
| const fs = require('node:fs') | ||
| const os = require('node:os') | ||
| const path = require('node:path') | ||
| const { startGrid, stopGrid } = require('./gridServer') | ||
|
|
||
| async function waitForFile(filePath, timeout) { | ||
| const deadline = Date.now() + timeout | ||
| while (!fs.existsSync(filePath)) { | ||
| if (Date.now() > deadline) { | ||
| throw new Error(`Timed out waiting for ${filePath} to be written`) | ||
| } | ||
| await new Promise((resolve) => setTimeout(resolve, 100)) | ||
| } | ||
| } | ||
|
|
||
| describe('Remote WebDriver Test', function () { | ||
| let driver | ||
| let gridUrl | ||
| let gridProcess | ||
|
|
||
| beforeEach(async function () { | ||
| const grid = await startGrid() | ||
| gridUrl = grid.url | ||
| gridProcess = grid.process | ||
| }) | ||
|
|
||
| afterEach(async function () { | ||
| try { | ||
| if (driver) { | ||
| await driver.quit() | ||
| driver = null | ||
| } | ||
| } finally { | ||
| stopGrid(gridProcess) | ||
| } | ||
| }) | ||
|
|
||
| it('Basic Example', async function () { | ||
| const options = new Chrome.Options().addArguments('--no-sandbox') | ||
| driver = new Builder().forBrowser(Browser.CHROME).setChromeOptions(options).usingServer(gridUrl).build() | ||
| }) | ||
|
|
||
| it('Uploads', async function () { | ||
| const options = new Chrome.Options().addArguments('--no-sandbox') | ||
| driver = new Builder().forBrowser(Browser.CHROME).setChromeOptions(options).usingServer(gridUrl).build() | ||
| await driver.get('https://the-internet.herokuapp.com/upload') | ||
| const uploadFile = path.resolve('./test/resources/selenium-snapshot.png') | ||
|
|
||
| await driver.setFileDetector(new remote.FileDetector()) | ||
| await driver.findElement(By.css('input[type=file]')).sendKeys(uploadFile) | ||
| await driver.findElement(By.id('file-submit')).click() | ||
|
|
||
| const fileName = await driver.findElement(By.id('uploaded-files')).getText() | ||
| assert.strictEqual(fileName, 'selenium-snapshot.png') | ||
| }) | ||
|
|
||
| it('Downloads', async function () { | ||
| const options = new Chrome.Options().addArguments('--no-sandbox').enableDownloads() | ||
| driver = new Builder().forBrowser(Browser.CHROME).setChromeOptions(options).usingServer(gridUrl).build() | ||
|
|
||
| const fileNames = ['file_1.txt', 'file_2.jpg'] | ||
| await driver.get('https://www.selenium.dev/selenium/web/downloads/download.html') | ||
| await driver.findElement(By.id('file-1')).click() | ||
| await driver.findElement(By.id('file-2')).click() | ||
| await driver.wait(async (d) => { | ||
| const downloaded = await d.getDownloadableFiles() | ||
| return fileNames.every((name) => downloaded.includes(name)) | ||
| }, 5000) | ||
|
|
||
| const files = await driver.getDownloadableFiles() | ||
| assert.deepStrictEqual(files.sort(), fileNames.sort()) | ||
|
|
||
| const targetDirectory = fs.mkdtempSync(path.join(os.tmpdir(), 'downloads-')) | ||
| const downloadableFile = files[0] | ||
| await driver.downloadFile(downloadableFile, targetDirectory) | ||
|
|
||
| const downloadedFilePath = path.join(targetDirectory, downloadableFile) | ||
| await waitForFile(downloadedFilePath, 5000) | ||
| const fileContent = fs.readFileSync(downloadedFilePath, 'utf8') | ||
| assert.strictEqual(fileContent.trim(), 'Hello, World!') | ||
|
|
||
| await driver.deleteDownloadableFiles() | ||
| assert.deepStrictEqual(await driver.getDownloadableFiles(), []) | ||
| }) | ||
|
|
||
| it('Browser specific functionalities', async function () { | ||
| const options = new Chrome.Options().addArguments('--no-sandbox') | ||
| driver = new Builder().forBrowser(Browser.CHROME).setChromeOptions(options).usingServer(gridUrl).build() | ||
|
|
||
| assert.ok(driver instanceof Chrome.Driver) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.