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.

InterruptUploadTest.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package com.vaadin.tests.components.upload;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import org.junit.Test;
  7. import org.openqa.selenium.By;
  8. import org.openqa.selenium.WebDriver;
  9. import org.openqa.selenium.WebElement;
  10. import org.openqa.selenium.internal.WrapsElement;
  11. import org.openqa.selenium.remote.LocalFileDetector;
  12. import org.openqa.selenium.remote.RemoteWebElement;
  13. import org.openqa.selenium.support.ui.ExpectedCondition;
  14. import com.vaadin.testbench.elements.ButtonElement;
  15. import com.vaadin.testbench.elements.LabelElement;
  16. import com.vaadin.testbench.elements.WindowElement;
  17. import com.vaadin.tests.tb3.MultiBrowserTest;
  18. import com.vaadin.tests.util.LoremIpsum;
  19. public class InterruptUploadTest extends MultiBrowserTest {
  20. private static final String EXPECTED_COUNTER_TEXT = " (counting interrupted at ";
  21. @Test
  22. public void testInterruptUpload() throws Exception {
  23. openTestURL();
  24. File tempFile = createTempFile();
  25. fillPathToUploadInput(tempFile.getPath());
  26. waitForElementPresent(By.className("v-window"));
  27. $(ButtonElement.class).caption("Cancel").first().click();
  28. waitUntilInterruptionRegistered();
  29. $(WindowElement.class).first().close();
  30. waitForElementNotPresent(By.className("v-window"));
  31. // Check if second upload happens
  32. tempFile = createTempFile();
  33. fillPathToUploadInput(tempFile.getPath());
  34. waitForElementPresent(By.className("v-window"));
  35. $(ButtonElement.class).caption("Cancel").first().click();
  36. waitUntilInterruptionRegistered();
  37. }
  38. private void waitUntilInterruptionRegistered() {
  39. waitUntil(new ExpectedCondition<Boolean>() {
  40. String actual;
  41. @Override
  42. public Boolean apply(WebDriver arg0) {
  43. actual = $(LabelElement.class).caption("Line breaks counted")
  44. .first().getText();
  45. return actual.contains(EXPECTED_COUNTER_TEXT);
  46. }
  47. @Override
  48. public String toString() {
  49. // Expected condition failed: waiting for ...
  50. return "line break count note to mention interruption (was: "
  51. + actual + ")";
  52. }
  53. });
  54. }
  55. /**
  56. * @return The generated temp file handle
  57. * @throws IOException
  58. */
  59. private File createTempFile() throws IOException {
  60. File tempFile = File.createTempFile("TestFileUpload", ".txt");
  61. BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
  62. writer.write(getTempFileContents());
  63. writer.close();
  64. tempFile.deleteOnExit();
  65. return tempFile;
  66. }
  67. private String getTempFileContents() {
  68. StringBuilder sb = new StringBuilder("This is a big test file!");
  69. for (int i = 0; i < 70; ++i) {
  70. sb.append("\n");
  71. sb.append(LoremIpsum.get());
  72. }
  73. return sb.toString();
  74. }
  75. private void fillPathToUploadInput(String tempFileName) {
  76. // create a valid path in upload input element. Instead of selecting a
  77. // file by some file browsing dialog, we use the local path directly.
  78. WebElement input = getInput();
  79. setLocalFileDetector(input);
  80. input.sendKeys(tempFileName);
  81. }
  82. private WebElement getInput() {
  83. return getDriver().findElement(By.className("gwt-FileUpload"));
  84. }
  85. private void setLocalFileDetector(WebElement element) {
  86. if (getRunLocallyBrowser() != null) {
  87. return;
  88. }
  89. if (element instanceof WrapsElement) {
  90. element = ((WrapsElement) element).getWrappedElement();
  91. }
  92. if (element instanceof RemoteWebElement) {
  93. ((RemoteWebElement) element)
  94. .setFileDetector(new LocalFileDetector());
  95. } else {
  96. throw new IllegalArgumentException(
  97. "Expected argument of type RemoteWebElement, received "
  98. + element.getClass().getName());
  99. }
  100. }
  101. }