Skip to content

Commit

Permalink
[bidi][js] Update the capture screenshot APIs to include all paramete…
Browse files Browse the repository at this point in the history
…rs and remove scroll parameter
  • Loading branch information
pujagani committed Mar 27, 2024
1 parent 1f058a8 commit 6c23e5e
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 12 deletions.
24 changes: 19 additions & 5 deletions javascript/node/selenium-webdriver/bidi/browsingContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const { InvalidArgumentError, NoSuchFrameError } = require('../lib/error')
const { BrowsingContextInfo } = require('./browsingContextTypes')
const { SerializationOptions, ReferenceValue, RemoteValue } = require('./protocolValue')
const { WebElement } = require('../lib/webdriver')
const { CaptureScreenshotParameters } = require('./captureScreenshotParameters')

class Locator {
static Type = Object.freeze({
Expand Down Expand Up @@ -216,6 +217,23 @@ class BrowsingContext {
return response['result']['data']
}

async captureScreenshot(captureScreenshotParameters) {
if (!(captureScreenshotParameters instanceof CaptureScreenshotParameters)) {
throw new Error(`Pass in a CaptureScreenshotParameters object. Received: ${captureScreenshotParameters}`)
}

const screenshotParams = captureScreenshotParameters.asMap()
screenshotParams.set('context', this._id)
let params = {
method: 'browsingContext.captureScreenshot',
params: Object.fromEntries(screenshotParams),
}

const response = await this.bidi.send(params)
this.checkErrorInScreenshot(response)
return response['result']['data']
}

async captureBoxScreenshot(x, y, width, height) {
let params = {
method: 'browsingContext.captureScreenshot',
Expand All @@ -231,15 +249,12 @@ class BrowsingContext {
},
}

console.log(JSON.stringify(params))

const response = await this.bidi.send(params)
console.log(JSON.stringify(response))
this.checkErrorInScreenshot(response)
return response['result']['data']
}

async captureElementScreenshot(sharedId, handle = undefined, scrollIntoView = undefined) {
async captureElementScreenshot(sharedId, handle = undefined) {
let params = {
method: 'browsingContext.captureScreenshot',
params: {
Expand All @@ -250,7 +265,6 @@ class BrowsingContext {
sharedId: sharedId,
handle: handle,
},
scrollIntoView: scrollIntoView,
},
},
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 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.

const { BoxClipRectangle, ElementClipRectangle } = require('./clipRectangle')
const Origin = {
VIEWPORT: 'viewport',
DOCUMENT: 'document',
}

class CaptureScreenshotParameters {
#map = new Map()

origin(origin) {
if (origin !== Origin.VIEWPORT && origin !== Origin.DOCUMENT) {
throw new Error(`Origin must be one of ${Object.values(Origin)}. Received:'${origin}'`)
}
this.#map.set('origin', origin)
return this
}

imageFormat(type, quality = undefined) {
if (typeof type !== 'string') {
throw new Error(`Type must be an instance of String. Received:'${type}'`)
}

this.#map.set('type', type)

if (quality !== undefined) {
if (typeof quality !== 'number') {
throw new Error(`Quality must be a number. Received:'${quality}'`)
}
this.#map.set('quality', quality)
}
return this
}

clipRectangle(clipRectangle) {
if (!(clipRectangle instanceof BoxClipRectangle || clipRectangle instanceof ElementClipRectangle)) {
throw new Error(`ClipRectangle must be an instance of ClipRectangle. Received:'${clipRectangle}'`)
}
this.#map.set('clip', Object.fromEntries(clipRectangle.asMap()))
return this
}

asMap() {
return this.#map
}
}

module.exports = { CaptureScreenshotParameters, Origin }
87 changes: 87 additions & 0 deletions javascript/node/selenium-webdriver/bidi/clipRectangle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// 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.

class ClipRectangle {
type

constructor(type) {
this.type = type
}

get type() {
return this.type
}

asMap() {}
}

class ElementClipRectangle extends ClipRectangle {
#sharedId
#handleId

constructor(sharedId, handleId = undefined) {
super('element')
this.#sharedId = sharedId

if (handleId !== undefined) {
this.#handleId = handleId
}
}

asMap() {
const map = new Map()
map.set('type', super.type)

const sharedReference = new Map()
sharedReference.set('sharedId', this.#sharedId)
if (this.#handleId !== undefined) {
sharedReference.set('handleId', this.#handleId)
}

map.set('element', Object.fromEntries(sharedReference))

return map
}
}

class BoxClipRectangle extends ClipRectangle {
#x
#y
#width
#height

constructor(x, y, width, height) {
super('box')
this.#x = x
this.#y = y
this.#width = width
this.#height = height
}

asMap() {
const map = new Map()
map.set('type', super.type)
map.set('x', this.#x)
map.set('y', this.#y)
map.set('width', this.#width)
map.set('height', this.#height)

return map
}
}

module.exports = { BoxClipRectangle, ElementClipRectangle }
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ const { Browser, By } = require('../../')
const { Pages, suite } = require('../../lib/test')
const BrowsingContext = require('../../bidi/browsingContext')
const until = require('../../lib/until')
const {
CaptureScreenshotParameter,
Origin,
CaptureScreenshotParameters,
} = require('../../bidi/captureScreenshotParameters')
const { BoxClipRectangle, ElementClipRectangle } = require('../../bidi/clipRectangle')

suite(
function (env) {
Expand Down Expand Up @@ -190,19 +196,22 @@ suite(
assert.equal(base64code, pngMagicNumber)
})

it('can take box screenshot', async function () {
it('can take screenshot with all parameters for box screenshot', async function () {
const id = await driver.getWindowHandle()
const browsingContext = await BrowsingContext(driver, {
browsingContextId: id,
})

const response = await browsingContext.captureBoxScreenshot(5, 5, 10, 10)
let captureScreenshotParams = new CaptureScreenshotParameters()
captureScreenshotParams.origin(Origin.VIEWPORT).clipRectangle(new BoxClipRectangle(5, 5, 10, 10))

const response = await browsingContext.captureScreenshot(captureScreenshotParams)

const base64code = response.slice(startIndex, endIndex)
assert.equal(base64code, pngMagicNumber)
})

it('can take element screenshot', async function () {
it('can take screenshot with all parameters for element screenshot', async function () {
const id = await driver.getWindowHandle()
const browsingContext = await BrowsingContext(driver, {
browsingContextId: id,
Expand All @@ -211,22 +220,38 @@ suite(
await driver.get(Pages.formPage)
const element = await driver.findElement(By.id('checky'))
const elementId = await element.getId()
const response = await browsingContext.captureElementScreenshot(elementId)

let captureScreenshotParams = new CaptureScreenshotParameters()
captureScreenshotParams.origin(Origin.VIEWPORT).clipRectangle(new ElementClipRectangle(elementId))

const response = await browsingContext.captureScreenshot(captureScreenshotParams)

const base64code = response.slice(startIndex, endIndex)
assert.equal(base64code, pngMagicNumber)
})

it('can take box screenshot', async function () {
const id = await driver.getWindowHandle()
const browsingContext = await BrowsingContext(driver, {
browsingContextId: id,
})

const response = await browsingContext.captureBoxScreenshot(5, 5, 10, 10)

const base64code = response.slice(startIndex, endIndex)
assert.equal(base64code, pngMagicNumber)
})

it('can scroll and take element screenshot', async function () {
it('can take element screenshot', async function () {
const id = await driver.getWindowHandle()
const browsingContext = await BrowsingContext(driver, {
browsingContextId: id,
})

await driver.get(Pages.formPage)
const element = await driver.findElement(By.id('checkbox-with-label'))
const element = await driver.findElement(By.id('checky'))
const elementId = await element.getId()
const response = await browsingContext.captureElementScreenshot(elementId, undefined, true)
const response = await browsingContext.captureElementScreenshot(elementId)

const base64code = response.slice(startIndex, endIndex)
assert.equal(base64code, pngMagicNumber)
Expand Down

0 comments on commit 6c23e5e

Please sign in to comment.