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.

PrivateTB3Configuration.java 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Copyright 2000-2013 Vaadind 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.tb3;
  17. import java.io.File;
  18. import java.io.FileInputStream;
  19. import java.io.IOException;
  20. import java.net.InetAddress;
  21. import java.net.NetworkInterface;
  22. import java.net.SocketException;
  23. import java.util.Calendar;
  24. import java.util.Enumeration;
  25. import java.util.Properties;
  26. import org.junit.Assert;
  27. import org.openqa.selenium.ie.InternetExplorerDriver;
  28. import org.openqa.selenium.remote.DesiredCapabilities;
  29. import com.vaadin.testbench.annotations.BrowserFactory;
  30. import com.vaadin.testbench.annotations.RunLocally;
  31. import com.vaadin.testbench.annotations.RunOnHub;
  32. import com.vaadin.testbench.parallel.Browser;
  33. import com.vaadin.testbench.parallel.BrowserUtil;
  34. /**
  35. * Provides values for parameters which depend on where the test is run.
  36. * Parameters should be configured in work/eclipse-run-selected-test.properties.
  37. * A template is available in uitest/.
  38. *
  39. * @author Vaadin Ltd
  40. */
  41. @RunOnHub("tb3-hub.intra.itmill.com")
  42. @BrowserFactory(VaadinBrowserFactory.class)
  43. public abstract class PrivateTB3Configuration extends ScreenshotTB3Test {
  44. /**
  45. *
  46. */
  47. public static final String SCREENSHOT_DIRECTORY = "com.vaadin.testbench.screenshot.directory";
  48. private static final String HOSTNAME_PROPERTY = "com.vaadin.testbench.deployment.hostname";
  49. private static final String RUN_LOCALLY_PROPERTY = "com.vaadin.testbench.runLocally";
  50. private static final String ALLOW_RUN_LOCALLY_PROPERTY = "com.vaadin.testbench.allowRunLocally";
  51. private static final String PORT_PROPERTY = "com.vaadin.testbench.deployment.port";
  52. private static final String DEPLOYMENT_PROPERTY = "com.vaadin.testbench.deployment.url";
  53. private static final String HUB_URL = "com.vaadin.testbench.hub.url";
  54. private static final Properties properties = new Properties();
  55. private static final File propertiesFile = new File("../work",
  56. "eclipse-run-selected-test.properties");
  57. private static final String FIREFOX_PATH = "firefox.path";
  58. private static final String PHANTOMJS_PATH = "phantomjs.binary.path";
  59. static {
  60. if (propertiesFile.exists()) {
  61. try {
  62. properties.load(new FileInputStream(propertiesFile));
  63. if (properties.containsKey(RUN_LOCALLY_PROPERTY)) {
  64. System.setProperty("useLocalWebDriver", "true");
  65. DesiredCapabilities localBrowser = getRunLocallyCapabilities();
  66. System.setProperty("browsers.include",
  67. localBrowser.getBrowserName()
  68. + localBrowser.getVersion());
  69. }
  70. if (properties.containsKey(FIREFOX_PATH)) {
  71. System.setProperty(FIREFOX_PATH,
  72. properties.getProperty(FIREFOX_PATH));
  73. }
  74. if (properties.containsKey(PHANTOMJS_PATH)) {
  75. System.setProperty(PHANTOMJS_PATH,
  76. properties.getProperty(PHANTOMJS_PATH));
  77. }
  78. } catch (IOException e) {
  79. throw new RuntimeException(e);
  80. }
  81. }
  82. }
  83. @Override
  84. public void setup() throws Exception {
  85. String allowRunLocally = getProperty(ALLOW_RUN_LOCALLY_PROPERTY);
  86. if ((allowRunLocally == null || !allowRunLocally.equals("" + true))
  87. && getClass().getAnnotation(RunLocally.class) != null) {
  88. Assert.fail(
  89. "@RunLocally annotation is not allowed by default in framework tests. "
  90. + "See file uitest/eclipse-run-selected-test.properties for more information.");
  91. }
  92. super.setup();
  93. }
  94. @Override
  95. public void setDesiredCapabilities(
  96. DesiredCapabilities desiredCapabilities) {
  97. super.setDesiredCapabilities(desiredCapabilities);
  98. if (BrowserUtil.isIE(desiredCapabilities)) {
  99. if (requireWindowFocusForIE()) {
  100. desiredCapabilities.setCapability(
  101. InternetExplorerDriver.REQUIRE_WINDOW_FOCUS, true);
  102. }
  103. if (!usePersistentHoverForIE()) {
  104. desiredCapabilities.setCapability(
  105. InternetExplorerDriver.ENABLE_PERSISTENT_HOVERING,
  106. false);
  107. }
  108. if (!useNativeEventsForIE()) {
  109. desiredCapabilities.setCapability(
  110. InternetExplorerDriver.NATIVE_EVENTS, false);
  111. }
  112. }
  113. desiredCapabilities.setCapability("project", "Vaadin Framework");
  114. desiredCapabilities.setCapability("build", String.format("%s / %s",
  115. getDeploymentHostname(), Calendar.getInstance().getTime()));
  116. desiredCapabilities.setCapability("name", String.format("%s.%s",
  117. getClass().getCanonicalName(), testName.getMethodName()));
  118. }
  119. protected static DesiredCapabilities getRunLocallyCapabilities() {
  120. VaadinBrowserFactory factory = new VaadinBrowserFactory();
  121. try {
  122. return factory.create(Browser.valueOf(properties
  123. .getProperty(RUN_LOCALLY_PROPERTY).toUpperCase()));
  124. } catch (Exception e) {
  125. System.err.println(e.getMessage());
  126. System.err.println("Falling back to FireFox");
  127. }
  128. return factory.create(Browser.FIREFOX);
  129. }
  130. protected static String getProperty(String name) {
  131. String property = properties.getProperty(name);
  132. if (property == null) {
  133. property = System.getProperty(name);
  134. }
  135. return property;
  136. }
  137. @Override
  138. protected String getScreenshotDirectory() {
  139. String screenshotDirectory = getProperty(SCREENSHOT_DIRECTORY);
  140. if (screenshotDirectory == null) {
  141. throw new RuntimeException("No screenshot directory defined. Use -D"
  142. + SCREENSHOT_DIRECTORY + "=<path>");
  143. }
  144. return screenshotDirectory;
  145. }
  146. @Override
  147. protected String getHubURL() {
  148. String hubUrl = getProperty(HUB_URL);
  149. if (hubUrl == null || hubUrl.trim().isEmpty()) {
  150. return super.getHubURL();
  151. }
  152. return hubUrl;
  153. }
  154. @Override
  155. protected String getBaseURL() {
  156. if (isRunLocally()) {
  157. return "http://localhost:8888";
  158. }
  159. String url = getProperty(DEPLOYMENT_PROPERTY);
  160. if (url == null || url.trim().isEmpty()) {
  161. return super.getBaseURL();
  162. }
  163. return url;
  164. }
  165. @Override
  166. protected String getDeploymentHostname() {
  167. if (isRunLocally()) {
  168. return "localhost";
  169. }
  170. return getConfiguredDeploymentHostname();
  171. }
  172. protected boolean isRunLocally() {
  173. if (properties.containsKey(RUN_LOCALLY_PROPERTY)) {
  174. return true;
  175. }
  176. if (properties.containsKey(ALLOW_RUN_LOCALLY_PROPERTY)
  177. && properties.get(ALLOW_RUN_LOCALLY_PROPERTY).equals("true")
  178. && getClass().getAnnotation(RunLocally.class) != null) {
  179. return true;
  180. }
  181. return false;
  182. }
  183. /**
  184. * Gets the hostname that tests are configured to use.
  185. *
  186. * @return the host name configuration value
  187. */
  188. public static String getConfiguredDeploymentHostname() {
  189. String hostName = getProperty(HOSTNAME_PROPERTY);
  190. if (hostName == null || "".equals(hostName)) {
  191. hostName = findAutoHostname();
  192. }
  193. return hostName;
  194. }
  195. @Override
  196. protected int getDeploymentPort() {
  197. return getConfiguredDeploymentPort();
  198. }
  199. /**
  200. * Gets the port that tests are configured to use.
  201. *
  202. * @return the port configuration value
  203. */
  204. public static int getConfiguredDeploymentPort() {
  205. String portString = getProperty(PORT_PROPERTY);
  206. int port = 8888;
  207. if (portString != null && !"".equals(portString)) {
  208. port = Integer.parseInt(portString);
  209. }
  210. return port;
  211. }
  212. /**
  213. * Tries to automatically determine the IP address of the machine the test
  214. * is running on.
  215. *
  216. * @return An IP address of one of the network interfaces in the machine.
  217. * @throws RuntimeException
  218. * if there was an error or no IP was found
  219. */
  220. private static String findAutoHostname() {
  221. try {
  222. Enumeration<NetworkInterface> interfaces = NetworkInterface
  223. .getNetworkInterfaces();
  224. while (interfaces.hasMoreElements()) {
  225. NetworkInterface nwInterface = interfaces.nextElement();
  226. if (!nwInterface.isUp() || nwInterface.isLoopback()
  227. || nwInterface.isVirtual()) {
  228. continue;
  229. }
  230. Enumeration<InetAddress> addresses = nwInterface
  231. .getInetAddresses();
  232. while (addresses.hasMoreElements()) {
  233. InetAddress address = addresses.nextElement();
  234. if (address.isLoopbackAddress()) {
  235. continue;
  236. }
  237. if (address.isSiteLocalAddress()) {
  238. return address.getHostAddress();
  239. }
  240. }
  241. }
  242. } catch (SocketException e) {
  243. throw new RuntimeException("Could not enumerate ");
  244. }
  245. throw new RuntimeException(
  246. "No compatible (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16) ip address found.");
  247. }
  248. }