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.

UploadInTabsheetTest.java 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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.WebElement;
  9. import org.openqa.selenium.internal.WrapsElement;
  10. import org.openqa.selenium.remote.LocalFileDetector;
  11. import org.openqa.selenium.remote.RemoteWebElement;
  12. import com.vaadin.testbench.elements.UploadElement;
  13. import com.vaadin.tests.tb3.MultiBrowserTest;
  14. /**
  15. * Verifies that there's no client side errors when changing a tab containing
  16. * Upload right after uploading is succeeded (#8728)
  17. */
  18. public class UploadInTabsheetTest extends MultiBrowserTest {
  19. @Test
  20. public void testThatChangingTabAfterUploadDoesntCauseErrors()
  21. throws Exception {
  22. setDebug(true);
  23. openTestURL();
  24. File tempFile = createTempFile();
  25. fillPathToUploadInput(tempFile.getPath());
  26. getSubmitButton().click();
  27. assertNoErrorNotifications();
  28. }
  29. /**
  30. * @return The generated temp file handle
  31. * @throws IOException
  32. */
  33. private File createTempFile() throws IOException {
  34. File tempFile = File.createTempFile("TestFileUpload", ".txt");
  35. BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
  36. writer.write(getTempFileContents());
  37. writer.close();
  38. tempFile.deleteOnExit();
  39. return tempFile;
  40. }
  41. private String getTempFileContents() {
  42. return "This is a test file!\nRow 2\nRow3";
  43. }
  44. private void fillPathToUploadInput(String tempFileName) throws Exception {
  45. // create a valid path in upload input element. Instead of selecting a
  46. // file by some file browsing dialog, we use the local path directly.
  47. WebElement input = getInput();
  48. setLocalFileDetector(input);
  49. input.sendKeys(tempFileName);
  50. }
  51. private WebElement getSubmitButton() {
  52. UploadElement upload = $(UploadElement.class).first();
  53. WebElement submitButton = upload.findElement(By.className("v-button"));
  54. return submitButton;
  55. }
  56. private WebElement getInput() {
  57. return getDriver().findElement(By.className("gwt-FileUpload"));
  58. }
  59. private void setLocalFileDetector(WebElement element) throws Exception {
  60. if (getRunLocallyBrowser() != null) {
  61. return;
  62. }
  63. if (element instanceof WrapsElement) {
  64. element = ((WrapsElement) element).getWrappedElement();
  65. }
  66. if (element instanceof RemoteWebElement) {
  67. ((RemoteWebElement) element)
  68. .setFileDetector(new LocalFileDetector());
  69. } else {
  70. throw new IllegalArgumentException(
  71. "Expected argument of type RemoteWebElement, received "
  72. + element.getClass().getName());
  73. }
  74. }
  75. }