diff options
author | Leif Åstrand <leif@vaadin.com> | 2014-07-22 10:24:57 +0300 |
---|---|---|
committer | Bogdan Udrescu <bogdan@vaadin.com> | 2014-07-23 15:11:01 +0300 |
commit | b67c28b66f3d7408f8f9f44c7c620f9f43bce8f7 (patch) | |
tree | bec2d9d4b8dfb55b0ae81bb463f15c4cf8bf1468 | |
parent | 7a3354146bb4d0996b3c79d25d082b84a0d6bf2c (diff) | |
download | vaadin-framework-b67c28b66f3d7408f8f9f44c7c620f9f43bce8f7.tar.gz vaadin-framework-b67c28b66f3d7408f8f9f44c7c620f9f43bce8f7.zip |
Simulate @RunLocally using eclipse-run-selected-test.properties (#14272)
Conflicts when cherry-pick (the file was missing):
uitest/src/com/vaadin/tests/components/upload/TestFileUploadTest.java
Change-Id: I4eb9409629f64c17f39b1560062e763270f1f582
5 files changed, 199 insertions, 14 deletions
diff --git a/uitest/eclipse-run-selected-test.properties b/uitest/eclipse-run-selected-test.properties index cbd1ab1cef..70010fd1da 100644 --- a/uitest/eclipse-run-selected-test.properties +++ b/uitest/eclipse-run-selected-test.properties @@ -1,4 +1,11 @@ ; +; This is an example property file showing how to control how TestBench is used +; in the Vaadin Framework project. You should not modify this file since it's +; under version control. Instead, create a copy of it inside the /work/ folder +; in the project and make your customizations to that file. +; + +; ; For both TestBench 2 and 3 ; @@ -8,6 +15,14 @@ com.vaadin.testbench.screenshot.directory=<enter the full path to the screenshot ; +; For only TestBench 3 +; + +; Simulates @RunLocally with the given value on all test classes without a @RunLocally annotation. +; com.vaadin.testbench.runLocally=firefox + + +; ; For only TestBench 2 ; diff --git a/uitest/src/com/vaadin/tests/components/upload/TestFileUploadTest.java b/uitest/src/com/vaadin/tests/components/upload/TestFileUploadTest.java new file mode 100644 index 0000000000..ae966a5b07 --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/upload/TestFileUploadTest.java @@ -0,0 +1,128 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.components.upload; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.math.BigInteger; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.List; + +import org.junit.Assert; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.internal.WrapsElement; +import org.openqa.selenium.remote.DesiredCapabilities; +import org.openqa.selenium.remote.LocalFileDetector; +import org.openqa.selenium.remote.RemoteWebElement; + +import com.vaadin.testbench.elements.UploadElement; +import com.vaadin.tests.tb3.MultiBrowserTest; + +public class TestFileUploadTest extends MultiBrowserTest { + + @Override + public List<DesiredCapabilities> getBrowsersToTest() { + // PhantomJS fails to upload files for unknown reasons + List<DesiredCapabilities> b = super.getBrowsersToTest(); + b.remove(Browser.PHANTOMJS.getDesiredCapabilities()); + return b; + } + + @Test + public void testUploadAnyFile() throws Exception { + openTestURL(); + + File tempFile = createTempFile(); + fillPathToUploadInput(tempFile.getPath()); + + getSubmitButton().click(); + + String expected = String.format( + "1. Upload finished. Name: %s, Size: %s, md5: %s", + tempFile.getName(), getTempFileContents().length(), + md5(getTempFileContents())); + + String actual = getLogRow(0); + Assert.assertEquals("Upload log row does not match expected", expected, + actual); + } + + private String md5(String string) throws NoSuchAlgorithmException { + byte[] digest = MessageDigest.getInstance("MD5").digest( + string.getBytes()); + BigInteger bigInt = new BigInteger(1, digest); + String hashtext = bigInt.toString(16); + return hashtext; + } + + /** + * @return The generated temp file handle + * @throws IOException + */ + private File createTempFile() throws IOException { + File tempFile = File.createTempFile("TestFileUpload", ".txt"); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + writer.write(getTempFileContents()); + writer.close(); + tempFile.deleteOnExit(); + return tempFile; + } + + private String getTempFileContents() { + return "This is a test file!\nRow 2\nRow3"; + } + + private void fillPathToUploadInput(String tempFileName) throws Exception { + // create a valid path in upload input element. Instead of selecting a + // file by some file browsing dialog, we use the local path directly. + WebElement input = getInput(); + setLocalFileDetector(input); + input.sendKeys(tempFileName); + } + + private WebElement getSubmitButton() { + UploadElement upload = $(UploadElement.class).first(); + WebElement submitButton = upload.findElement(By.className("v-button")); + return submitButton; + } + + private WebElement getInput() { + return getDriver().findElement(By.className("gwt-FileUpload")); + } + + private void setLocalFileDetector(WebElement element) throws Exception { + if (getRunLocallyBrowser() != null) { + return; + } + + if (element instanceof WrapsElement) { + element = ((WrapsElement) element).getWrappedElement(); + } + if (element instanceof RemoteWebElement) { + ((RemoteWebElement) element) + .setFileDetector(new LocalFileDetector()); + } else { + throw new IllegalArgumentException( + "Expected argument of type RemoteWebElement, received " + + element.getClass().getName()); + } + } +} diff --git a/uitest/src/com/vaadin/tests/tb3/AbstractTB3Test.java b/uitest/src/com/vaadin/tests/tb3/AbstractTB3Test.java index 24ebc1a57a..cfc7b37c98 100644 --- a/uitest/src/com/vaadin/tests/tb3/AbstractTB3Test.java +++ b/uitest/src/com/vaadin/tests/tb3/AbstractTB3Test.java @@ -122,13 +122,13 @@ public abstract class AbstractTB3Test extends TestBenchTestCase { protected void setupDriver() throws Exception { DesiredCapabilities capabilities; - RunLocally runLocally = getClass().getAnnotation(RunLocally.class); - if (runLocally != null) { + Browser runLocallyBrowser = getRunLocallyBrowser(); + if (runLocallyBrowser != null) { if (System.getenv().containsKey("TEAMCITY_VERSION")) { throw new RuntimeException( "@RunLocally is not supported for tests run on the build server"); } - capabilities = runLocally.value().getDesiredCapabilities(); + capabilities = runLocallyBrowser.getDesiredCapabilities(); setupLocalDriver(capabilities); } else { capabilities = getDesiredCapabilities(); @@ -158,6 +158,15 @@ public abstract class AbstractTB3Test extends TestBenchTestCase { } + protected Browser getRunLocallyBrowser() { + RunLocally runLocally = getClass().getAnnotation(RunLocally.class); + if (runLocally != null) { + return runLocally.value(); + } else { + return null; + } + } + protected WebElement getTooltipElement() { return getDriver().findElement(com.vaadin.testbench.By.className("v-tooltip-text")); } diff --git a/uitest/src/com/vaadin/tests/tb3/PrivateTB3Configuration.java b/uitest/src/com/vaadin/tests/tb3/PrivateTB3Configuration.java index faa74d6c9d..15ca97f701 100644 --- a/uitest/src/com/vaadin/tests/tb3/PrivateTB3Configuration.java +++ b/uitest/src/com/vaadin/tests/tb3/PrivateTB3Configuration.java @@ -22,6 +22,7 @@ import java.io.IOException; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; +import java.util.Arrays; import java.util.Enumeration; import java.util.Properties; @@ -34,6 +35,7 @@ import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.safari.SafariDriver; import com.vaadin.testbench.TestBench; +import com.vaadin.tests.tb3.MultiBrowserTest.Browser; /** * Provides values for parameters which depend on where the test is run. @@ -43,6 +45,7 @@ import com.vaadin.testbench.TestBench; * @author Vaadin Ltd */ public abstract class PrivateTB3Configuration extends ScreenshotTB3Test { + private static final String RUN_LOCALLY_PROPERTY = "com.vaadin.testbench.runLocally"; private static final String HOSTNAME_PROPERTY = "com.vaadin.testbench.deployment.hostname"; private static final String PORT_PROPERTY = "com.vaadin.testbench.deployment.port"; private static final Properties properties = new Properties(); @@ -67,6 +70,16 @@ public abstract class PrivateTB3Configuration extends ScreenshotTB3Test { return property; } + private static String getSource(String propertyName) { + if (properties.containsKey(propertyName)) { + return propertiesFile.getAbsolutePath(); + } else if (System.getProperty(propertyName) != null) { + return "System.getProperty()"; + } else { + return null; + } + } + @Override protected String getScreenshotDirectory() { String screenshotDirectory = getProperty("com.vaadin.testbench.screenshot.directory"); @@ -84,7 +97,7 @@ public abstract class PrivateTB3Configuration extends ScreenshotTB3Test { @Override protected String getDeploymentHostname() { - if (getClass().getAnnotation(RunLocally.class) != null) { + if (getRunLocallyBrowser() != null) { return "localhost"; } return getConfiguredDeploymentHostname(); @@ -210,4 +223,28 @@ public abstract class PrivateTB3Configuration extends ScreenshotTB3Test { setDriver(TestBench.createDriver(driver)); setDesiredCapabilities(desiredCapabilities); } + + @Override + protected Browser getRunLocallyBrowser() { + Browser runLocallyBrowser = super.getRunLocallyBrowser(); + if (runLocallyBrowser != null) { + // Always use annotation value if present + return runLocallyBrowser; + } + + String runLocallyValue = getProperty(RUN_LOCALLY_PROPERTY); + if (runLocallyValue == null || runLocallyValue.trim().isEmpty()) { + return null; + } + + String browserName = runLocallyValue.trim().toUpperCase(); + try { + return Browser.valueOf(browserName); + } catch (IllegalArgumentException e) { + throw new RuntimeException("Invalid " + RUN_LOCALLY_PROPERTY + + " property from " + getSource(RUN_LOCALLY_PROPERTY) + + ": " + runLocallyValue + ". Expected one of " + + Arrays.toString(Browser.values())); + } + } } diff --git a/uitest/src/com/vaadin/tests/tb3/TB3Runner.java b/uitest/src/com/vaadin/tests/tb3/TB3Runner.java index 69880008ff..eb881deaa5 100644 --- a/uitest/src/com/vaadin/tests/tb3/TB3Runner.java +++ b/uitest/src/com/vaadin/tests/tb3/TB3Runner.java @@ -37,7 +37,7 @@ import org.openqa.selenium.remote.DesiredCapabilities; import com.vaadin.tests.annotations.TestCategory; import com.vaadin.tests.tb3.AbstractTB3Test.BrowserUtil; -import com.vaadin.tests.tb3.AbstractTB3Test.RunLocally; +import com.vaadin.tests.tb3.MultiBrowserTest.Browser; /** * This runner is loosely based on FactoryTestRunner by Ted Young @@ -179,17 +179,17 @@ public class TB3Runner extends BlockJUnit4ClassRunner { /* * Returns a list of desired browser capabilities according to browsers * defined in the test class, filtered by possible filter parameters. Use - * {@code @RunLocally} annotation to override all capabilities. + * {@code @RunLocally} annotation or com.vaadin.testbench.runLocally + * property to override all capabilities. */ private Collection<DesiredCapabilities> getDesiredCapabilities( AbstractTB3Test testClassInstance) { Collection<DesiredCapabilities> desiredCapabilites = getFilteredCapabilities(testClassInstance); - if (isRunLocally(testClassInstance)) { + Browser runLocallyBrowser = testClassInstance.getRunLocallyBrowser(); + if (runLocallyBrowser != null) { desiredCapabilites = new ArrayList<DesiredCapabilities>(); - desiredCapabilites.add(testClassInstance.getClass() - .getAnnotation(RunLocally.class).value() - .getDesiredCapabilities()); + desiredCapabilites.add(runLocallyBrowser.getDesiredCapabilities()); } return desiredCapabilites; @@ -231,10 +231,6 @@ public class TB3Runner extends BlockJUnit4ClassRunner { return filteredCapabilities; } - private boolean isRunLocally(AbstractTB3Test testClassInstance) { - return testClassInstance.getClass().getAnnotation(RunLocally.class) != null; - } - private AbstractTB3Test getTestClassInstance() throws InstantiationException, IllegalAccessException, InvocationTargetException { |