@telefonica/acceptance-testing 2.10.0 → 2.11.1

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/README.md CHANGED
@@ -155,3 +155,14 @@ test('example screenshot test', async () => {
155
155
 
156
156
  You can also use globs for paths if you need: `api.spyOn('/some/*/path')`. We use the
157
157
  [`glob-to-regexp`](https://www.npmjs.com/package/glob-to-regexp) lib internally.
158
+
159
+ ## Uploading files
160
+
161
+ Due to a puppeteer bug or limitation, when the chromium is dockerized, the file to upload must exist in the
162
+ host and the container with the same path.
163
+
164
+ A helper function `prepareFile` is provided to facilitate this:
165
+
166
+ ```js
167
+ await elementHandle.uploadFile(prepareFile('/path/to/file'));
168
+ ```
@@ -813,6 +813,9 @@ var _excluded = ["captureBeyondViewport"],
813
813
  _excluded2 = ["userAgent", "isDarkMode", "viewport", "cookies"];
814
814
 
815
815
  var _pkg$acceptanceTests, _ref;
816
+
817
+ var execSync = /*#__PURE__*/require('child_process').execSync;
818
+
816
819
  var getGlobalBrowser = function getGlobalBrowser() {
817
820
  return global.browser;
818
821
  };
@@ -962,6 +965,18 @@ var normalizeSreenshotOptions = function normalizeSreenshotOptions(_temp2) {
962
965
  return _extends({}, options, {
963
966
  captureBeyondViewport: captureBeyondViewport
964
967
  });
968
+ }; // Puppeteer already calls scrollIntoViewIfNeeded before clicking an element. But it doesn't work in all situations
969
+ // For example, when there is a fixed footer in the page and the element to click is under it, the browser won't scroll
970
+ // because the element is already in the viewport (the ifNeeded part is important here). By forcing the scroll to the
971
+ // center, we manage to fix these edge cases.
972
+
973
+
974
+ var scrollIntoView = function scrollIntoView(el) {
975
+ return el.evaluate(function (e) {
976
+ return e.scrollIntoView({
977
+ block: 'center'
978
+ });
979
+ });
965
980
  };
966
981
 
967
982
  var getPageApi = function getPageApi(page) {
@@ -973,9 +988,13 @@ var getPageApi = function getPageApi(page) {
973
988
  while (1) {
974
989
  switch (_context2.prev = _context2.next) {
975
990
  case 0:
991
+ _context2.next = 2;
992
+ return scrollIntoView(elementHandle);
993
+
994
+ case 2:
976
995
  return _context2.abrupt("return", elementHandle.type(text, options));
977
996
 
978
- case 1:
997
+ case 3:
979
998
  case "end":
980
999
  return _context2.stop();
981
1000
  }
@@ -994,9 +1013,13 @@ var getPageApi = function getPageApi(page) {
994
1013
  while (1) {
995
1014
  switch (_context3.prev = _context3.next) {
996
1015
  case 0:
1016
+ _context3.next = 2;
1017
+ return scrollIntoView(elementHandle);
1018
+
1019
+ case 2:
997
1020
  return _context3.abrupt("return", elementHandle.click(options));
998
1021
 
999
- case 1:
1022
+ case 3:
1000
1023
  case "end":
1001
1024
  return _context3.stop();
1002
1025
  }
@@ -1020,13 +1043,17 @@ var getPageApi = function getPageApi(page) {
1020
1043
  while (1) {
1021
1044
  switch (_context4.prev = _context4.next) {
1022
1045
  case 0:
1046
+ _context4.next = 2;
1047
+ return scrollIntoView(elementHandle);
1048
+
1049
+ case 2:
1023
1050
  for (_len = _args4.length, values = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
1024
1051
  values[_key - 1] = _args4[_key];
1025
1052
  }
1026
1053
 
1027
1054
  return _context4.abrupt("return", elementHandle.select.apply(elementHandle, values));
1028
1055
 
1029
- case 2:
1056
+ case 4:
1030
1057
  case "end":
1031
1058
  return _context4.stop();
1032
1059
  }
@@ -1348,7 +1375,7 @@ var buildQueryMethods = function buildQueryMethods(_temp3) {
1348
1375
  var _Object$entries$_i = _Object$entries[_i],
1349
1376
  queryName = _Object$entries$_i[0],
1350
1377
  queryFn = _Object$entries$_i[1];
1351
- boundQueries[queryName] = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee9() {
1378
+ boundQueries[queryName] = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee12() {
1352
1379
  var doc,
1353
1380
  body,
1354
1381
  _len2,
@@ -1357,25 +1384,25 @@ var buildQueryMethods = function buildQueryMethods(_temp3) {
1357
1384
  queryArgs,
1358
1385
  elementHandle,
1359
1386
  newElementHandle,
1360
- _args9 = arguments;
1387
+ _args12 = arguments;
1361
1388
 
1362
- return runtime_1.wrap(function _callee9$(_context9) {
1389
+ return runtime_1.wrap(function _callee12$(_context12) {
1363
1390
  while (1) {
1364
- switch (_context9.prev = _context9.next) {
1391
+ switch (_context12.prev = _context12.next) {
1365
1392
  case 0:
1366
- _context9.next = 2;
1393
+ _context12.next = 2;
1367
1394
  return pptrTestingLibrary.getDocument(page != null ? page : getGlobalPage());
1368
1395
 
1369
1396
  case 2:
1370
- doc = _context9.sent;
1371
- _context9.next = 5;
1397
+ doc = _context12.sent;
1398
+ _context12.next = 5;
1372
1399
  return doc.$('body');
1373
1400
 
1374
1401
  case 5:
1375
- body = _context9.sent;
1402
+ body = _context12.sent;
1376
1403
 
1377
- for (_len2 = _args9.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
1378
- args[_key2] = _args9[_key2];
1404
+ for (_len2 = _args12.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
1405
+ args[_key2] = _args12[_key2];
1379
1406
  }
1380
1407
 
1381
1408
  queryArgs = [].concat(args);
@@ -1390,11 +1417,11 @@ var buildQueryMethods = function buildQueryMethods(_temp3) {
1390
1417
  });
1391
1418
  }
1392
1419
 
1393
- _context9.next = 11;
1420
+ _context12.next = 11;
1394
1421
  return queryFn.apply(void 0, [element != null ? element : body].concat(queryArgs));
1395
1422
 
1396
1423
  case 11:
1397
- elementHandle = _context9.sent;
1424
+ elementHandle = _context12.sent;
1398
1425
  newElementHandle = Object.create(elementHandle);
1399
1426
 
1400
1427
  newElementHandle.screenshot = /*#__PURE__*/function () {
@@ -1433,14 +1460,83 @@ var buildQueryMethods = function buildQueryMethods(_temp3) {
1433
1460
  };
1434
1461
  }();
1435
1462
 
1436
- return _context9.abrupt("return", newElementHandle);
1463
+ newElementHandle.click = /*#__PURE__*/function () {
1464
+ var _ref19 = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee9(options) {
1465
+ return runtime_1.wrap(function _callee9$(_context9) {
1466
+ while (1) {
1467
+ switch (_context9.prev = _context9.next) {
1468
+ case 0:
1469
+ _context9.next = 2;
1470
+ return scrollIntoView(elementHandle);
1471
+
1472
+ case 2:
1473
+ return _context9.abrupt("return", elementHandle.click(options));
1474
+
1475
+ case 3:
1476
+ case "end":
1477
+ return _context9.stop();
1478
+ }
1479
+ }
1480
+ }, _callee9);
1481
+ }));
1482
+
1483
+ return function (_x13) {
1484
+ return _ref19.apply(this, arguments);
1485
+ };
1486
+ }();
1487
+
1488
+ newElementHandle.type = /*#__PURE__*/function () {
1489
+ var _ref20 = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee10(text, options) {
1490
+ return runtime_1.wrap(function _callee10$(_context10) {
1491
+ while (1) {
1492
+ switch (_context10.prev = _context10.next) {
1493
+ case 0:
1494
+ _context10.next = 2;
1495
+ return scrollIntoView(elementHandle);
1496
+
1497
+ case 2:
1498
+ return _context10.abrupt("return", elementHandle.type(text, options));
1499
+
1500
+ case 3:
1501
+ case "end":
1502
+ return _context10.stop();
1503
+ }
1504
+ }
1505
+ }, _callee10);
1506
+ }));
1507
+
1508
+ return function (_x14, _x15) {
1509
+ return _ref20.apply(this, arguments);
1510
+ };
1511
+ }();
1512
+
1513
+ newElementHandle.select = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee11() {
1514
+ var _args11 = arguments;
1515
+ return runtime_1.wrap(function _callee11$(_context11) {
1516
+ while (1) {
1517
+ switch (_context11.prev = _context11.next) {
1518
+ case 0:
1519
+ _context11.next = 2;
1520
+ return scrollIntoView(elementHandle);
1521
+
1522
+ case 2:
1523
+ return _context11.abrupt("return", elementHandle.select.apply(elementHandle, _args11));
1524
+
1525
+ case 3:
1526
+ case "end":
1527
+ return _context11.stop();
1528
+ }
1529
+ }
1530
+ }, _callee11);
1531
+ }));
1532
+ return _context12.abrupt("return", newElementHandle);
1437
1533
 
1438
- case 15:
1534
+ case 18:
1439
1535
  case "end":
1440
- return _context9.stop();
1536
+ return _context12.stop();
1441
1537
  }
1442
1538
  }
1443
- }, _callee9);
1539
+ }, _callee12);
1444
1540
  }));
1445
1541
  };
1446
1542
 
@@ -1462,55 +1558,86 @@ var within = function within(element) {
1462
1558
  element: element
1463
1559
  });
1464
1560
  };
1465
- beforeEach( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee10() {
1466
- return runtime_1.wrap(function _callee10$(_context10) {
1561
+ beforeEach( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee13() {
1562
+ return runtime_1.wrap(function _callee13$(_context13) {
1467
1563
  while (1) {
1468
- switch (_context10.prev = _context10.next) {
1564
+ switch (_context13.prev = _context13.next) {
1469
1565
  case 0:
1470
- _context10.next = 2;
1566
+ _context13.next = 2;
1471
1567
  return getGlobalPage().setRequestInterception(false);
1472
1568
 
1473
1569
  case 2:
1474
- _context10.next = 4;
1570
+ _context13.next = 4;
1475
1571
  return global.jestPuppeteer.resetPage();
1476
1572
 
1477
1573
  case 4:
1478
1574
  case "end":
1479
- return _context10.stop();
1575
+ return _context13.stop();
1480
1576
  }
1481
1577
  }
1482
- }, _callee10);
1578
+ }, _callee13);
1483
1579
  })));
1484
- afterEach( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee11() {
1580
+ afterEach( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee14() {
1485
1581
  var page;
1486
- return runtime_1.wrap(function _callee11$(_context11) {
1582
+ return runtime_1.wrap(function _callee14$(_context14) {
1487
1583
  while (1) {
1488
- switch (_context11.prev = _context11.next) {
1584
+ switch (_context14.prev = _context14.next) {
1489
1585
  case 0:
1490
- _context11.prev = 0;
1586
+ _context14.prev = 0;
1491
1587
  page = getGlobalPage();
1492
1588
  requestHandlers = [];
1493
1589
  needsRequestInterception = false;
1494
1590
  page.off('request', requestInterceptor); // clear tab, this way we clear the DOM and stop js execution or pending requests
1495
1591
 
1496
- _context11.next = 7;
1592
+ _context14.next = 7;
1497
1593
  return page["goto"]('about:blank');
1498
1594
 
1499
1595
  case 7:
1500
- _context11.next = 11;
1596
+ _context14.next = 11;
1501
1597
  break;
1502
1598
 
1503
1599
  case 9:
1504
- _context11.prev = 9;
1505
- _context11.t0 = _context11["catch"](0);
1600
+ _context14.prev = 9;
1601
+ _context14.t0 = _context14["catch"](0);
1506
1602
 
1507
1603
  case 11:
1508
1604
  case "end":
1509
- return _context11.stop();
1605
+ return _context14.stop();
1510
1606
  }
1511
1607
  }
1512
- }, _callee11, null, [[0, 9]]);
1608
+ }, _callee14, null, [[0, 9]]);
1513
1609
  })));
1610
+ /**
1611
+ * Returns a new path to the file that can be used by chromium in acceptance tests
1612
+ *
1613
+ * To be able to use `element.uploadFile()` in a dockerized chromium, the file must exist in the
1614
+ * host and the docker, and both sides must use the same path.
1615
+ *
1616
+ * To workaround this bug or limitation, this function prepares the file by copying it to /tmp in
1617
+ * the host and the container.
1618
+ */
1619
+
1620
+ var prepareFile = function prepareFile(filepath) {
1621
+ var isLocal = !isCi;
1622
+ var isHeadless = !!process.env.HEADLESS;
1623
+ var usesDocker = isLocal && isHeadless;
1624
+ var dockerComposeFile = path.join(__dirname, '..', 'docker-compose.yaml');
1625
+
1626
+ if (usesDocker) {
1627
+ var containerId = execSync("docker-compose -f " + dockerComposeFile + " ps -q").toString().trim();
1628
+
1629
+ if (!containerId) {
1630
+ throw Error('acceptance-testing container not found');
1631
+ }
1632
+
1633
+ execSync("docker cp " + filepath + " " + containerId + ":/tmp");
1634
+ var newPath = path.join('/tmp', path.basename(filepath));
1635
+ fs.copyFileSync(filepath, newPath);
1636
+ return newPath;
1637
+ } else {
1638
+ return filepath;
1639
+ }
1640
+ };
1514
1641
 
1515
1642
  exports.createApiEndpointMock = createApiEndpointMock;
1516
1643
  exports.getGlobalBrowser = getGlobalBrowser;
@@ -1519,6 +1646,7 @@ exports.getPageApi = getPageApi;
1519
1646
  exports.getScreen = getScreen;
1520
1647
  exports.interceptRequest = interceptRequest;
1521
1648
  exports.openPage = openPage;
1649
+ exports.prepareFile = prepareFile;
1522
1650
  exports.screen = screen;
1523
1651
  exports.serverHostName = serverHostName;
1524
1652
  exports.serverPort = serverPort;