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.

DisablingUploadTest.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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.WebElement;
  8. import org.openqa.selenium.internal.WrapsElement;
  9. import org.openqa.selenium.remote.LocalFileDetector;
  10. import org.openqa.selenium.remote.RemoteWebElement;
  11. import com.vaadin.testbench.By;
  12. import com.vaadin.testbench.elements.ButtonElement;
  13. import com.vaadin.testbench.elements.UploadElement;
  14. import com.vaadin.tests.tb3.SingleBrowserTest;
  15. import com.vaadin.tests.util.LoremIpsum;
  16. import static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertFalse;
  18. import static org.junit.Assert.assertTrue;
  19. public class DisablingUploadTest extends SingleBrowserTest {
  20. ButtonElement button;
  21. ButtonElement pushButton;
  22. ButtonElement stateButton;
  23. @Override
  24. public void setup() throws Exception {
  25. super.setup();
  26. openTestURL();
  27. }
  28. @Test
  29. public void buttonWorksAsExpected() {
  30. buttonGroup();
  31. // Disable button is working
  32. assertTrue("Upload button should be enabled",
  33. getSubmitButton().isEnabled());
  34. button.click();
  35. assertFalse("Upload button should be disabled",
  36. getSubmitButton().isEnabled());
  37. // pushmode button is working
  38. assertEquals("Set the Push Mode", pushButton.getCaption());
  39. pushButton.click();
  40. sleep(100);
  41. assertEquals("enable push mode", pushButton.getCaption());
  42. pushButton.click();
  43. sleep(100);
  44. assertEquals("disable push mode", pushButton.getCaption());
  45. // upload button state is correct
  46. assertEquals("true", stateButton.getCaption());
  47. stateButton.click();
  48. sleep(100);
  49. assertEquals("false", stateButton.getCaption());
  50. }
  51. @Test
  52. public void pushEnabled_uploadFile_uploadButtonDisabled() throws Exception {
  53. buttonGroup();
  54. uploadFile(false);
  55. String expected = "2. File has been uploaded.";
  56. String actual = getLogRow(0);
  57. assertEquals("Upload log row does not match expected", expected,
  58. actual);
  59. stateButton.click();
  60. sleep(100);
  61. assertEquals("false", stateButton.getCaption());
  62. uploadFile(false);
  63. //assert no new log
  64. assertEquals("Upload log row does not match expected", expected,
  65. actual);
  66. }
  67. @Test
  68. public void pushDisabled_uploadFile_uploadButtonDisabled() throws Exception {
  69. buttonGroup();
  70. pushButton.click();
  71. uploadFile(false);
  72. String expected = "2. File has been uploaded.";
  73. String actual = getLogRow(0);
  74. assertEquals("Upload log row does not match expected", expected,
  75. actual);
  76. stateButton.click();
  77. sleep(100);
  78. assertEquals("false", stateButton.getCaption());
  79. uploadFile(false);
  80. //assert no new log
  81. assertEquals("Upload log row does not match expected", expected,
  82. actual);
  83. }
  84. @Test
  85. public void pushEnabled_uploadLargeFile_uploadButtonDisabled() throws Exception {
  86. buttonGroup();
  87. uploadFile(true);
  88. String expected = "2. File has been uploaded.";
  89. String actual = getLogRow(0);
  90. assertEquals("Upload log row does not match expected", expected,
  91. actual);
  92. stateButton.click();
  93. sleep(100);
  94. assertEquals("false", stateButton.getCaption());
  95. uploadFile(true);
  96. //assert no new log
  97. assertEquals("Upload log row does not match expected", expected,
  98. actual);
  99. }
  100. @Test
  101. public void pushDisabled_uploadLargeFile_uploadButtonDisabled() throws Exception {
  102. buttonGroup();
  103. pushButton.click();
  104. uploadFile(true);
  105. String expected = "2. File has been uploaded.";
  106. String actual = getLogRow(0);
  107. assertEquals("Upload log row does not match expected", expected,
  108. actual);
  109. stateButton.click();
  110. sleep(100);
  111. assertEquals("false", stateButton.getCaption());
  112. uploadFile(true);
  113. //assert no new log
  114. assertEquals("Upload log row does not match expected", expected,
  115. actual);
  116. }
  117. private void buttonGroup() {
  118. button = $(ButtonElement.class).id("button-id");
  119. pushButton = $(ButtonElement.class).id("push-button");
  120. stateButton = $(ButtonElement.class).id("state-button");
  121. }
  122. private void uploadFile(boolean large) throws Exception {
  123. File tempFile = createTempFile(large);
  124. fillPathToUploadInput(tempFile.getPath());
  125. getSubmitButton().click();
  126. sleep(100);
  127. }
  128. /**
  129. * @return The generated temp file handle
  130. * @throws IOException
  131. */
  132. private File createTempFile(boolean large) throws IOException {
  133. File tempFile = File.createTempFile("TestFileUpload", ".txt");
  134. BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
  135. if(large) {
  136. writer.write(getLargeTempFileContents());
  137. } else {
  138. writer.write(getTempFileContents());
  139. }
  140. writer.close();
  141. tempFile.deleteOnExit();
  142. return tempFile;
  143. }
  144. private String getTempFileContents() {
  145. return "This is a test file!\nRow 2\nRow3";
  146. }
  147. private String getLargeTempFileContents() {
  148. return LoremIpsum.get();
  149. }
  150. private void fillPathToUploadInput(String tempFileName) throws Exception {
  151. // create a valid path in upload input element. Instead of selecting a
  152. // file by some file browsing dialog, we use the local path directly.
  153. WebElement input = getInput();
  154. setLocalFileDetector(input);
  155. input.sendKeys(tempFileName);
  156. }
  157. private WebElement getSubmitButton() {
  158. UploadElement upload = $(UploadElement.class).first();
  159. WebElement submitButton = upload.findElement(By.className("v-button"));
  160. return submitButton;
  161. }
  162. private WebElement getInput() {
  163. return getDriver().findElement(By.className("gwt-FileUpload"));
  164. }
  165. private void setLocalFileDetector(WebElement element) throws Exception {
  166. if (getRunLocallyBrowser() != null) {
  167. return;
  168. }
  169. if (element instanceof WrapsElement) {
  170. element = ((WrapsElement) element).getWrappedElement();
  171. }
  172. if (element instanceof RemoteWebElement) {
  173. ((RemoteWebElement) element)
  174. .setFileDetector(new LocalFileDetector());
  175. } else {
  176. throw new IllegalArgumentException(
  177. "Expected argument of type RemoteWebElement, received "
  178. + element.getClass().getName());
  179. }
  180. }
  181. }