Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

InterruptUploadTest.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package com.vaadin.tests.components.upload;
  2. import static org.junit.Assert.assertTrue;
  3. import java.io.BufferedWriter;
  4. import java.io.File;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import org.junit.Test;
  8. import org.openqa.selenium.By;
  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 com.vaadin.testbench.elements.ButtonElement;
  14. import com.vaadin.testbench.elements.LabelElement;
  15. import com.vaadin.testbench.elements.WindowElement;
  16. import com.vaadin.tests.tb3.MultiBrowserTest;
  17. import com.vaadin.tests.util.LoremIpsum;
  18. public class InterruptUploadTest extends MultiBrowserTest {
  19. @Test
  20. public void testInterruptUpload() throws Exception {
  21. openTestURL();
  22. File tempFile = createTempFile();
  23. fillPathToUploadInput(tempFile.getPath());
  24. waitForElementPresent(By.className("v-window"));
  25. $(ButtonElement.class).caption("Cancel").first().click();
  26. // Wait for 1 second for server to finish up.
  27. Thread.sleep(1000);
  28. String expected = " (counting interrupted at ";
  29. String actual = $(LabelElement.class).caption("Line breaks counted")
  30. .first().getText();
  31. assertTrue("Line break count note does not match expected (was: "
  32. + actual + ")", actual.contains(expected));
  33. $(WindowElement.class).first().close();
  34. waitForElementNotPresent(By.className("v-window"));
  35. tempFile = createTempFile();
  36. fillPathToUploadInput(tempFile.getPath());
  37. waitForElementPresent(By.className("v-window"));
  38. $(ButtonElement.class).caption("Cancel").first().click();
  39. }
  40. /**
  41. * @return The generated temp file handle
  42. * @throws IOException
  43. */
  44. private File createTempFile() throws IOException {
  45. File tempFile = File.createTempFile("TestFileUpload", ".txt");
  46. BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
  47. writer.write(getTempFileContents());
  48. writer.close();
  49. tempFile.deleteOnExit();
  50. return tempFile;
  51. }
  52. private String getTempFileContents() {
  53. StringBuilder sb = new StringBuilder("This is a big test file!");
  54. for (int i = 0; i < 70; ++i) {
  55. sb.append("\n");
  56. sb.append(LoremIpsum.get());
  57. }
  58. return sb.toString();
  59. }
  60. private void fillPathToUploadInput(String tempFileName) throws Exception {
  61. // create a valid path in upload input element. Instead of selecting a
  62. // file by some file browsing dialog, we use the local path directly.
  63. WebElement input = getInput();
  64. setLocalFileDetector(input);
  65. input.sendKeys(tempFileName);
  66. }
  67. private WebElement getInput() {
  68. return getDriver().findElement(By.className("gwt-FileUpload"));
  69. }
  70. private void setLocalFileDetector(WebElement element) throws Exception {
  71. if (getRunLocallyBrowser() != null) {
  72. return;
  73. }
  74. if (element instanceof WrapsElement) {
  75. element = ((WrapsElement) element).getWrappedElement();
  76. }
  77. if (element instanceof RemoteWebElement) {
  78. ((RemoteWebElement) element)
  79. .setFileDetector(new LocalFileDetector());
  80. } else {
  81. throw new IllegalArgumentException(
  82. "Expected argument of type RemoteWebElement, received "
  83. + element.getClass().getName());
  84. }
  85. }
  86. }