Skip to content

Commit

Permalink
Support for custom viewports and mobile / devices when running macOS …
Browse files Browse the repository at this point in the history
…Big Sur (#115)
  • Loading branch information
younglim authored Apr 14, 2023
1 parent 9a687c5 commit 367da2c
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions playwrightAxeGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import os from 'os';
import path from 'path';
import readline from 'readline';
import safe from 'safe-regex';
import { devices } from 'playwright';
import { consoleLogger, silentLogger } from './logs.js';

const playwrightAxeGenerator = async (domain, data) => {
Expand All @@ -26,7 +27,7 @@ const playwrightAxeGenerator = async (domain, data) => {
};
}

const { isHeadless, randomToken, deviceChosen, customDevice, viewportWidth } = data;
let { isHeadless, randomToken, deviceChosen, customDevice, viewportWidth } = data;
const block1 = `import { chromium, devices, webkit } from 'playwright';
import { createCrawleeSubFolders, runAxeScript } from '#root/crawlers/commonCrawlerFunc.js';
import { generateArtifacts } from '#root/mergeAxeResults.js';
Expand Down Expand Up @@ -263,22 +264,29 @@ const processPage = async page => {
try {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), appPrefix));

let bigSur = false;
let browser = "webkit";
let userAgentOpts = null;

// Compatibility workaround for macOS Big Sur
if (os.platform() ==='darwin' && os.release().startsWith("20.")) {
console.log(" ⚠️ Running custom scans is not fully supported on macOS Big Sur. Please upgrade to Monterey (12.0 and above) for a more reliable experience.");
browser = "chromium";
bigSur = "true";

if (deviceChosen === 'Mobile') {
customDevice = "iPhone 11";
}

if (customDevice) {
viewportWidth = devices[customDevice].viewport.width;
userAgentOpts = `--user-agent \"${devices[customDevice].userAgent}\"`;
}
}

let codegenCmd = `npx playwright codegen --target javascript -o ${tmpDir}/intermediateScript.js ${domain}`;
let extraCodegenOpts = `--browser ${browser} --block-service-workers --ignore-https-errors`;
let extraCodegenOpts = `${userAgentOpts} --browser ${browser} --block-service-workers --ignore-https-errors`;
let codegenResult;

if (bigSur) {
codegenResult = execSync(`${codegenCmd} ${extraCodegenOpts}`);
} else if (viewportWidth || customDevice === 'Specify viewport') {
if (viewportWidth || customDevice === 'Specify viewport') {
codegenResult = execSync(
`${codegenCmd} --viewport-size=${viewportWidth},720 ${extraCodegenOpts}`,
);
Expand Down

0 comments on commit 367da2c

Please sign in to comment.