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.

TestFileUploadTest.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package com.vaadin.v7.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 java.math.BigInteger;
  8. import java.security.MessageDigest;
  9. import java.security.NoSuchAlgorithmException;
  10. import org.junit.Test;
  11. import org.openqa.selenium.By;
  12. import org.openqa.selenium.WebElement;
  13. import org.openqa.selenium.internal.WrapsElement;
  14. import org.openqa.selenium.remote.LocalFileDetector;
  15. import org.openqa.selenium.remote.RemoteWebElement;
  16. import com.vaadin.testbench.elements.UploadElement;
  17. import com.vaadin.tests.tb3.MultiBrowserTest;
  18. public class TestFileUploadTest extends MultiBrowserTest {
  19. @Test
  20. public void testUploadAnyFile() throws Exception {
  21. openTestURL();
  22. File tempFile = createTempFile();
  23. fillPathToUploadInput(tempFile.getPath());
  24. getSubmitButton().click();
  25. String expected = String.format(
  26. "1. Upload finished. Name: %s, Size: %s, md5: %s",
  27. tempFile.getName(), getTempFileContents().length(),
  28. md5(getTempFileContents()));
  29. String actual = getLogRow(0);
  30. assertEquals("Upload log row does not match expected", expected,
  31. actual);
  32. }
  33. private String md5(String string) throws NoSuchAlgorithmException {
  34. byte[] digest = MessageDigest.getInstance("MD5")
  35. .digest(string.getBytes());
  36. BigInteger bigInt = new BigInteger(1, digest);
  37. String hashtext = bigInt.toString(16);
  38. return hashtext;
  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. return "This is a test file!\nRow 2\nRow3";
  54. }
  55. private void fillPathToUploadInput(String tempFileName) throws Exception {
  56. // create a valid path in upload input element. Instead of selecting a
  57. // file by some file browsing dialog, we use the local path directly.
  58. WebElement input = getInput();
  59. setLocalFileDetector(input);
  60. input.sendKeys(tempFileName);
  61. }
  62. private WebElement getSubmitButton() {
  63. UploadElement upload = $(UploadElement.class).first();
  64. WebElement submitButton = upload.findElement(By.className("v-button"));
  65. return submitButton;
  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. }