@@ -121,6 +121,18 @@ When('I press {string} key', async function (key: string) {
121121 await page . press ( 'body' , key ) ;
122122} ) ;
123123
124+ /**
125+ * Press button given number of times
126+ * @param {string } key - key to press
127+ * @param {number } num - number of times
128+ * @example I press 'Enter' key 5 times
129+ */
130+ When ( 'I press {string} key {int} time(s)' , async function ( key : string , num : number ) {
131+ for ( let i : number = 0 ; i < num ; i ++ ) {
132+ await page . keyboard . press ( key ) ;
133+ }
134+ } ) ;
135+
124136/**
125137 * Hover over element
126138 * @param {string } alias - element to hover over
@@ -197,7 +209,6 @@ When('I scroll by {string} in {string}', async function (offset: string, alias:
197209 } , coords ) ;
198210} ) ;
199211
200-
201212/**
202213 * Provide file url to upload input
203214 * @param {string } alias - element to upload file
@@ -209,3 +220,37 @@ When('I upload {string} file to {string}', async function (value: string, alias:
209220 const filePath = await getValue ( value ) ;
210221 await element . setInputFiles ( filePath ) ;
211222} ) ;
223+
224+ /**
225+ * Accept alert
226+ * @example I accept alert
227+ */
228+ When ( 'I accept alert' , async function ( ) {
229+ await new Promise < void > ( ( resolve ) => page . once ( 'dialog' , async ( dialog ) => {
230+ await dialog . accept ( ) ;
231+ resolve ( ) ;
232+ } ) )
233+ } ) ;
234+
235+ /**
236+ * Dismiss alert
237+ * Playwright automatically dismisses all dialogs. This step is just to make it implicitly.
238+ * @example I dismiss alert
239+ */
240+ When ( 'I dismiss alert' , async function ( ) {
241+ await new Promise < void > ( ( resolve ) => page . once ( 'dialog' , async ( dialog ) => {
242+ await dialog . dismiss ( ) ;
243+ resolve ( ) ;
244+ } ) ) ;
245+ } ) ;
246+
247+ /**
248+ * I type {string} to alert
249+ * @example I type 'coffee' to alert
250+ */
251+ When ( 'I type {string} to alert' , async function ( value : string ) {
252+ await new Promise < void > ( ( resolve ) => page . once ( 'dialog' , async ( dialog ) => {
253+ await dialog . accept ( value ) ;
254+ resolve ( ) ;
255+ } ) )
256+ } ) ;
0 commit comments