Path: blob/trunk/javascript/selenium-webdriver/bidi/captureScreenshotParameters.js
2884 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617const { BoxClipRectangle, ElementClipRectangle } = require('./clipRectangle')1819/**20* Defines the reference point from which to compute offsets for capturing screenshot.21*22* @enum {string}23*/24const Origin = {25VIEWPORT: 'viewport',26DOCUMENT: 'document',27}2829/**30* Represents the optional parameters for capturing a screenshot.31* Described in https://w3c.github.io/webdriver-bidi/#command-browsingContext-captureScreenshot.32*/33class CaptureScreenshotParameters {34#map = new Map()3536/**37* Sets the origin for capturing the screenshot.38*39* @param {Origin} origin - The origin for capturing the screenshot. Must be one of `Origin.VIEWPORT` or `Origin.DOCUMENT`.40* @returns {CaptureScreenshotParameters} - The current instance of the CaptureScreenshotParameters for chaining.41* @throws {Error} - If the provided origin is not valid.42*/43origin(origin) {44if (origin !== Origin.VIEWPORT && origin !== Origin.DOCUMENT) {45throw new Error(`Origin must be one of ${Object.values(Origin)}. Received:'${origin}'`)46}47this.#map.set('origin', origin)48return this49}5051/**52* Sets the image format and quality for capturing a screenshot.53*54* @param {string} type - The image format type.55* @param {number} [quality] - The image quality (optional).56* @throws {Error} If the type is not a string or if the quality is not a number.57* @returns {CaptureScreenshotParameters} - The current instance of the CaptureScreenshotParameters for chaining.58*/59imageFormat(type, quality = undefined) {60if (typeof type !== 'string') {61throw new Error(`Type must be an instance of String. Received:'${type}'`)62}6364this.#map.set('type', type)6566if (quality !== undefined) {67if (typeof quality !== 'number') {68throw new Error(`Quality must be a number. Received:'${quality}'`)69}70this.#map.set('quality', quality)71}72return this73}7475/**76* Sets the clip rectangle for capturing a screenshot.77*78* @param {BoxClipRectangle|ElementClipRectangle} clipRectangle - The clip rectangle to set.79* @throws {Error} If the clipRectangle is not an instance of ClipRectangle.80* @returns {CaptureScreenshotParameters} - The current instance of the CaptureScreenshotParameters for chaining.81*/82clipRectangle(clipRectangle) {83if (!(clipRectangle instanceof BoxClipRectangle || clipRectangle instanceof ElementClipRectangle)) {84throw new Error(`ClipRectangle must be an instance of ClipRectangle. Received:'${clipRectangle}'`)85}86this.#map.set('clip', Object.fromEntries(clipRectangle.asMap()))87return this88}8990asMap() {91return this.#map92}93}9495module.exports = { CaptureScreenshotParameters, Origin }969798