Skip to content

Commit

Permalink
[JS] Adding support for print endpoint (#8880)
Browse files Browse the repository at this point in the history

Co-authored-by: David Burns <[email protected]>
  • Loading branch information
harsha509 and AutomatedTester authored Nov 24, 2020
1 parent 65563b8 commit 2f7727c
Show file tree
Hide file tree
Showing 6 changed files with 304 additions and 0 deletions.
69 changes: 69 additions & 0 deletions common/src/web/printPage.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<!--
~ Licensed to the Software Freedom Conservancy (SFC) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The SFC licenses this file
~ to you under the Apache License, Version 2.0 (the
~ "License"); you may not use this file except in compliance
~ with the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->

<!DOCTYPE html>
<html>
<body>

<table>
<tbody>
<tr>
<td>
<div class="page" style="line-height: 3;">
<br/> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc tincidunt metus eu consectetur rutrum. Praesent tempor facilisis dapibus. Aliquam cursus diam ac vehicula pulvinar. Integer lacinia non odio et condimentum. Aenean faucibus cursus
mi, sed interdum turpis sagittis a. Quisque quis pellentesque mi. Ut erat eros, posuere sed scelerisque ut, pharetra vitae tellus. Suspendisse ligula sapien, laoreet ac hendrerit sit amet, viverra vel mi. Pellentesque faucibus nisl et dolor
pharetra, vel mattis massa venenatis. Integer congue condimentum nisi, sed tincidunt velit tincidunt non. Nulla sagittis sed lorem pretium aliquam. Praesent consectetur volutpat nibh, quis pulvinar est volutpat id. Cras maximus odio posuere
suscipit venenatis. Donec rhoncus scelerisque metus, in tempus erat rhoncus sed. Morbi massa sapien, porttitor id urna vel, volutpat blandit velit. Cras sit amet sem eros. Quisque commodo facilisis tristique. Proin pellentesque sodales rutrum.
Vestibulum purus neque, congue vel dapibus in, venenatis ut felis. Donec et ligula enim. Sed sapien sapien, tincidunt vitae lectus quis, ultricies rhoncus mi. Nunc dapibus nulla tempus nunc interdum, sed facilisis ex pellentesque. Nunc vel
lorem leo. Cras pharetra sodales metus. Cras lacus ex, consequat at consequat vel, laoreet ac dui. Curabitur aliquam, sapien quis congue feugiat, nisi nisl feugiat diam, sed vehicula velit nulla ac nisl. Aliquam quis nisi euismod massa blandit
pharetra nec eget nunc. Etiam eros ante, auctor sit amet quam vel, fringilla faucibus leo. Morbi a pulvinar nulla. Praesent sed vulputate nisl. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aenean commodo
mollis iaculis. Maecenas consectetur enim vitae mollis venenatis. Ut scelerisque pretium orci id laoreet. In sit amet pharetra diam. Vestibulum in molestie lorem. Nunc gravida, eros non consequat fermentum, ex orci vestibulum orci, non accumsan
sem velit ac lectus. Vivamus malesuada lacus nec velit dignissim, ac fermentum nulla pretium. Aenean mi nisi, convallis sed tempor in, porttitor eu libero. Praesent et molestie ante. Duis suscipit vitae purus sit amet aliquam. Vestibulum lectus
justo, lobortis a purus a, dapibus efficitur metus. Suspendisse potenti. Duis dictum ex lorem. Suspendisse nec ligula consectetur magna hendrerit ullamcorper et eget mauris. Etiam vestibulum sodales diam, eget venenatis nunc luctus quis. Ut
fermentum placerat neque nec elementum. Praesent orci erat, rhoncus vitae est eu, dictum molestie metus. Cras et fermentum elit. Aenean eget augue lacinia, varius ante in, ullamcorper dolor. Cras viverra purus non egestas consectetur. Nulla
nec dolor ac lectus convallis aliquet sed a metus. Suspendisse eu imperdiet nunc, id pulvinar risus. Maecenas varius sagittis est, vel fermentum risus accumsan at. Vestibulum sollicitudin dui pharetra sapien volutpat, id convallis mi vestibulum.
Phasellus commodo sit amet lorem quis imperdiet. Proin nec diam sed urna euismod ultricies at sed urna. Quisque ornare, nulla et vehicula ultrices, massa purus vehicula urna, ac sodales lacus leo vitae mi. Sed congue placerat justo at placerat.
Aenean suscipit fringilla vehicula. Quisque iaculis orci vitae arcu commodo maximus. Maecenas nec nunc rutrum, cursus elit quis, porttitor sapien. Sed ac hendrerit ipsum, lacinia fringilla velit. Donec ultricies feugiat dictum.
</div>
</td>
</tr>
<tr>
<td>
<div class="page" style="line-height: 3;">
<br/> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc tincidunt metus eu consectetur rutrum. Praesent tempor facilisis dapibus. Aliquam cursus diam ac vehicula pulvinar. Integer lacinia non odio et condimentum. Aenean faucibus cursus
mi, sed interdum turpis sagittis a. Quisque quis pellentesque mi. Ut erat eros, posuere sed scelerisque ut, pharetra vitae tellus. Suspendisse ligula sapien, laoreet ac hendrerit sit amet, viverra vel mi. Pellentesque faucibus nisl et dolor
pharetra, vel mattis massa venenatis. Integer congue condimentum nisi, sed tincidunt velit tincidunt non. Nulla sagittis sed lorem pretium aliquam. Praesent consectetur volutpat nibh, quis pulvinar est volutpat id. Cras maximus odio posuere
suscipit venenatis. Donec rhoncus scelerisque metus, in tempus erat rhoncus sed. Morbi massa sapien, porttitor id urna vel, volutpat blandit velit. Cras sit amet sem eros. Quisque commodo facilisis tristique. Proin pellentesque sodales rutrum.
Vestibulum purus neque, congue vel dapibus in, venenatis ut felis. Donec et ligula enim. Sed sapien sapien, tincidunt vitae lectus quis, ultricies rhoncus mi. Nunc dapibus nulla tempus nunc interdum, sed facilisis ex pellentesque. Nunc vel
lorem leo. Cras pharetra sodales metus. Cras lacus ex, consequat at consequat vel, laoreet ac dui. Curabitur aliquam, sapien quis congue feugiat, nisi nisl feugiat diam, sed vehicula velit nulla ac nisl. Aliquam quis nisi euismod massa blandit
pharetra nec eget nunc. Etiam eros ante, auctor sit amet quam vel, fringilla faucibus leo. Morbi a pulvinar nulla. Praesent sed vulputate nisl. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aenean commodo
mollis iaculis. Maecenas consectetur enim vitae mollis venenatis. Ut scelerisque pretium orci id laoreet. In sit amet pharetra diam. Vestibulum in molestie lorem. Nunc gravida, eros non consequat fermentum, ex orci vestibulum orci, non accumsan
sem velit ac lectus. Vivamus malesuada lacus nec velit dignissim, ac fermentum nulla pretium. Aenean mi nisi, convallis sed tempor in, porttitor eu libero. Praesent et molestie ante. Duis suscipit vitae purus sit amet aliquam. Vestibulum lectus
justo, lobortis a purus a, dapibus efficitur metus. Suspendisse potenti. Duis dictum ex lorem. Suspendisse nec ligula consectetur magna hendrerit ullamcorper et eget mauris. Etiam vestibulum sodales diam, eget venenatis nunc luctus quis. Ut
fermentum placerat neque nec elementum. Praesent orci erat, rhoncus vitae est eu, dictum molestie metus. Cras et fermentum elit. Aenean eget augue lacinia, varius ante in, ullamcorper dolor. Cras viverra purus non egestas consectetur. Nulla
nec dolor ac lectus convallis aliquet sed a metus. Suspendisse eu imperdiet nunc, id pulvinar risus. Maecenas varius sagittis est, vel fermentum risus accumsan at. Vestibulum sollicitudin dui pharetra sapien volutpat, id convallis mi vestibulum.
Phasellus commodo sit amet lorem quis imperdiet. Proin nec diam sed urna euismod ultricies at sed urna. Quisque ornare, nulla et vehicula ultrices, massa purus vehicula urna, ac sodales lacus leo vitae mi. Sed congue placerat justo at placerat.
Aenean suscipit fringilla vehicula. Quisque iaculis orci vitae arcu commodo maximus. Maecenas nec nunc rutrum, cursus elit quis, porttitor sapien. Sed ac hendrerit ipsum, lacinia fringilla velit. Donec ultricies feugiat dictum.
</div>
</td>
</tr>
</tbody>
</table>
</body>
</html>
2 changes: 2 additions & 0 deletions javascript/node/selenium-webdriver/lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ const Name = {
TAKE_ELEMENT_SCREENSHOT: 'takeElementScreenshot',
SET_SCRIPT_TIMEOUT: 'setScriptTimeout',

PRINT_PAGE: 'printPage',

GET_TIMEOUT: 'getTimeout',
SET_TIMEOUT: 'setTimeout',

Expand Down
2 changes: 2 additions & 0 deletions javascript/node/selenium-webdriver/lib/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,8 @@ const W3C_COMMAND_MAP = new Map([
cmd.Name.TAKE_ELEMENT_SCREENSHOT,
get('/session/:sessionId/element/:id/screenshot'),
],
// print page.
[cmd.Name.PRINT_PAGE, post('/session/:sessionId/print')],
// Log extensions.
[cmd.Name.GET_LOG, post('/session/:sessionId/se/log')],
[cmd.Name.GET_AVAILABLE_LOG_TYPES, get('/session/:sessionId/se/log/types')],
Expand Down
1 change: 1 addition & 0 deletions javascript/node/selenium-webdriver/lib/test/fileserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ const Pages = (function () {
addPage('redirectPage', 'redirect')
addPage('resultPage', 'resultPage.html')
addPage('richTextPage', 'rich_text.html')
addPage('printPage', 'printPage.html')
addPage('selectableItemsPage', 'selectableItems.html')
addPage('selectPage', 'selectPage.html')
addPage('simpleTestPage', 'simpleTest.html')
Expand Down
101 changes: 101 additions & 0 deletions javascript/node/selenium-webdriver/lib/webdriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,25 @@ class IWebDriver {
* instance.
*/
switchTo() {}

/**
*
* Takes a PDF of the current page. The driver makes a best effort to
* return a PDF based on the provided parameters.
*
* @param {{orientation: (string|undefined),
* scale: (number|undefined),
* background: (boolean|undefined)
* width: (number|undefined)
* height: (number|undefined)
* top: (number|undefined)
* bottom: (number|undefined)
* left: (number|undefined)
* right: (number|undefined)
* shrinkToFit: (boolean|undefined)
* pageRanges: (<Array>|undefined)}} options.
*/
printPage(options) {} // eslint-disable-line
}

/**
Expand Down Expand Up @@ -1049,6 +1068,88 @@ class WebDriver {
switchTo() {
return new TargetLocator(this)
}

validatePrintPageParams(keys, object) {
let page = {}
let margin = {}
let data
Object.keys(keys).forEach(function (key) {
data = keys[key]
let obj = {
orientation: function () {
object.orientation = data
},

scale: function () {
object.scale = data
},

background: function () {
object.background = data
},

width: function () {
page.width = data
object.page = page
},

height: function () {
page.height = data
object.page = page
},

top: function () {
margin.top = data
object.margin = margin
},

left: function () {
margin.left = data
object.margin = margin
},

bottom: function () {
margin.bottom = data
object.margin = margin
},

right: function () {
margin.right = data
object.margin = margin
},

shrinkToFit: function () {
object.shrinkToFit = data
},

pageRanges: function () {
object.pageRanges = data
},
}

if (!Object.prototype.hasOwnProperty.call(obj, key)) {
throw new error.InvalidArgumentError(`Invalid Argument '${key}'`)
} else {
obj[key]()
}
})

return object
}

/** @override */
printPage(options = {}) {
let keys = options
let params = {}
let resultObj

let self = this
resultObj = self.validatePrintPageParams(keys, params)

return this.execute(
new command.Command(command.Name.PRINT_PAGE).setParameters(resultObj)
)
}
}

/**
Expand Down
129 changes: 129 additions & 0 deletions javascript/node/selenium-webdriver/test/print_pdf_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

'use strict'

const test = require('../lib/test')
const { Pages } = require('../lib/test')
const { Browser } = require('../')
const chrome = require('../chrome')
const assert = require('assert')

let startIndex = 0
let endIndex = 5
let pdfMagicNumber = 'JVBER'
let base64Code

test.suite(
function (env) {
let driver

afterEach(function () {
return driver.quit()
})

it('Should Print pdf with 2 pages', async function () {
driver = env.builder().build()
await driver.get(Pages.printPage)
base64Code = await driver.printPage({ pageRanges: ['1-2'] })
base64Code = base64Code.slice(startIndex, endIndex)
assert.equal(base64Code, pdfMagicNumber)
})

it('Should Print pdf with total pages', async function () {
driver = env.builder().build()
await driver.get(Pages.printPage)
base64Code = await driver.printPage()
base64Code = base64Code.slice(startIndex, endIndex)
assert.equal(base64Code, pdfMagicNumber)
})

it('Check with all valid params', async function () {
driver = env.builder().build()
await driver.get(Pages.printPage)
base64Code = await driver.printPage({
orientation: 'landscape',
scale: 1,
background: true,
width: 30,
height: 30,
top: 1,
bottom: 1,
left: 1,
right: 1,
shrinkToFit: true,
pageRanges: ['1-2'],
})
base64Code = base64Code.slice(startIndex, endIndex)
assert.equal(base64Code, pdfMagicNumber)
})

it('Check with page params', async function () {
driver = env.builder().build()
await driver.get(Pages.printPage)
base64Code = await driver.printPage({ width: 30, height: 30 })
base64Code = base64Code.slice(startIndex, endIndex)
assert.equal(base64Code, pdfMagicNumber)
})

it('Check with margin params', async function () {
driver = env.builder().build()
await driver.get(Pages.printPage)
base64Code = await driver.printPage({
top: 1,
bottom: 1,
left: 1,
right: 1,
})
base64Code = base64Code.slice(startIndex, endIndex)
assert.equal(base64Code, pdfMagicNumber)
})
},
{ browsers: [Browser.FIREFOX] }
)

// in chrome printPdf supports in headless mode
test.suite(
function (env) {
let driver

let options = new chrome.Options()
options.headless()

afterEach(function () {
return driver.quit()
})

it('Should Print pdf with 2 pages', async function () {
driver = env.builder().setChromeOptions(options).build()

await driver.get(Pages.printPage)
base64Code = await driver.printPage({ pageRanges: ['1-2'] })
base64Code = base64Code.slice(startIndex, endIndex)
assert.equal(base64Code, pdfMagicNumber)
})

it('Should Print pdf with total pages', async function () {
driver = env.builder().setChromeOptions(options).build()
await driver.get(Pages.printPage)
base64Code = await driver.printPage()
base64Code = base64Code.slice(startIndex, endIndex)
assert.equal(base64Code, pdfMagicNumber)
})
},
{ browsers: [Browser.CHROME] }
)

0 comments on commit 2f7727c

Please sign in to comment.