cssClass.mjs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { describe, it, before, after, afterEach } from 'mocha'
  2. import { expect } from 'chai'
  3. import { By, Condition } from 'selenium-webdriver'
  4. import {
  5. DEFAULT_UI_WAIT_MS,
  6. getRootAndWait,
  7. takeScreenshotOnFailure,
  8. } from '../../lib/elements.js'
  9. async function waitForThemeCss () {
  10. await webdriver.wait(
  11. new Condition('wait for theme CSS to load', async () => {
  12. const body = await webdriver.findElement(By.tagName('body'))
  13. const loadedTheme = await body.getAttribute('loaded-theme')
  14. if (!loadedTheme) {
  15. return false
  16. }
  17. return await webdriver.executeScript(`
  18. const style = document.getElementById('theme-style');
  19. return !!(style && style.textContent && style.textContent.includes('test-custom-class'));
  20. `)
  21. }),
  22. DEFAULT_UI_WAIT_MS
  23. )
  24. }
  25. async function waitForCssColor (element, channelPattern, description) {
  26. await webdriver.wait(
  27. new Condition(`wait for ${description}`, async () => {
  28. const bgColor = await element.getCssValue('background-color')
  29. return channelPattern.test(bgColor)
  30. }),
  31. DEFAULT_UI_WAIT_MS
  32. )
  33. }
  34. describe('config: cssClass', function () {
  35. before(async function () {
  36. await runner.start('cssClass')
  37. })
  38. after(async () => {
  39. await runner.stop()
  40. })
  41. afterEach(function () {
  42. takeScreenshotOnFailure(this.currentTest, webdriver)
  43. })
  44. it('cssClass is applied to action button (link component)', async function () {
  45. await getRootAndWait()
  46. const buttonWithClass = await webdriver.findElements(By.css('.action-button button.test-custom-class'))
  47. expect(buttonWithClass).to.have.length.at.least(1, 'Action button should have cssClass test-custom-class on the button')
  48. const classAttr = await buttonWithClass[0].getAttribute('class')
  49. expect(classAttr).to.include('test-custom-class')
  50. })
  51. it('custom theme applies background color to action button via cssClass', async function () {
  52. await getRootAndWait()
  53. await waitForThemeCss()
  54. const buttonWithClass = await webdriver.findElements(By.css('.action-button button.test-custom-class'))
  55. expect(buttonWithClass).to.have.length.at.least(1, 'Action button with test-custom-class should exist')
  56. await waitForCssColor(
  57. buttonWithClass[0],
  58. /rgba?\(\s*32\s*,\s*64\s*,\s*128\s*(,\s*1)?\s*\)/,
  59. 'theme action button background'
  60. )
  61. const bgColor = await buttonWithClass[0].getCssValue('background-color')
  62. expect(bgColor, 'Theme theme.css should set .action-button button.test-custom-class background to rgb(32, 64, 128)')
  63. .to.match(/rgba?\(\s*32\s*,\s*64\s*,\s*128\s*(,\s*1)?\s*\)/)
  64. })
  65. it('cssClass override: style rule targeting custom class wins over component styles', async function () {
  66. await getRootAndWait()
  67. const buttonWithClass = await webdriver.findElements(By.css('.action-button button.test-custom-class'))
  68. expect(buttonWithClass).to.have.length.at.least(1)
  69. const beforePx = await buttonWithClass[0].getCssValue('border-top-width')
  70. await webdriver.executeScript(`
  71. var style = document.getElementById('cssclass-test-override-style');
  72. if (!style) {
  73. style = document.createElement('style');
  74. style.id = 'cssclass-test-override-style';
  75. style.textContent = '.test-custom-class { border-top-width: 31px !important; }';
  76. document.head.appendChild(style);
  77. } else {
  78. style.textContent = '.test-custom-class { border-top-width: 31px !important; }';
  79. }
  80. `)
  81. await webdriver.sleep(150)
  82. const afterPx = await buttonWithClass[0].getCssValue('border-top-width')
  83. const afterNum = parseInt(afterPx, 10)
  84. expect(afterNum).to.be.greaterThan(10, 'Override targeting cssClass should win over component 1px (before=' + beforePx + ' after=' + afterPx + ') (#804)')
  85. })
  86. it('cssClass is applied to display component', async function () {
  87. await getRootAndWait()
  88. const displayElements = await webdriver.findElements(By.css('.display.test-display-class'))
  89. expect(displayElements).to.have.length.at.least(1, 'Display component with cssClass test-display-class should be in DOM')
  90. const classAttr = await displayElements[0].getAttribute('class')
  91. expect(classAttr).to.include('test-display-class')
  92. })
  93. it('custom theme applies background color to display component via cssClass', async function () {
  94. await getRootAndWait()
  95. await waitForThemeCss()
  96. const displayElements = await webdriver.findElements(By.css('.display.test-display-class'))
  97. expect(displayElements).to.have.length.at.least(1, 'Display with test-display-class should exist')
  98. await waitForCssColor(
  99. displayElements[0],
  100. /rgba?\(\s*64\s*,\s*128\s*,\s*192\s*(,\s*1)?\s*\)/,
  101. 'theme display background'
  102. )
  103. const bgColor = await displayElements[0].getCssValue('background-color')
  104. expect(bgColor, 'Theme theme.css should set .display.test-display-class background to rgb(64, 128, 192)')
  105. .to.match(/rgba?\(\s*64\s*,\s*128\s*,\s*192\s*(,\s*1)?\s*\)/)
  106. })
  107. })