Skip to content

Commit b1cd355

Browse files
[js][bidi] Added printPage command (#12124)
* [js][bidi] Added printPage command with tests * [js][bidi] Corrected default value for height --------- Co-authored-by: Sri Harsha <[email protected]>
1 parent 178c60e commit b1cd355

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

javascript/node/selenium-webdriver/bidi/browsingContext.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,42 @@ class BrowsingContext {
129129
}
130130
await this.bidi.send(params)
131131
}
132+
133+
/**
134+
* Prints PDF of the webpage
135+
* @param options print options given by the user
136+
* @returns PrintResult object
137+
*/
138+
async printPage(options = {}) {
139+
let params = {
140+
method: 'browsingContext.print',
141+
// Setting default values for parameters
142+
params: {
143+
context: this._id,
144+
background: false,
145+
margin: {
146+
bottom: 1.0,
147+
left: 1.0,
148+
right: 1.0,
149+
top: 1.0,
150+
},
151+
orientation: 'portrait',
152+
page: {
153+
height: 27.94,
154+
width: 21.59,
155+
},
156+
pageRanges: [],
157+
scale: 1.0,
158+
shrinkToFit: true,
159+
},
160+
}
161+
162+
// Updating parameter values based on the options passed
163+
params.params = this._driver.validatePrintPageParams(options, params.params)
164+
165+
const response = await this.bidi.send(params)
166+
return new PrintResult(response.result.data)
167+
}
132168
}
133169

134170
class NavigateResult {
@@ -146,6 +182,16 @@ class NavigateResult {
146182
}
147183
}
148184

185+
class PrintResult {
186+
constructor(data) {
187+
this._data = data
188+
}
189+
190+
get data() {
191+
return this._data
192+
}
193+
}
194+
149195
/**
150196
* initiate browsing context instance and return
151197
* @param driver

javascript/node/selenium-webdriver/test/bidi/bidi_test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,10 @@ suite(
297297
})
298298

299299
describe('Browsing Context', function () {
300+
let startIndex = 0
301+
let endIndex = 5
302+
let pdfMagicNumber = 'JVBER'
303+
300304
it('can create a browsing context for given id', async function () {
301305
const id = await driver.getWindowHandle()
302306
const browsingContext = await BrowsingContext(driver, {
@@ -398,6 +402,44 @@ suite(
398402
})
399403
await assert.rejects(window2.getTree(), { message: 'no such frame' })
400404
})
405+
406+
it('can print PDF with total pages', async function () {
407+
const id = await driver.getWindowHandle()
408+
const browsingContext = await BrowsingContext(driver, {
409+
browsingContextId: id,
410+
})
411+
412+
await driver.get(Pages.printPage)
413+
const result = await browsingContext.printPage()
414+
415+
let base64Code = result.data.slice(startIndex, endIndex)
416+
assert.strictEqual(base64Code, pdfMagicNumber)
417+
})
418+
419+
it('can print PDF with all valid parameters', async function () {
420+
const id = await driver.getWindowHandle()
421+
const browsingContext = await BrowsingContext(driver, {
422+
browsingContextId: id,
423+
})
424+
425+
await driver.get(Pages.printPage)
426+
const result = await browsingContext.printPage({
427+
orientation: 'landscape',
428+
scale: 1,
429+
background: true,
430+
width: 30,
431+
height: 30,
432+
top: 1,
433+
bottom: 1,
434+
left: 1,
435+
right: 1,
436+
shrinkToFit: true,
437+
pageRanges: ['1-2'],
438+
})
439+
440+
let base64Code = result.data.slice(startIndex, endIndex)
441+
assert.strictEqual(base64Code, pdfMagicNumber)
442+
})
401443
})
402444

403445
describe('Browsing Context Inspector', function () {

0 commit comments

Comments
 (0)