run.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 as extractTextFunc } 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. * Helper function to extract text.
  25. */
  26. async function extractText(driver: WebDriver): Promise<string> {
  27. const script = `
  28. ${extractTextFunc.toString()}
  29. const element = document.querySelector("div.compose");
  30. return extractText(element);
  31. `;
  32. return driver.executeScript<string>(script);
  33. }
  34. /**
  35. * The emoji trigger should toggle the emoji keyboard.
  36. */
  37. async function showEmojiSelector(driver: WebDriver) {
  38. // Initially not visible
  39. expect(
  40. await driver.findElement(emojiKeyboard).isDisplayed()
  41. ).to.be.false;
  42. // Show
  43. await driver.findElement(emojiTrigger).click();
  44. expect(
  45. await driver.findElement(emojiKeyboard).isDisplayed()
  46. ).to.be.true;
  47. // Hide
  48. await driver.findElement(emojiTrigger).click();
  49. expect(
  50. await driver.findElement(emojiKeyboard).isDisplayed()
  51. ).to.be.false;
  52. }
  53. /**
  54. * Insert two emoji and some text.
  55. */
  56. async function insertEmoji(driver: WebDriver) {
  57. // Show emoji keyboard
  58. await driver.findElement(emojiTrigger).click();
  59. // Insert woman zombie emoji
  60. await driver.findElement(By.css('.e1._1f9df-2640')).click();
  61. // Insert text
  62. await driver.findElement(composeArea).sendKeys('hi');
  63. // Insert beer
  64. await driver.findElement(By.className('e1-food')).click();
  65. await driver.findElement(By.css('.e1._1f37b')).click();
  66. // Validate emoji
  67. const emoji = await driver.findElement(composeArea).findElements(By.xpath('*'));
  68. expect(emoji.length).to.equal(2);
  69. expect(await emoji[0].getAttribute('title')).to.equal(':woman_zombie:');
  70. expect(await emoji[1].getAttribute('title')).to.equal(':beers:');
  71. // Validate text
  72. const html = await driver.findElement(composeArea).getAttribute('innerHTML');
  73. expect(/>hi<img/.test(html)).to.be.true;
  74. }
  75. /**
  76. * Insert a newline using shift-enter.
  77. */
  78. async function insertNewline(driver: WebDriver) {
  79. // Insert text
  80. await driver.findElement(composeArea).click();
  81. await driver.findElement(composeArea).sendKeys('hello');
  82. await driver.findElement(composeArea).sendKeys(Key.SHIFT, Key.ENTER);
  83. await driver.findElement(composeArea).sendKeys('threema');
  84. await driver.findElement(composeArea).sendKeys(Key.SHIFT, Key.ENTER);
  85. await driver.findElement(composeArea).sendKeys('web');
  86. const text = await extractText(driver);
  87. expect(text).to.equal('hello\nthreema\nweb');
  88. }
  89. /**
  90. * Insert an emoji after some newlines.
  91. * Regression test for #574.
  92. */
  93. async function regression574(driver: WebDriver) {
  94. // Insert text
  95. await driver.findElement(composeArea).click();
  96. await driver.findElement(composeArea).sendKeys('hello');
  97. await driver.findElement(composeArea).sendKeys(Key.SHIFT, Key.ENTER);
  98. await driver.findElement(composeArea).sendKeys('threema');
  99. await driver.findElement(composeArea).sendKeys(Key.SHIFT, Key.ENTER);
  100. await driver.findElement(composeArea).sendKeys('web');
  101. await driver.findElement(composeArea).sendKeys(Key.SHIFT, Key.ENTER);
  102. // Insert emoji
  103. await driver.findElement(emojiTrigger).click();
  104. await driver.findElement(By.css('.e1[title=":smile:"]')).click();
  105. const text = await extractText(driver);
  106. expect(text).to.equal('hello\nthreema\nweb\n😄');
  107. }
  108. // Register tests here
  109. const TESTS: Array<[string, Testfunc]> = [
  110. ['Show and hide emoji selector', showEmojiSelector],
  111. ['Insert emoji and text', insertEmoji],
  112. ['Insert three lines of text', insertNewline],
  113. ['Regression test #574', regression574],
  114. ];
  115. // Test runner
  116. const TEST_URL = 'http://localhost:7777/tests/ui/compose_area.html';
  117. (async function() {
  118. const driver: WebDriver = await new Builder().forBrowser(browser).build();
  119. let i = 0;
  120. let success = 0;
  121. let failed = 0;
  122. let skipped = 0;
  123. console.info('\n====== THREEMA WEB UI TESTS ======\n');
  124. if (filterQuery !== undefined) {
  125. console.info(`Filter query: "${filterQuery}"\n`);
  126. }
  127. try {
  128. for (const [name, testfunc] of TESTS) {
  129. if (filterQuery === undefined || name.toLowerCase().indexOf(filterQuery.toLowerCase()) !== -1) {
  130. console.info(TermColor.blue(`» ${i + 1}: Running test: ${name}`));
  131. await driver.get(TEST_URL);
  132. await testfunc(driver);
  133. success++;
  134. } else {
  135. skipped++;
  136. }
  137. i++;
  138. }
  139. } catch (e) {
  140. console.error(TermColor.red(`\nTest failed:`));
  141. console.error(e);
  142. failed++;
  143. } finally {
  144. await driver.quit();
  145. }
  146. const colorFunc = failed > 0 ? TermColor.red : TermColor.green;
  147. console.info(colorFunc(`\nSummary: ${i} tests run, ${success} succeeded, ${failed} failed, ${skipped} skipped`));
  148. process.exit(failed > 0 ? 1 : 0);
  149. })();