@suffolklitlab/alkiln 4.4.0 → 4.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -40,6 +40,9 @@ Format:
40
40
  -->
41
41
  <!-- ## [Unreleased] -->
42
42
 
43
+ ## [4.5.0] - 2022-05-23
44
+ - Steps that tap tabs (made using the `tabbed_templates_html` function from ALToolbox) and other arbitrary elements on the page.
45
+
43
46
  ## [4.4.0] - 2022-05-23
44
47
  ### Added
45
48
  - Appends the results of the cucumber [summary formatter](https://github.com/cucumber/cucumber-js/blob/main/docs/formatters.md#summary) to the `debug_log.txt`, which includes useful stack traces into the Kiln code when tests fail.
package/lib/scope.js CHANGED
@@ -244,10 +244,37 @@ module.exports = {
244
244
  if ( showif ) { await scope.page.waitForTimeout( scope.showif_timeout ); }
245
245
  },
246
246
 
247
- afterStep: async function afterStep(scope, options = {waitForShowIf: false, navigated: false, waitForTimeout: 0}) {
247
+ waitForTab: async function waitForTab(scope, tab_to_show) {
248
+ /**
249
+ * @param {tab_to_show} the aria-labeledby text of the tab panel, which
250
+ * we use to query and wait for that tab panel to be shown.
251
+ */
252
+ // Implementation linked to https://github.com/SuffolkLITLab/docassemble-ALToolbox/blob/4aa1069389d2ca9cadb586f309dd73d791f081d1/docassemble/ALToolbox/misc.py#L127
253
+ // I think `.$()` does not use a timeout
254
+ if ( tab_to_show.length == 0) { return }
255
+ // waiting for the transitionend event: https://stackoverflow.com/a/57955251/11416267
256
+ await scope.page.evaluate((element_query) => {
257
+ return new Promise((resolve) => {
258
+ const elem = document.querySelector(element_query);
259
+ // Taken from jQuery: https://stackoverflow.com/a/33456469/11416267
260
+ const is_visible = !!( elem.offsetWidth || elem.offsetHeight || elem.getClientRects().length );
261
+ if (!is_visible) {
262
+ const onEnd = function() {
263
+ elem.removeEventListener('transitionend', onEnd);
264
+ resolve();
265
+ };
266
+ elem.addEventListener('transitionend', onEnd);
267
+ } else {
268
+ resolve();
269
+ }
270
+ });
271
+ }, `div[aria-labelledby="${tab_to_show}"]`);
272
+ },
273
+
274
+ afterStep: async function afterStep(scope, options = {waitForShowIf: false, tabToWaitFor: '', navigated: false, waitForTimeout: 0}) {
248
275
  /* Common things to do after a step, including take care of errors.
249
276
  * TODO: discuss splitting into `afterField` and `afterStep` or something. Or just change name
250
- * TODO: Update to latest cucumberjs, which as `AfterStep` */
277
+ * TODO: Update to latest cucumberjs, which has `AfterStep` */
251
278
 
252
279
  if ( scope.page ) {
253
280
  // Use a while loop until the below does not error with `Error: Execution context was destroyed`
@@ -274,6 +301,7 @@ module.exports = {
274
301
 
275
302
  // Wait for elements that might get revealed
276
303
  if ( options.waitForShowIf ) { await scope.waitForShowIf( scope ); }
304
+ if ( options.tabToWaitFor ) { await scope.waitForTab( scope, options.tabToWaitFor); }
277
305
  } // ends if scope.page
278
306
 
279
307
  if ( options.navigated ) {
@@ -290,7 +318,7 @@ module.exports = {
290
318
  if ( options.waitForTimeout ) { await time.waitForTimeout( options.waitForTimeout ); }
291
319
  }, // Ends afterStep
292
320
 
293
- tapElement: async function tapElement(scope, elem) {
321
+ tapElement: async function tapElement(scope, elem, tabToWaitFor='', waitForTimeout=0) {
294
322
  /* Tap a given element, detect navigation, and do appropriate related things. */
295
323
 
296
324
  // This check might never be needed now as we try to match fields on the page
@@ -303,25 +331,32 @@ module.exports = {
303
331
 
304
332
  let start_url = await scope.page.url(); // so we can later check for navigation
305
333
 
306
- let winner;
307
- winner = await Promise.race([
308
- Promise.all([ // Error loads page, so no need to detect to keep things short
309
- // Click with no navigation will end immediately
310
- // Can't use elem[ scope.activate ](). It works locally, but errors on GitHub
311
- // with "Node is not visible or not an HTMLElement". We're not using a custom Chromium version
312
- // Unfortunately, this won't catch an invisible element. Hopefully we'd catch it before now...?
313
- // "Tap" on mobile is more complex to implement ourselves.
314
- // See https://stackoverflow.com/a/56547605/14144258
315
- // and other convos around it. We haven't made it public that the device
316
- // can customized. Until we do that, we'll just use "click" all the time.
334
+ let clickWait;
335
+ // Can't use elem[ scope.activate ](). It works locally, but errors on GitHub
336
+ // with "Node is not visible or not an HTMLElement". We're not using a custom Chromium version
337
+ // Unfortunately, this won't catch an invisible element. Hopefully we'd catch it before now...?
338
+ // "Tap" on mobile is more complex to implement ourselves.
339
+ // See https://stackoverflow.com/a/56547605/14144258
340
+ // and other convos around it. We haven't made it public that the device
341
+ // can customized. Until we do that, we'll just use "click" all the time.
342
+ if (!tabToWaitFor) {
343
+ // Click with no navigation will end immediately
344
+ clickWait = Promise.all([
317
345
  elem.evaluate( (el) => { return el.click(); }),
318
346
  scope.page.waitForNavigation({ waitUntil: 'domcontentloaded' }),
319
- ]),
347
+ ]);
348
+ } else {
349
+ // Same thing as above, but the promise doesn't wait for navigation.
350
+ clickWait = elem.evaluate( (el) => {return el.click(); });
351
+ }
352
+ // Error loads page, so no need to detect to keep things short
353
+ let winner = await Promise.race([
354
+ clickWait,
320
355
  // This is meant to detect user-visible alerts/errors,
321
356
  // not breaking errors in the testing code.
322
357
  scope.page.waitForSelector('.alert-danger', { visible: true }),
323
358
  scope.page.waitForSelector('.da-has-error', { visible: true }),
324
- ]);
359
+ ])
325
360
 
326
361
  // TODO: Not sure how to detect navigation landing at same page.
327
362
  // Maybe url change + trigger var change? Maybe just trigger var?
@@ -335,8 +370,12 @@ module.exports = {
335
370
  // If navigation, wait for 200ms to let things settle down
336
371
  await scope.afterStep(scope, {waitForTimeout: 200, navigated: navigated});
337
372
  } else {
338
- // If stay on page, wait for `show if` or feedback elements to be move around
339
- await scope.afterStep(scope, {waitForShowIf: true, navigated: navigated});
373
+ // If stay on page, wait for `show if`, the new tab to be loaded, or feedback elements to be move around
374
+ options = {waitForShowIf: !tabToWaitFor, tabToWaitFor: tabToWaitFor, navigated: navigated};
375
+ if (waitForTimeout > 0) {
376
+ options = {waitForTimeout: waitForTimeout, navigated: navigated};
377
+ }
378
+ await scope.afterStep(scope, options);
340
379
  }
341
380
 
342
381
  return winner;
@@ -426,13 +465,33 @@ module.exports = {
426
465
  await scope.tapElement( scope, elem );
427
466
  },
428
467
 
468
+ tapTab: async function ( scope, tab_id ) {
469
+ let elem = await scope.page.$(`#${tab_id}`);
470
+ let error_msg = `Couldn't find the tab with id "#${tab_id}" on the page, is there a typo?`;
471
+ if ( !elem ) {
472
+ await scope.addToReport(scope, { type: `error`, value: error_msg });
473
+ }
474
+ expect( elem, error_msg ).to.exist;
475
+ await scope.tapElement( scope, elem, tab_id );
476
+ },
477
+
478
+ tapElementBySelector: async function ( scope, selector, waitForTimeout ) {
479
+ let elem = await scope.page.$(`${selector}`);
480
+ let error_msg = `Couldn't find "${selector}" on the page, is there a typo?`;
481
+ if ( !elem ) {
482
+ await scope.addToReport(scope, { type: `error`, value: error_msg });
483
+ }
484
+ expect( elem, error_msg ).to.exist;
485
+ await scope.tapElement( scope, elem, '', waitForTimeout );
486
+ },
487
+
429
488
  load: async function ( scope, file_name ) {
430
489
  /* Try to load the page. Should we also pass a `timeout` arg or just
431
490
  * use scope.timeout? */
432
491
 
433
492
  // If there is no browser open, start a new one
434
493
  if (!scope.browser) {
435
- scope.browser = await scope.driver.launch({ headless: !session_vars.get_debug() });
494
+ scope.browser = await scope.driver.launch({ headless: !session_vars.get_debug(), devtools: session_vars.get_debug() });
436
495
  }
437
496
  if ( !scope.page ) { scope.page = await scope.browser.newPage(); }
438
497
 
package/lib/steps.js CHANGED
@@ -391,7 +391,7 @@ When(/^(?:the user gets)?(?:I get)? to ?(?:the)?(?: ?question)?(?: id)? "([^"]+)
391
391
  //#####################################
392
392
  //#####################################
393
393
 
394
- When(/I wait (\d*\.?\d+) seconds?/, { timeout: -1 }, async ( seconds ) => {
394
+ When(/I wait ?(?:for)? (\d*\.?\d+) seconds?/, { timeout: -1 }, async ( seconds ) => {
395
395
  /** Wait for a specified amount of time. Can help with some race conditions.
396
396
  * We're working on reducing race conditions, but it'll never be perfect.
397
397
  *
@@ -707,6 +707,27 @@ When('I tap to continue', { timeout: -1 }, async () => {
707
707
  );
708
708
  });
709
709
 
710
+ When(/I tap the "([^"]+)" tab/i, { timeout: -1 }, async ( tabId ) => {
711
+ return wrapPromiseWithTimeout(
712
+ scope.tapTab( scope, tabId ),
713
+ scope.timeout
714
+ )
715
+ });
716
+
717
+ When(/I tap the "([^"]+)" element$/i, { timeout: -1 }, async ( elemSelector ) => {
718
+ return wrapPromiseWithTimeout(
719
+ scope.tapElementBySelector( scope, elemSelector, 2000 ),
720
+ scope.timeout
721
+ )
722
+ });
723
+
724
+ When(/I tap the "([^"]+)" element and wait ([0-9]+) second(?:s)?/i, { timeout: -1 }, async ( elemSelector, waitTime ) => {
725
+ return wrapPromiseWithTimeout(
726
+ scope.tapElementBySelector( scope, elemSelector, waitTime * 1000 ),
727
+ scope.timeout
728
+ )
729
+ });
730
+
710
731
 
711
732
  //#####################################
712
733
  // UI element interaction
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@suffolklitlab/alkiln",
3
- "version": "4.4.0",
3
+ "version": "4.5.0",
4
4
  "description": "Integrated automated end-to-end testing with docassemble, puppeteer, and cucumber.",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
@@ -101,3 +101,25 @@ Scenario: handles settings from Github secrets
101
101
  Then I see the phrase "secret-var2-value"
102
102
  When I tap to continue
103
103
  And I set the variable "third_text_entry" to secret "SECRET_NOT_THERE"
104
+
105
+ @i3 @tap-elements @tabs
106
+ Scenario: tap elements
107
+ Given I start the interview at "test_taps"
108
+ And I tap the "Tests-first_template-tab" tab
109
+ Then I see the phrase "Mechanics"
110
+ And I tap the "Tests-second_template-tab" tab
111
+ Then I see the phrase "villify"
112
+ And I tap the "Tests-third_template-tab" tab
113
+ Then I see the phrase "museum"
114
+ And I tap the "#special_event" element and wait 1 second
115
+ Then I see the phrase "Portishead"
116
+
117
+ @i4 @tap-elements @tabs
118
+ Scenario: tap element with an error
119
+ Given the final Scenario status should be "failed"
120
+ And the Scenario report should include:
121
+ """
122
+ is there a typo?
123
+ """
124
+ Given I start the interview at "test_taps"
125
+ And I tap the "Tests-not_there-tab" tab