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.

UploadChangeListenerTest.java 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package com.vaadin.tests.components.upload;
  2. import static org.junit.Assert.assertEquals;
  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.tests.tb3.MultiBrowserTest;
  14. public class UploadChangeListenerTest extends MultiBrowserTest {
  15. @Test
  16. public void changeListenerWorksAfterFirstUpload() throws IOException {
  17. openTestURL();
  18. WebElement upload = findElement(By.className("v-button"));
  19. File tempFile = createTempFile();
  20. fillPathToUploadInput(tempFile.getPath());
  21. assertEquals("1. change", getLogRow(0));
  22. upload.click();
  23. assertEquals("2. finished", getLogRow(0));
  24. tempFile = createTempFile();
  25. fillPathToUploadInput(tempFile.getPath());
  26. assertEquals("3. change", getLogRow(0));
  27. }
  28. /**
  29. * @return The generated temp file handle
  30. * @throws IOException
  31. */
  32. private File createTempFile() throws IOException {
  33. File tempFile = File.createTempFile("TestFileUpload", ".txt");
  34. BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
  35. writer.write(getTempFileContents());
  36. writer.close();
  37. tempFile.deleteOnExit();
  38. return tempFile;
  39. }
  40. private String getTempFileContents() {
  41. StringBuilder sb = new StringBuilder("This is a small test file.");
  42. sb.append("\n");
  43. sb.append("Very small.");
  44. return sb.toString();
  45. }
  46. private void fillPathToUploadInput(String tempFileName) {
  47. // create a valid path in upload input element. Instead of selecting a
  48. // file by some file browsing dialog, we use the local path directly.
  49. WebElement input = getInput();
  50. setLocalFileDetector(input);
  51. input.sendKeys(tempFileName);
  52. }
  53. private WebElement getInput() {
  54. return getDriver().findElement(By.className("gwt-FileUpload"));
  55. }
  56. private void setLocalFileDetector(WebElement element) {
  57. if (getRunLocallyBrowser() != null) {
  58. return;
  59. }
  60. if (element instanceof WrapsElement) {
  61. element = ((WrapsElement) element).getWrappedElement();
  62. }
  63. if (element instanceof RemoteWebElement) {
  64. ((RemoteWebElement) element)
  65. .setFileDetector(new LocalFileDetector());
  66. } else {
  67. throw new IllegalArgumentException(
  68. "Expected argument of type RemoteWebElement, received "
  69. + element.getClass().getName());
  70. }
  71. }
  72. }