Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/bidi/captureScreenshotParameters.js
2884 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
const { BoxClipRectangle, ElementClipRectangle } = require('./clipRectangle')
19
20
/**
21
* Defines the reference point from which to compute offsets for capturing screenshot.
22
*
23
* @enum {string}
24
*/
25
const Origin = {
26
VIEWPORT: 'viewport',
27
DOCUMENT: 'document',
28
}
29
30
/**
31
* Represents the optional parameters for capturing a screenshot.
32
* Described in https://w3c.github.io/webdriver-bidi/#command-browsingContext-captureScreenshot.
33
*/
34
class CaptureScreenshotParameters {
35
#map = new Map()
36
37
/**
38
* Sets the origin for capturing the screenshot.
39
*
40
* @param {Origin} origin - The origin for capturing the screenshot. Must be one of `Origin.VIEWPORT` or `Origin.DOCUMENT`.
41
* @returns {CaptureScreenshotParameters} - The current instance of the CaptureScreenshotParameters for chaining.
42
* @throws {Error} - If the provided origin is not valid.
43
*/
44
origin(origin) {
45
if (origin !== Origin.VIEWPORT && origin !== Origin.DOCUMENT) {
46
throw new Error(`Origin must be one of ${Object.values(Origin)}. Received:'${origin}'`)
47
}
48
this.#map.set('origin', origin)
49
return this
50
}
51
52
/**
53
* Sets the image format and quality for capturing a screenshot.
54
*
55
* @param {string} type - The image format type.
56
* @param {number} [quality] - The image quality (optional).
57
* @throws {Error} If the type is not a string or if the quality is not a number.
58
* @returns {CaptureScreenshotParameters} - The current instance of the CaptureScreenshotParameters for chaining.
59
*/
60
imageFormat(type, quality = undefined) {
61
if (typeof type !== 'string') {
62
throw new Error(`Type must be an instance of String. Received:'${type}'`)
63
}
64
65
this.#map.set('type', type)
66
67
if (quality !== undefined) {
68
if (typeof quality !== 'number') {
69
throw new Error(`Quality must be a number. Received:'${quality}'`)
70
}
71
this.#map.set('quality', quality)
72
}
73
return this
74
}
75
76
/**
77
* Sets the clip rectangle for capturing a screenshot.
78
*
79
* @param {BoxClipRectangle|ElementClipRectangle} clipRectangle - The clip rectangle to set.
80
* @throws {Error} If the clipRectangle is not an instance of ClipRectangle.
81
* @returns {CaptureScreenshotParameters} - The current instance of the CaptureScreenshotParameters for chaining.
82
*/
83
clipRectangle(clipRectangle) {
84
if (!(clipRectangle instanceof BoxClipRectangle || clipRectangle instanceof ElementClipRectangle)) {
85
throw new Error(`ClipRectangle must be an instance of ClipRectangle. Received:'${clipRectangle}'`)
86
}
87
this.#map.set('clip', Object.fromEntries(clipRectangle.asMap()))
88
return this
89
}
90
91
asMap() {
92
return this.#map
93
}
94
}
95
96
module.exports = { CaptureScreenshotParameters, Origin }
97
98