8000 feat: allow to screenshoot custom area by OldSkyTree · Pull Request #105 · gemini-testing/gemini-core · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Sep 21, 2022. It is now read-only.
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
25 changes: 16 additions & 9 deletions lib/browser/client-scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ exports.queryFirst = lib.queryFirst;
// - elementCaptureRect - sum of extRects of the element and its pseudo-elements
// - captureRect - sum of all elementCaptureRect for each captureSelectors

exports.prepareScreenshot = function prepareScreenshot(selectors, opts) {
exports.prepareScreenshot = function prepareScreenshot(areas, opts) {
opts = opts || {};
try {
return prepareScreenshotUnsafe(selectors, opts);
return prepareScreenshotUnsafe(areas, opts);
} catch (e) {
return {
error: 'JS',
Expand All @@ -33,7 +33,7 @@ exports.prepareScreenshot = function prepareScreenshot(selectors, opts) {
}
};

function prepareScreenshotUnsafe(selectors, opts) {
function prepareScreenshotUnsafe(areas, opts) {
var allowViewportOverflow = opts.allowViewportOverflow;
var captureElementFromTop = opts.captureElementFromTop;
var scrollElem = window;
Expand All @@ -50,10 +50,17 @@ function prepareScreenshotUnsafe(selectors, opts) {
}
}

var rect = getCaptureRect(selectors, {allowViewportOverflow: allowViewportOverflow, scrollElem: scrollElem});
if (rect.error) {
return rect;
}
var rect, selectors = [];

areas.forEach(function(area) {
if (Rect.isRect(area)) {
rect = rect ? rect.merge(new Rect(area)) : new Rect(area);
} else {
selectors.push(area);
}
});

rect = getCaptureRect(selectors, {allowViewportOverflow: allowViewportOverflow, scrollElem: scrollElem}, rect);

var viewportHeight = document.documentElement.clientHeight,
viewportWidth = document.documentElement.clientWidth,
Expand Down Expand Up @@ -115,8 +122,8 @@ exports.resetZoom = function() {
meta.content = 'width=device-width,initial-scale=1.0,user-scalable=no';
};

function getCaptureRect(selectors, opts) {
var element, elementRect, rect;
function getCaptureRect(selectors, opts, initialRect) {
var element, elementRect, rect = initialRect;
for (var i = 0; i < selectors.length; i++) {
element = lib.queryFirst(selectors[i]);
if (!element) {
Expand Down
11 changes: 11 additions & 0 deletions lib/browser/client-scripts/rect.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ function Rect(data) {
}
}

Rect.isRect = function(data) {
if (typeof data !== 'object' || data === null || Array.isArray(data)) {
return false;
}

return 'left' in data && 'top' in data && (
'width' in data && 'height' in data ||
'right' in data && 'bottom' in data
);
};

Rect.prototype = {
constructor: Rect,
merge: function(otherRect) {
Expand Down
80 changes: 80 additions & 0 deletions test/lib/browser/client-scripts/rect.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,86 @@ describe('Rect', () => {
});
});

describe('isRect', () => {
it('should return false if argument is not an object', () => {
assert.isFalse(Rect.isRect('foo'));
});

it('should return false if argument is null', () => {
assert.isFalse(Rect.isRect(null));
});

it('should return false if argument is array', () => {
assert.isFalse(Rect.isRect([]));
});

it('should return false if argument has no left property', () => {
assert.isFalse(Rect.isRect({
top: 1,
width: 1,
height: 1
}));
});

it('should return false if argument has no top property', () => {
assert.isFalse(Rect.isRect({
left: 1,
width: 1,
height: 1
}));
});

it('should return false if argument has no width property, but has height', () => {
assert.isFalse(Rect.isRect({
left: 1,
top: 1,
height: 1
}));
});

it('should return false if argument has no height property, but has width', () => {
assert.isFalse(Rect.isRect({
left: 1,
top: 1,
width: 1
}));
});

it('should return false if argument has no right property, but has bottom', () => {
assert.isFalse(Rect.isRect({
left: 1,
top: 1,
bottom: 1
}));
});

it('should return false if argument has no bottom property, but has right', () => {
assert.isFalse(Rect.isRect({
left: 1,
top: 1,
right: 1
}));
});

it('should return true if argument has left, top, width, height properties', () => {
assert.isTrue(Rect.isRect({
left: 1,
top: 1,
width: 1,
height: 1
}));
});

it('should return true if argument has left, top, right, bottom properties', () => {
assert.isTrue(Rect.isRect({
left: 1,
top: 1,
right: 1,
bottom: 1
}));
});
});

describe('rectInside', () => {
it('should return true when rect is inside', () => {
assert.isTrue(rect.rectInside(
Expand Down
0