@@ -7,10 +7,11 @@ import { assert } from 'chai';
77import path from 'path' ;
88import fs from 'fs' ;
99import { Server , createServer } from 'http' ;
10- import { Webdriver } from './test-utils' ;
10+ import { FirefoxDriver , SafariDriver , Webdriver } from './test-utils' ;
1111import { AxeBuilder } from '../src' ;
1212import { axeRunPartial } from '../src/browser' ;
1313import { fixturesPath } from 'axe-test-fixtures' ;
14+ import { Command , Name } from 'selenium-webdriver/lib/command' ;
1415
1516const dylangConfig = JSON . parse (
1617 fs . readFileSync ( path . join ( fixturesPath , 'dylang-config.json' ) , 'utf8' )
@@ -837,6 +838,48 @@ describe('@axe-core/webdriverjs', () => {
837838 ) ;
838839 }
839840 } ) ;
841+ it ( 'throws an error if switchTo fails' , async ( ) => {
842+ driver . switchTo = ( ) => {
843+ throw new Error ( 'switchTo failed.' ) ;
844+ } ;
845+ const title = await driver . getTitle ( ) ;
846+
847+ assert . notEqual ( title , 'Error' ) ;
848+ try {
849+ await new AxeBuilder ( driver , axeSource ) . analyze ( ) ;
850+ assert . fail ( 'Should have thrown' ) ;
851+ } catch ( err ) {
852+ assert . match ( ( err as Error ) . message , / s w i t c h T o f a i l e d ./ ) ;
853+ }
854+ } ) ;
855+ it ( 'throws an error if unable to determine window handle' , async ( ) => {
856+ // note: overriding executeScript to run twice and thus force finishRun to throw
857+ driver . executeScript = async ( script , ...args ) => {
858+ driver . execute (
859+ new Command ( Name . EXECUTE_SCRIPT )
860+ . setParameter ( 'script' , script )
861+ . setParameter ( 'args' , args )
862+ ) ;
863+ return driver . execute (
864+ new Command ( Name . EXECUTE_SCRIPT )
865+ . setParameter ( 'script' , script )
866+ . setParameter ( 'args' , args )
867+ ) ;
868+ } ;
869+ const title = await driver . getTitle ( ) ;
870+
871+ assert . notEqual ( title , 'Error' ) ;
872+ try {
873+ await new AxeBuilder ( driver , axeSource ) . analyze ( ) ;
874+ assert . fail ( 'Should have thrown' ) ;
875+ } catch ( err ) {
876+ assert . match ( ( err as Error ) . message , / U n a b l e t o d e t e r m i n e w i n d o w / ) ;
877+ assert . include (
878+ ( err as Error ) . message ,
879+ 'Please check out https://github.com/dequelabs/axe-core-npm/blob/develop/packages/webdriverjs/error-handling.md'
880+ ) ;
881+ }
882+ } ) ;
840883 } ) ;
841884
842885 describe ( 'setLegacyMode' , ( ) => {
@@ -1026,4 +1069,31 @@ describe('@axe-core/webdriverjs', () => {
10261069 assert . lengthOf ( allowedOrigins , 1 ) ;
10271070 } ) ;
10281071 } ) ;
1072+
1073+ describe ( 'driver' , ( ) => {
1074+ it ( `should not throw when it's Chrome` , async ( ) => {
1075+ assert . doesNotThrow ( async ( ) => {
1076+ await driver . get ( `${ addr } /index.html` ) ;
1077+ await driver . get ( 'about:blank' ) ;
1078+ } , Error ) ;
1079+ } ) ;
1080+ it ( `should not throw when it's Firefox` , async ( ) => {
1081+ driver = FirefoxDriver ( ) ;
1082+ assert . doesNotThrow ( async ( ) => {
1083+ await driver . get ( `${ addr } /index.html` ) ;
1084+ await driver . get ( 'about:blank' ) ;
1085+ } , Error ) ;
1086+ } ) ;
1087+ // skip this if we're not on Mac
1088+ ( process . platform === 'darwin' ? it : it . skip ) (
1089+ `should not throw when it's Safari` ,
1090+ async ( ) => {
1091+ driver = SafariDriver ( ) ;
1092+ assert . doesNotThrow ( async ( ) => {
1093+ await driver . get ( `${ addr } /index.html` ) ;
1094+ await driver . get ( 'about:blank' ) ;
1095+ } , Error ) ;
1096+ }
1097+ ) ;
1098+ } ) ;
10291099} ) ;
0 commit comments