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 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tests.components.upload;
  17. import java.io.BufferedWriter;
  18. import java.io.File;
  19. import java.io.FileWriter;
  20. import java.io.IOException;
  21. import java.math.BigInteger;
  22. import java.security.MessageDigest;
  23. import java.security.NoSuchAlgorithmException;
  24. import java.util.List;
  25. import org.junit.Assert;
  26. import org.junit.Test;
  27. import org.openqa.selenium.By;
  28. import org.openqa.selenium.WebElement;
  29. import org.openqa.selenium.internal.WrapsElement;
  30. import org.openqa.selenium.remote.DesiredCapabilities;
  31. import org.openqa.selenium.remote.LocalFileDetector;
  32. import org.openqa.selenium.remote.RemoteWebElement;
  33. import com.vaadin.testbench.elements.UploadElement;
  34. import com.vaadin.tests.tb3.MultiBrowserTest;
  35. public class TestFileUploadTest extends MultiBrowserTest {
  36. @Override
  37. public List<DesiredCapabilities> getBrowsersToTest() {
  38. // PhantomJS fails to upload files for unknown reasons
  39. return getBrowsersExcludingPhantomJS();
  40. }
  41. @Test
  42. public void testUploadAnyFile() throws Exception {
  43. openTestURL();
  44. File tempFile = createTempFile();
  45. fillPathToUploadInput(tempFile.getPath());
  46. getSubmitButton().click();
  47. String expected = String.format(
  48. "1. Upload finished. Name: %s, Size: %s, md5: %s",
  49. tempFile.getName(), getTempFileContents().length(),
  50. md5(getTempFileContents()));
  51. String actual = getLogRow(0);
  52. Assert.assertEquals("Upload log row does not match expected", expected,
  53. actual);
  54. }
  55. private String md5(String string) throws NoSuchAlgorithmException {
  56. byte[] digest = MessageDigest.getInstance("MD5").digest(
  57. string.getBytes());
  58. BigInteger bigInt = new BigInteger(1, digest);
  59. String hashtext = bigInt.toString(16);
  60. return hashtext;
  61. }
  62. /**
  63. * @return The generated temp file handle
  64. * @throws IOException
  65. */
  66. private File createTempFile() throws IOException {
  67. File tempFile = File.createTempFile("TestFileUpload", ".txt");
  68. BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
  69. writer.write(getTempFileContents());
  70. writer.close();
  71. tempFile.deleteOnExit();
  72. return tempFile;
  73. }
  74. private String getTempFileContents() {
  75. return "This is a test file!\nRow 2\nRow3";
  76. }
  77. private void fillPathToUploadInput(String tempFileName) throws Exception {
  78. // create a valid path in upload input element. Instead of selecting a
  79. // file by some file browsing dialog, we use the local path directly.
  80. WebElement input = getInput();
  81. setLocalFileDetector(input);
  82. input.sendKeys(tempFileName);
  83. }
  84. private WebElement getSubmitButton() {
  85. UploadElement upload = $(UploadElement.class).first();
  86. WebElement submitButton = upload.findElement(By.className("v-button"));
  87. return submitButton;
  88. }
  89. private WebElement getInput() {
  90. return getDriver().findElement(By.className("gwt-FileUpload"));
  91. }
  92. private void setLocalFileDetector(WebElement element) throws Exception {
  93. if (getRunLocallyBrowser() != null) {
  94. return;
  95. }
  96. if (element instanceof WrapsElement) {
  97. element = ((WrapsElement) element).getWrappedElement();
  98. }
  99. if (element instanceof RemoteWebElement) {
  100. ((RemoteWebElement) element)
  101. .setFileDetector(new LocalFileDetector());
  102. } else {
  103. throw new IllegalArgumentException(
  104. "Expected argument of type RemoteWebElement, received "
  105. + element.getClass().getName());
  106. }
  107. }
  108. }