run.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * Copyright © 2016-2018 Threema GmbH (https://threema.ch/).
  3. *
  4. * This file is part of Threema Web.
  5. */
  6. // tslint:disable:no-reference
  7. // tslint:disable:no-console
  8. // tslint:disable:no-unused-expression
  9. /// <reference path="../../src/threema.d.ts" />
  10. import { expect } from 'chai';
  11. import { Builder, By, Key, until, WebDriver, WebElement } from 'selenium-webdriver';
  12. import * as TermColor from 'term-color';
  13. import { extractText } from '../../src/helpers';
  14. // Script arguments
  15. const browser = process.argv[2];
  16. const filterQuery = process.argv[3];
  17. // Type aliases
  18. type Testfunc = (driver: WebDriver) => void;
  19. // Shared selectors
  20. const composeArea = By.css('div.compose');
  21. const emojiKeyboard = By.css('.emoji-keyboard');
  22. const emojiTrigger = By.css('.emoji-trigger');
  23. /**
  24. * The emoji trigger should toggle the emoji keyboard.
  25. */
  26. async function showEmojiSelector(driver: WebDriver) {
  27. // Initially not visible
  28. expect(
  29. await driver.findElement(emojiKeyboard).isDisplayed()
  30. ).to.be.false;
  31. // Show
  32. await driver.findElement(emojiTrigger).click();
  33. expect(
  34. await driver.findElement(emojiKeyboard).isDisplayed()
  35. ).to.be.true;
  36. // Hide
  37. await driver.findElement(emojiTrigger).click();
  38. expect(
  39. await driver.findElement(emojiKeyboard).isDisplayed()
  40. ).to.be.false;
  41. }
  42. /**
  43. * Insert two emoji and some text.
  44. */
  45. async function insertEmoji(driver: WebDriver) {
  46. // Show emoji keyboard
  47. await driver.findElement(emojiTrigger).click();
  48. // Insert woman zombie emoji
  49. await driver.findElement(By.css('.e1._1f9df-2640')).click();
  50. // Insert text
  51. await driver.findElement(composeArea).sendKeys('hi');
  52. // Insert beer
  53. await driver.findElement(By.className('e1-food')).click();
  54. await driver.findElement(By.css('.e1._1f37b')).click();
  55. // Validate emoji
  56. const emoji = await driver.findElement(composeArea).findElements(By.xpath('*'));
  57. expect(emoji.length).to.equal(2);
  58. expect(await emoji[0].getAttribute('title')).to.equal(':woman_zombie:');
  59. expect(await emoji[1].getAttribute('title')).to.equal(':beers:');
  60. // Validate text
  61. const html = await driver.findElement(composeArea).getAttribute('innerHTML');
  62. expect(/>hi<img/.test(html)).to.be.true;
  63. }
  64. /**
  65. * Insert a newline using shift-enter.
  66. */
  67. async function insertNewline(driver: WebDriver) {
  68. // Insert text
  69. await driver.findElement(composeArea).click();
  70. await driver.findElement(composeArea).sendKeys('hello');
  71. await driver.findElement(composeArea).sendKeys(Key.SHIFT, Key.ENTER);
  72. await driver.findElement(composeArea).sendKeys('threema');
  73. await driver.findElement(composeArea).sendKeys(Key.SHIFT, Key.ENTER);
  74. await driver.findElement(composeArea).sendKeys('web');
  75. const script = `
  76. ${extractText.toString()}
  77. const element = document.querySelector("div.compose");
  78. return extractText(element);
  79. `;
  80. const text = await driver.executeScript<string>(script);
  81. expect(text).to.equal('hello\nthreema\nweb');
  82. }
  83. // Register tests here
  84. const TESTS: Array<[string, Testfunc]> = [
  85. ['Show and hide emoji selector', showEmojiSelector],
  86. ['Insert emoji and text', insertEmoji],
  87. ['Insert three lines of text', insertNewline],
  88. ];
  89. // Test runner
  90. const TEST_URL = 'http://localhost:7777/tests/ui/compose_area.html';
  91. (async function() {
  92. const driver: WebDriver = await new Builder().forBrowser(browser).build();
  93. let i = 0;
  94. let success = 0;
  95. let failed = 0;
  96. let skipped = 0;
  97. console.info('\n====== THREEMA WEB UI TESTS ======\n');
  98. if (filterQuery !== undefined) {
  99. console.info(`Filter query: "${filterQuery}"\n`);
  100. }
  101. try {
  102. for (const [name, testfunc] of TESTS) {
  103. if (filterQuery === undefined || name.toLowerCase().indexOf(filterQuery.toLowerCase()) !== -1) {
  104. console.info(TermColor.blue(`» ${i + 1}: Running test: ${name}`));
  105. await driver.get(TEST_URL);
  106. await testfunc(driver);
  107. success++;
  108. } else {
  109. skipped++;
  110. }
  111. i++;
  112. }
  113. } catch (e) {
  114. console.error(TermColor.red(`\nTest failed:`));
  115. console.error(e);
  116. failed++;
  117. } finally {
  118. await driver.quit();
  119. }
  120. const colorFunc = failed > 0 ? TermColor.red : TermColor.green;
  121. console.info(colorFunc(`\nSummary: ${i} tests run, ${success} succeeded, ${failed} failed, ${skipped} skipped`));
  122. process.exit(failed > 0 ? 1 : 0);
  123. })();