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.

TestUploadMIMETypeTest.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package com.vaadin.tests.components.upload;
  2. import com.vaadin.testbench.elements.UploadElement;
  3. import com.vaadin.testbench.parallel.Browser;
  4. import com.vaadin.tests.tb3.MultiBrowserTest;
  5. import org.junit.Test;
  6. import org.openqa.selenium.By;
  7. import org.openqa.selenium.WebElement;
  8. import org.openqa.selenium.internal.WrapsElement;
  9. import org.openqa.selenium.remote.DesiredCapabilities;
  10. import org.openqa.selenium.remote.LocalFileDetector;
  11. import org.openqa.selenium.remote.RemoteWebElement;
  12. import java.io.BufferedWriter;
  13. import java.io.File;
  14. import java.io.FileWriter;
  15. import java.io.IOException;
  16. import java.util.List;
  17. import static com.vaadin.tests.components.upload.TestUploadMIMEType.TEST_MIME_TYPE;
  18. import static org.hamcrest.CoreMatchers.is;
  19. import static org.junit.Assert.assertThat;
  20. public class TestUploadMIMETypeTest extends MultiBrowserTest {
  21. @Override
  22. public void setup() throws Exception {
  23. super.setup();
  24. openTestURL();
  25. }
  26. @Test
  27. public void testAcceptAttribute() throws Exception {
  28. WebElement input = getInput();
  29. assertThat(input.getAttribute("accept"), is(TEST_MIME_TYPE));
  30. uploadFile();
  31. waitUntil(driver -> getSubmitButton().isEnabled());
  32. //Previous element is removed, getting a new one
  33. input = getInput();
  34. assertThat(
  35. String.format("Accept is expected to be %s , but was %s ",
  36. TEST_MIME_TYPE, input.getAttribute("accept")),
  37. input.getAttribute("accept"), is(TEST_MIME_TYPE));}
  38. private void uploadFile() throws Exception {
  39. File tempFile = createTempFile();
  40. fillPathToUploadInput(tempFile.getPath());
  41. getSubmitButton().click();
  42. }
  43. /**
  44. * @return The generated temp file handle
  45. * @throws IOException
  46. */
  47. private File createTempFile() throws IOException {
  48. File tempFile = File.createTempFile("TestFileUpload", ".pdf");
  49. BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
  50. writer.write(getTempFileContents());
  51. writer.close();
  52. tempFile.deleteOnExit();
  53. return tempFile;
  54. }
  55. private String getTempFileContents() {
  56. return "This is a test file!\nRow 2\nRow3";
  57. }
  58. private void fillPathToUploadInput(String tempFileName) throws Exception {
  59. // create a valid path in upload input element. Instead of selecting a
  60. // file by some file browsing dialog, we use the local path directly.
  61. WebElement input = getInput();
  62. setLocalFileDetector(input);
  63. input.sendKeys(tempFileName);
  64. }
  65. private WebElement getSubmitButton() {
  66. UploadElement upload = $(UploadElement.class).first();
  67. WebElement submitButton = upload.findElement(By.className("v-button"));
  68. return submitButton;
  69. }
  70. private WebElement getInput() {
  71. return getDriver().findElement(By.className("gwt-FileUpload"));
  72. }
  73. private void setLocalFileDetector(WebElement element) throws Exception {
  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. @Override
  87. public List<DesiredCapabilities> getBrowsersToTest() {
  88. // IE11 throws an `Unhandled Alert Exception`
  89. //https://stackoverflow.com/questions/23883071/unhandled-alert-exception-modal-dialog-present-selenium
  90. return getBrowserCapabilities(Browser.CHROME, Browser.FIREFOX);
  91. }
  92. }