cypress-set-device-pixel-ratio
Version:
A Cypress custom command to set window.devicePixelRatio
34 lines (33 loc) • 1.64 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
require("cypress-wait-until");
;
Cypress.Commands.add('setDevicePixelRatio', function (pixelRatio, options) {
if (options === void 0) { options = {
mobile: false,
width: 0,
height: 0,
}; }
var overrideDeviceScaleFactor = function (scaleFactor) { return cy.wrap(Cypress.automation('remote:debugger:protocol', {
command: 'Emulation.setDeviceMetricsOverride',
params: __assign({ deviceScaleFactor: scaleFactor }, options),
})); };
var initialPixelRatio = window.devicePixelRatio;
// Set to an unlikely value to ensure that the override is working
var calibrationPixelRatio = 0.1;
return overrideDeviceScaleFactor(calibrationPixelRatio)
.then(function () { return cy.waitUntil(function () { return cy.wrap(window.devicePixelRatio.toFixed(3)).should('not.eq', initialPixelRatio.toFixed(3)); }); })
.then(function () { return overrideDeviceScaleFactor(pixelRatio / window.devicePixelRatio * calibrationPixelRatio); })
.then(function () { return cy.waitUntil(function () { return cy.wrap(window.devicePixelRatio.toFixed(3)).should('eq', pixelRatio.toFixed(3)); }, { interval: 10 }); });
});