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

Fix takeScreenshot command not returning anything. #3769

Merged
merged 3 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 7 additions & 5 deletions lib/api/web-element/commands/takeScreenshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
* @example
* export default {
* demoTest(browser: NightwatchAPI): void {
* const data = browser.element('#main').takeScreenshot();
* require('fs').writeFile('out.png', data, 'base64');
* const screenshot = browser.element('#main').takeScreenshot();
* screenshot.then((screenshotData) => {
* require('fs').writeFile('out.png', screenshotData, 'base64');
* });
* },
*
* async demoTestAsync(browser: NightwatchAPI): Promise<void> {
* const data = await browser.element('#main').takeScreenshot();
* require('fs').writeFile('out.png', data, 'base64');
* const screenshotData = await browser.element('#main').takeScreenshot();
* require('fs').writeFile('out.png', screenshotData, 'base64');
* }
* }
*
Expand All @@ -23,5 +25,5 @@
* @returns {ScopedValue<string>}
*/
module.exports.command = function() {
return this.runQueuedCommand('takeElementScreenshot');
return this.runQueuedCommandScoped('takeElementScreenshot');
};
30 changes: 21 additions & 9 deletions test/src/api/commands/web-element/testTakeScreenshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,18 @@ describe('element().takeScreenshot() command', function () {
}, true);

const resultPromise = this.client.api.element('#signupSection').takeScreenshot();
// neither an instance of Element or Promise, but an instance of ScopedWebElement.
assert.strictEqual(resultPromise instanceof Element, false);
assert.strictEqual(typeof resultPromise.find, 'undefined');

assert.strictEqual(resultPromise instanceof Promise, false);
assert.strictEqual(typeof resultPromise.then, 'function');

const result = await resultPromise;
assert.strictEqual(result instanceof WebElement, true);
assert.strictEqual(await result.getId(), '0');
assert.strictEqual(result instanceof WebElement, false);
assert.strictEqual(result, 'iVBORw0KGgoAAAANSUhEUgAAAbsAAAAiCAYAAADGd3chAAABK');

const resultValue = await resultPromise.value;
assert.strictEqual(resultValue, 'iVBORw0KGgoAAAANSUhEUgAAAbsAAAAiCAYAAADGd3chAAABK');
});

it('test .element().find().takeScreenshot()', async function() {
Expand All @@ -45,14 +49,18 @@ describe('element().takeScreenshot() command', function () {
}, true);

const resultPromise = this.client.api.element('#signupSection').find('#helpBtn').takeScreenshot();
// neither an instance of Element or Promise, but an instance of ScopedWebElement.
assert.strictEqual(resultPromise instanceof Element, false);
assert.strictEqual(typeof resultPromise.find, 'undefined');

assert.strictEqual(resultPromise instanceof Promise, false);
assert.strictEqual(typeof resultPromise.then, 'function');

const result = await resultPromise;
assert.strictEqual(result instanceof WebElement, true);
assert.strictEqual(await result.getId(), '1');
assert.strictEqual(result instanceof WebElement, false);
assert.strictEqual(result, 'iVBORw0KGgoAAAANSUhEUgAAAbsAAAAiCAYAAADGd3chAAABK');

const resultValue = await resultPromise.value;
assert.strictEqual(resultValue, 'iVBORw0KGgoAAAANSUhEUgAAAbsAAAAiCAYAAADGd3chAAABK');
});

it('test .element.find().takeScreenshot()', async function() {
Expand All @@ -66,13 +74,17 @@ describe('element().takeScreenshot() command', function () {
}, true);

const resultPromise = this.client.api.element.find('#signupSection').takeScreenshot();
// neither an instance of Element or Promise, but an instance of ScopedWebElement.
assert.strictEqual(resultPromise instanceof Element, false);
assert.strictEqual(typeof resultPromise.find, 'undefined');

assert.strictEqual(resultPromise instanceof Promise, false);
assert.strictEqual(typeof resultPromise.then, 'function');

const result = await resultPromise;
assert.strictEqual(result instanceof WebElement, true);
assert.strictEqual(await result.getId(), '0');
assert.strictEqual(result instanceof WebElement, false);
assert.strictEqual(result, 'iVBORw0KGgoAAAANSUhEUgAAAbsAAAAiCAYAAADGd3chAAABK');

const resultValue = await resultPromise.value;
assert.strictEqual(resultValue, 'iVBORw0KGgoAAAANSUhEUgAAAbsAAAAiCAYAAADGd3chAAABK');
});
});
1 change: 1 addition & 0 deletions types/tests/webElement.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ describe('new element() api', function () {
expectType<ElementValue<string>>(elem.getAccessibleName());
expectType<ElementValue<string>>(elem.getAriaRole());
expectType<ElementValue<string>>(elem.getCssProperty('height'));
expectType<ElementValue<string>>(elem.takeScreenshot());
vaibhavsingh97 marked this conversation as resolved.
Show resolved Hide resolved

expectType<Promise<WebElement>>(elem.click());
expectType<Promise<WebElement>>(elem.clear());
Expand Down
2 changes: 1 addition & 1 deletion types/web-element.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export interface ScopedElement extends Element, PromiseLike<WebElement> {

setAttribute(name: string, value: string | null): Promise<WebElement>;

takeScreenshot(shouldBeInView?: boolean): ElementValue<string>;
garg3133 marked this conversation as resolved.
Show resolved Hide resolved
takeScreenshot(): ElementValue<string>;

dragAndDrop(destination: DragAndDropDestination): Promise<WebElement>;

Expand Down