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

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