run.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. // Type aliases
  17. type Testfunc = (driver: WebDriver) => void;
  18. // Shared selectors
  19. const composeArea = By.css('div.compose');
  20. const emojiKeyboard = By.css('.emoji-keyboard');
  21. const emojiTrigger = By.css('.emoji-trigger');
  22. /**
  23. * The emoji trigger should toggle the emoji keyboard.
  24. */
  25. async function showEmojiSelector(driver: WebDriver) {
  26. // Initially not visible
  27. expect(
  28. await driver.findElement(emojiKeyboard).isDisplayed()
  29. ).to.be.false;
  30. // Show
  31. await driver.findElement(emojiTrigger).click();
  32. expect(
  33. await driver.findElement(emojiKeyboard).isDisplayed()
  34. ).to.be.true;
  35. // Hide
  36. await driver.findElement(emojiTrigger).click();
  37. expect(
  38. await driver.findElement(emojiKeyboard).isDisplayed()
  39. ).to.be.false;
  40. }
  41. /**
  42. * Insert two emoji and some text.
  43. */
  44. async function insertEmoji(driver: WebDriver) {
  45. // Show emoji keyboard
  46. await driver.findElement(emojiTrigger).click();
  47. // Insert woman zombie emoji
  48. await driver.findElement(By.css('.e1._1f9df-2640')).click();
  49. // Insert text
  50. await driver.findElement(composeArea).sendKeys('hi');
  51. // Insert beer
  52. await driver.findElement(By.className('e1-food')).click();
  53. await driver.findElement(By.css('.e1._1f37b')).click();
  54. // Validate emoji
  55. const emoji = await driver.findElement(composeArea).findElements(By.xpath('*'));
  56. expect(emoji.length).to.equal(2);
  57. expect(await emoji[0].getAttribute('title')).to.equal(':woman_zombie:');
  58. expect(await emoji[1].getAttribute('title')).to.equal(':beers:');
  59. // Validate text
  60. const html = await driver.findElement(composeArea).getAttribute('innerHTML');
  61. expect(/>hi<img/.test(html)).to.be.true;
  62. }
  63. /**
  64. * Insert a newline using shift-enter.
  65. */
  66. async function insertNewline(driver: WebDriver) {
  67. // Insert text
  68. await driver.findElement(composeArea).click();
  69. await driver.findElement(composeArea).sendKeys('hello');
  70. await driver.findElement(composeArea).sendKeys(Key.SHIFT, Key.ENTER);
  71. await driver.findElement(composeArea).sendKeys('threema');
  72. await driver.findElement(composeArea).sendKeys(Key.SHIFT, Key.ENTER);
  73. await driver.findElement(composeArea).sendKeys('web');
  74. const script = `
  75. ${extractText.toString()}
  76. const element = document.querySelector("div.compose");
  77. return extractText(element);
  78. `;
  79. const text = await driver.executeScript<string>(script);
  80. expect(text).to.equal('hello\nthreema\nweb');
  81. }
  82. // Register tests here
  83. const TESTS: Array<[string, Testfunc]> = [
  84. ['Show and hide emoji selector', showEmojiSelector],
  85. ['Insert emoji and text', insertEmoji],
  86. ['Insert three lines of text', insertNewline],
  87. ];
  88. // Test runner
  89. const TEST_URL = 'http://localhost:7777/tests/ui/compose_area.html';
  90. (async function() {
  91. const driver: WebDriver = await new Builder().forBrowser(browser).build();
  92. let i = 0;
  93. let success = 0;
  94. let failed = 0;
  95. console.info('\n====== THREEMA WEB UI TESTS ======\n');
  96. try {
  97. for (const [name, testfunc] of TESTS) {
  98. console.info(TermColor.blue(`» ${i + 1}: Running test: ${name}`));
  99. await driver.get(TEST_URL);
  100. await testfunc(driver);
  101. success++;
  102. i++;
  103. }
  104. } catch (e) {
  105. console.error(TermColor.red(`\nTest failed:`));
  106. console.error(e);
  107. failed++;
  108. } finally {
  109. await driver.quit();
  110. }
  111. const colorFunc = failed > 0 ? TermColor.red : TermColor.green;
  112. console.info(colorFunc(`\nSummary: ${i} tests run, ${success} succeeded, ${failed} failed`));
  113. process.exit(failed > 0 ? 1 : 0);
  114. })();