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

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