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

[JS] Adding support for print endpoint #8880

Merged
merged 5 commits into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
161 changes: 161 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,148 @@ 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 () {
if (data && data !== 'landscape' && data !== 'portrait') {
throw new error.InvalidArgumentError(
`Invalid argument '${data}'. It should be either 'landscape' (or) 'portrait'`
)
} else {
object.orientation = data
}
},

scale: function () {
if (data >= 0.1 && data <= 2 && typeof data === 'number' && data) {
object.scale = data
} else {
throw new error.InvalidArgumentError(
`Invalid value '${data}' for argument '${key}'. It should be in between > 0.1 and < 2`
)
}
},

background: function () {
if (data && data !== true && data !== false) {
throw new error.InvalidArgumentError(
`Invalid value '${data}' for argument '${key}'. It should be either "true" (or) "false"`
)
} else {
object.background = data
}
},

width: function () {
if (data && typeof data === 'number' && data > 0) {
page.width = data
object.page = page
} else {
throw new error.InvalidArgumentError(
`Invalid value '${data}' for argument '${key}'. The width should be of type 'integer' and > 0`
)
}
},

height: function () {
if (data && typeof data === 'number' && data > 0) {
page.height = data
object.page = page
} else {
throw new error.InvalidArgumentError(
`Invalid value '${data}' for argument '${key}'. The height should be of type 'integer' and > 0`
)
}
},

top: function () {
if (data && typeof data === 'number' && data > 0) {
margin.top = data
object.margin = margin
} else {
throw new error.InvalidArgumentError(
`Invalid value '${data}' for argument '${key}'. It should be of type 'integer' and > 0`
)
}
},

left: function () {
if (data && typeof data === 'number' && data > 0) {
Copy link
Member

Choose a reason for hiding this comment

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

We have this repeated a few times, we should probably extract that into it's own

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you for review @AutomatedTester !

error message are returned by endpoint already. Removing custom error throws

margin.left = data
object.margin = margin
} else {
throw new error.InvalidArgumentError(
`Invalid value '${data}' for argument '${key}'. It should be of type 'integer' and > 0`
)
}
},

bottom: function () {
if (data && typeof data === 'number' && data > 0) {
margin.bottom = data
object.margin = margin
} else {
throw new error.InvalidArgumentError(
`Invalid value '${data}' for argument '${key}'. It should be of type 'integer' and > 0`
)
}
},

right: function () {
if (data && typeof data === 'number' && data > 0) {
margin.right = data
object.margin = margin
} else {
throw new error.InvalidArgumentError(
`Invalid value '${data}' for argument '${key}'. It should be of type 'integer' and > 0`
)
}
},

shrinkToFit: function () {
if (data && data !== true && data !== false) {
throw new error.InvalidArgumentError(
`Invalid value '${data}' for argument '${key}'. It should be either "true" (or) "false"`
)
} else {
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
Loading