diff --git a/bin/command.js b/bin/command.js index de33e38..ac05d00 100755 --- a/bin/command.js +++ b/bin/command.js @@ -56,6 +56,11 @@ program "If using BrowserStack, specify browsers using --browserstack. " + "Choices: " + browsers.join( ", " ) + ". Defaults to Chrome." ) + .option( + "--safari-tp", + "Use Safari Technology Preview instead of regular Safari. " + + "Only works with --browser safari and cannot be used with --browserstack." + ) .option( "-m, --middleware ", "Add middleware to the test server by passing the path to a module that exports " + diff --git a/browsers.js b/browsers.js index e451b4e..1be7b83 100644 --- a/browsers.js +++ b/browsers.js @@ -49,7 +49,10 @@ export async function createBrowserWorker( url, browser, options, restarts = 0 ) ) }` ); } - const { browserstack, debug, headless, reportId, runId, tunnelId, verbose } = options; + const { + browserstack, debug, headless, reportId, runId, safariTp, tunnelId, verbose + } = options; + while ( await maxWorkersReached( options ) ) { if ( verbose ) { console.log( "\nWaiting for available sessions..." ); @@ -90,6 +93,7 @@ export async function createBrowserWorker( url, browser, options, restarts = 0 ) const driver = await createDriver( { browserName: browser.browser, headless, + safariTp, url, verbose } ); diff --git a/lib/getBrowserString.js b/lib/getBrowserString.js index 413a605..263a811 100644 --- a/lib/getBrowserString.js +++ b/lib/getBrowserString.js @@ -25,10 +25,19 @@ export function getBrowserString( os, os_version: osVersion }, - headless + { + headless = false, + safariTp = false + } = {} ) { browser = browser.toLowerCase(); browser = browserMap[ browser ] || browser; + + // Handle Safari Technology Preview + if ( safariTp && browser === "Safari" ) { + browser = "Safari Technology Preview"; + } + let str = browser; if ( browserVersion ) { str += ` ${ browserVersion }`; diff --git a/queue.js b/queue.js index 3f1fc2a..ccf8741 100644 --- a/queue.js +++ b/queue.js @@ -77,7 +77,10 @@ export async function hardRetryTest( reportId, maxHardRetries ) { export function addRun( url, browser, options ) { queue.push( { browser, - fullBrowser: getBrowserString( browser ), + fullBrowser: getBrowserString( browser, { + headless: options.headless, + safariTp: options.safariTp + } ), hardRetries: 0, id: options.reportId, url, diff --git a/run.js b/run.js index 4886402..195195a 100644 --- a/run.js +++ b/run.js @@ -34,6 +34,7 @@ export async function run( { retries = 0, run: runs = [], runId, + safariTp, testUrl: testUrls = [], verbose } ) { @@ -45,6 +46,11 @@ export async function run( { "Cannot run in headless mode and debug mode at the same time." ); } + if ( safariTp && browserstack ) { + throw new Error( + "Cannot use --safari-tp with --browserstack." + ); + } // Ensure baseUrl ends with a slash if ( !rendsWithSlash.test( baseUrl ) ) { @@ -255,7 +261,7 @@ export async function run( { } function queueRun( browser, { run, testUrl } = {} ) { - const fullBrowser = getBrowserString( browser, headless ); + const fullBrowser = getBrowserString( browser, { headless, safariTp } ); const reportId = generateHash( `${ hashValue }-${ run }-${ testUrl }-${ fullBrowser }` ); @@ -287,6 +293,7 @@ export async function run( { headless, run, reportId, + safariTp, testUrl, tunnelId, verbose diff --git a/selenium/createDriver.js b/selenium/createDriver.js index f5eaa11..7840df0 100644 --- a/selenium/createDriver.js +++ b/selenium/createDriver.js @@ -2,13 +2,20 @@ import { Builder, Capabilities, logging } from "selenium-webdriver"; import Chrome from "selenium-webdriver/chrome.js"; import Edge from "selenium-webdriver/edge.js"; import Firefox from "selenium-webdriver/firefox.js"; +import Safari from "selenium-webdriver/safari.js"; import IE from "selenium-webdriver/ie.js"; import { browserSupportsHeadless } from "../lib/getBrowserString.js"; // Set script timeout to 10min const DRIVER_SCRIPT_TIMEOUT = 1000 * 60 * 10; -export default async function createDriver( { browserName, headless, url, verbose } ) { +export default async function createDriver( { + browserName, url, + headless = false, + verbose = false, + safariTp = false +} ) { + const capabilities = Capabilities[ browserName ](); // Support: IE 11+ @@ -55,6 +62,21 @@ export default async function createDriver( { browserName, headless, url, verbos edgeOptions.setEdgeChromiumBinaryPath( process.env.EDGE_BIN ); } + const safariOptions = new Safari.Options(); + + // Use Safari Technology Preview if --safari-tp flag is provided + if ( safariTp ) { + if ( verbose ) { + console.log( "Using Safari Technology Preview" ); + } + safariOptions.setTechnologyPreview( true ); + + // Without it, we're getting an error: + // SessionNotCreatedError: Could not create a session: Browser name does + // not match (requested: safari; available: Safari Technology Preview) + safariOptions.set( "browserName", "Safari Technology Preview" ); + } + const ieOptions = new IE.Options(); ieOptions.setEdgeChromium( true ); ieOptions.setEdgePath( "C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe" ); @@ -96,6 +118,7 @@ export default async function createDriver( { browserName, headless, url, verbos .setChromeOptions( chromeOptions ) .setFirefoxOptions( firefoxOptions ) .setEdgeOptions( edgeOptions ) + .setSafariOptions( safariOptions ) .setIeOptions( ieOptions ); if ( ieService ) {