You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ReconnectDialogThemeTest.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package com.vaadin.tests.application;
  2. import static org.junit.Assert.assertEquals;
  3. import static org.junit.Assert.fail;
  4. import java.awt.image.BufferedImage;
  5. import java.io.ByteArrayInputStream;
  6. import java.io.IOException;
  7. import javax.imageio.ImageIO;
  8. import org.junit.Test;
  9. import org.openqa.selenium.By;
  10. import org.openqa.selenium.OutputType;
  11. import org.openqa.selenium.TakesScreenshot;
  12. import org.openqa.selenium.WebElement;
  13. import com.vaadin.testbench.elements.ButtonElement;
  14. import com.vaadin.testbench.parallel.TestCategory;
  15. import com.vaadin.tests.tb3.CustomTestBenchCommandExecutor;
  16. import com.vaadin.tests.tb3.MultiBrowserThemeTestWithProxy;
  17. @TestCategory("communication")
  18. public class ReconnectDialogThemeTest extends MultiBrowserThemeTestWithProxy {
  19. static By reconnectDialogBy = By.className("v-reconnect-dialog");
  20. @Test
  21. public void reconnectDialogTheme() throws IOException {
  22. openTestURL();
  23. ButtonElement helloButton = $(ButtonElement.class).caption("Say hello")
  24. .first();
  25. helloButton.click();
  26. assertEquals("1. Hello from the server", getLogRow(0));
  27. disconnectProxy();
  28. helloButton.click();
  29. testBench().disableWaitForVaadin();
  30. waitUntil(driver -> isElementPresent(reconnectDialogBy));
  31. WebElement dialog = findElement(reconnectDialogBy);
  32. WebElement spinner = dialog.findElement(By.className("spinner"));
  33. // Hide spinner to make screenshot stable
  34. executeScript("arguments[0].style.visibility='hidden';", spinner);
  35. compareScreen("onscreen-without-spinner");
  36. // Show spinner and make sure it is shown by comparing to the screenshot
  37. // without a spinner
  38. executeScript("arguments[0].style.visibility='visible';", spinner);
  39. BufferedImage fullScreen = ImageIO
  40. .read(new ByteArrayInputStream(((TakesScreenshot) getDriver())
  41. .getScreenshotAs(OutputType.BYTES)));
  42. BufferedImage spinnerImage = CustomTestBenchCommandExecutor
  43. .cropToElement(spinner, fullScreen);
  44. assertHasManyColors("Spinner is not shown", spinnerImage);
  45. }
  46. @Test
  47. public void gaveUpTheme() throws IOException {
  48. openTestURL("reconnectAttempts=3");
  49. waitUntil(input -> {
  50. try {
  51. return $(ButtonElement.class).first() != null;
  52. } catch (Exception e) {
  53. return false;
  54. }
  55. });
  56. disconnectProxy();
  57. $(ButtonElement.class).first().click();
  58. waitForReconnectDialogWithText("Server connection lost.");
  59. compareScreen("gaveupdialog");
  60. }
  61. private void waitForReconnectDialogWithText(final String text) {
  62. waitUntil(input -> {
  63. try {
  64. final WebElement reconnectDialog = findElement(
  65. ReconnectDialogThemeTest.reconnectDialogBy);
  66. return reconnectDialog.findElement(By.className("text"))
  67. .getText().equals(text);
  68. } catch (Exception e) {
  69. return false;
  70. }
  71. }, 10);
  72. }
  73. private void assertHasManyColors(String message,
  74. BufferedImage spinnerImage) {
  75. int backgroundColor = spinnerImage.getRGB(0, 0);
  76. for (int x = 0; x < spinnerImage.getWidth(); x++) {
  77. for (int y = 0; y < spinnerImage.getHeight(); y++) {
  78. if (Math.abs(
  79. spinnerImage.getRGB(x, y) - backgroundColor) > 50) {
  80. return;
  81. }
  82. }
  83. }
  84. fail(message);
  85. }
  86. @Override
  87. protected Class<?> getUIClass() {
  88. return ReconnectDialogUI.class;
  89. }
  90. }