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.

AbstractTB3Test.java 40KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  1. /*
  2. * Copyright 2000-2016 Vaadin 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.IOException;
  18. import java.io.InputStream;
  19. import java.io.StringWriter;
  20. import java.lang.reflect.Field;
  21. import java.net.URL;
  22. import java.util.ArrayList;
  23. import java.util.Arrays;
  24. import java.util.Collections;
  25. import java.util.HashSet;
  26. import java.util.List;
  27. import java.util.Set;
  28. import java.util.logging.Level;
  29. import org.apache.commons.io.IOUtils;
  30. import org.apache.commons.lang3.StringUtils;
  31. import org.apache.http.HttpHost;
  32. import org.apache.http.HttpResponse;
  33. import org.apache.http.impl.client.DefaultHttpClient;
  34. import org.apache.http.message.BasicHttpEntityEnclosingRequest;
  35. import org.junit.Assert;
  36. import org.junit.Rule;
  37. import org.junit.rules.TestName;
  38. import org.junit.runner.RunWith;
  39. import org.openqa.selenium.By;
  40. import org.openqa.selenium.Dimension;
  41. import org.openqa.selenium.JavascriptExecutor;
  42. import org.openqa.selenium.NoSuchElementException;
  43. import org.openqa.selenium.WebDriver;
  44. import org.openqa.selenium.WebElement;
  45. import org.openqa.selenium.interactions.Actions;
  46. import org.openqa.selenium.interactions.HasInputDevices;
  47. import org.openqa.selenium.interactions.Keyboard;
  48. import org.openqa.selenium.interactions.Mouse;
  49. import org.openqa.selenium.interactions.internal.Coordinates;
  50. import org.openqa.selenium.internal.Locatable;
  51. import org.openqa.selenium.internal.WrapsElement;
  52. import org.openqa.selenium.remote.DesiredCapabilities;
  53. import org.openqa.selenium.remote.HttpCommandExecutor;
  54. import org.openqa.selenium.remote.RemoteWebDriver;
  55. import org.openqa.selenium.support.ui.ExpectedCondition;
  56. import org.openqa.selenium.support.ui.ExpectedConditions;
  57. import org.openqa.selenium.support.ui.WebDriverWait;
  58. import com.vaadin.server.LegacyApplication;
  59. import com.vaadin.server.UIProvider;
  60. import com.vaadin.testbench.TestBenchDriverProxy;
  61. import com.vaadin.testbench.TestBenchElement;
  62. import com.vaadin.testbench.annotations.BrowserConfiguration;
  63. import com.vaadin.testbench.elements.CheckBoxElement;
  64. import com.vaadin.testbench.elements.LabelElement;
  65. import com.vaadin.testbench.elements.TableElement;
  66. import com.vaadin.testbench.elements.VerticalLayoutElement;
  67. import com.vaadin.testbench.parallel.Browser;
  68. import com.vaadin.testbench.parallel.BrowserUtil;
  69. import com.vaadin.testbench.parallel.ParallelTest;
  70. import com.vaadin.ui.UI;
  71. import elemental.json.JsonObject;
  72. import elemental.json.impl.JsonUtil;
  73. /**
  74. * Base class for TestBench 3+ tests. All TB3+ tests in the project should
  75. * extend this class.
  76. *
  77. * Provides:
  78. * <ul>
  79. * <li>Helpers for browser selection</li>
  80. * <li>Hub connection setup and teardown</li>
  81. * <li>Automatic generation of URL for a given test on the development server
  82. * using {@link #getUIClass()} or by automatically finding an enclosing UI class
  83. * and based on requested features, e.g. {@link #isDebug()},
  84. * {@link #isPush()}</li>
  85. * <li>Generic helpers for creating TB3+ tests</li>
  86. * </ul>
  87. *
  88. * @author Vaadin Ltd
  89. */
  90. @RunWith(TB3Runner.class)
  91. public abstract class AbstractTB3Test extends ParallelTest {
  92. @Rule
  93. public TestName testName = new TestName();
  94. @Rule
  95. public RetryOnFail retry = new RetryOnFail();
  96. /**
  97. * Height of the screenshots we want to capture
  98. */
  99. private static final int SCREENSHOT_HEIGHT = 850;
  100. /**
  101. * Width of the screenshots we want to capture
  102. */
  103. private static final int SCREENSHOT_WIDTH = 1500;
  104. /**
  105. * Timeout used by the TB grid
  106. */
  107. private static final int BROWSER_TIMEOUT_IN_MS = 30 * 1000;
  108. protected static DesiredCapabilities PHANTOMJS2() {
  109. DesiredCapabilities phantomjs2 = new VaadinBrowserFactory()
  110. .create(Browser.PHANTOMJS, "2");
  111. // Hack for the test cluster
  112. phantomjs2.setCapability("phantomjs.binary.path",
  113. "/usr/bin/phantomjs2");
  114. return phantomjs2;
  115. }
  116. private boolean debug = false;
  117. private boolean push = false;
  118. static {
  119. com.vaadin.testbench.Parameters
  120. .setScreenshotComparisonCursorDetection(true);
  121. }
  122. /**
  123. * Connect to the hub using a remote web driver, set the canvas size and
  124. * opens the initial URL as specified by {@link #getTestUrl()}
  125. *
  126. * @throws Exception
  127. */
  128. @Override
  129. public void setup() throws Exception {
  130. super.setup();
  131. int w = SCREENSHOT_WIDTH;
  132. int h = SCREENSHOT_HEIGHT;
  133. try {
  134. testBench().resizeViewPortTo(w, h);
  135. } catch (UnsupportedOperationException e) {
  136. // Opera does not support this...
  137. }
  138. }
  139. /**
  140. * Method for closing the tested application.
  141. */
  142. protected void closeApplication() {
  143. if (driver != null) {
  144. try {
  145. openTestURL("closeApplication");
  146. } catch (Exception e) {
  147. e.printStackTrace();
  148. }
  149. }
  150. }
  151. protected WebElement getTooltipErrorElement() {
  152. WebElement tooltip = getDriver()
  153. .findElement(com.vaadin.testbench.By.className("v-tooltip"));
  154. return tooltip.findElement(By.className("v-errormessage"));
  155. }
  156. protected WebElement getTooltipElement() {
  157. return getDriver().findElement(
  158. com.vaadin.testbench.By.className("v-tooltip-text"));
  159. }
  160. protected Coordinates getCoordinates(TestBenchElement element) {
  161. return ((Locatable) element.getWrappedElement()).getCoordinates();
  162. }
  163. private boolean hasDebugMessage(String message) {
  164. return getDebugMessage(message) != null;
  165. }
  166. private WebElement getDebugMessage(String message) {
  167. return driver.findElement(By.xpath(String.format(
  168. "//span[@class='v-debugwindow-message' and text()='%s']",
  169. message)));
  170. }
  171. protected void waitForDebugMessage(final String expectedMessage) {
  172. waitForDebugMessage(expectedMessage, 30);
  173. }
  174. protected void waitForDebugMessage(final String expectedMessage,
  175. int timeout) {
  176. waitUntil(new ExpectedCondition<Boolean>() {
  177. @Override
  178. public Boolean apply(WebDriver input) {
  179. return hasDebugMessage(expectedMessage);
  180. }
  181. }, timeout);
  182. }
  183. protected void clearDebugMessages() {
  184. driver.findElement(By
  185. .xpath("//button[@class='v-debugwindow-button' and @title='Clear log']"))
  186. .click();
  187. }
  188. protected void waitUntilRowIsVisible(final TableElement table,
  189. final int row) {
  190. waitUntil(new ExpectedCondition<Object>() {
  191. @Override
  192. public Object apply(WebDriver input) {
  193. try {
  194. return table.getCell(row, 0) != null;
  195. } catch (NoSuchElementException e) {
  196. return false;
  197. }
  198. }
  199. });
  200. }
  201. protected void scrollTable(TableElement table, int rows, int rowToWait) {
  202. testBenchElement(table.findElement(By.className("v-scrollable")))
  203. .scroll(rows * 30);
  204. waitUntilRowIsVisible(table, rowToWait);
  205. }
  206. /**
  207. * Opens the given test (defined by {@link #getTestUrl()}, optionally with
  208. * debug window and/or push (depending on {@link #isDebug()} and
  209. * {@link #isPush()}.
  210. */
  211. protected void openTestURL() {
  212. openTestURL(new String[0]);
  213. }
  214. /**
  215. * Opens the given test (defined by {@link #getTestUrl()}, optionally with
  216. * debug window and/or push (depending on {@link #isDebug()} and
  217. * {@link #isPush()}.
  218. */
  219. protected void openTestURL(String... parameters) {
  220. openTestURL(getUIClass(), parameters);
  221. }
  222. /**
  223. * Opens the given test (defined by {@link #getTestUrl()}, optionally with
  224. * debug window and/or push (depending on {@link #isDebug()} and
  225. * {@link #isPush()}.
  226. */
  227. protected void openTestURL(Class<?> uiClass, String... parameters) {
  228. openTestURL(uiClass, new HashSet<>(Arrays.asList(parameters)));
  229. }
  230. private void openTestURL(Class<?> uiClass, Set<String> parameters) {
  231. String url = getTestURL(uiClass);
  232. if (isDebug()) {
  233. parameters.add("debug");
  234. }
  235. if (LegacyApplication.class.isAssignableFrom(uiClass)) {
  236. parameters.add("restartApplication");
  237. }
  238. if (parameters.size() > 0) {
  239. url += "?" + StringUtils.join(parameters, "&");
  240. }
  241. driver.get(url);
  242. }
  243. /**
  244. * Returns the full URL to be used for the test
  245. *
  246. * @return the full URL for the test
  247. */
  248. protected String getTestUrl() {
  249. return StringUtils.strip(getBaseURL(), "/") + getDeploymentPath();
  250. }
  251. /**
  252. * Returns the full URL to be used for the test for the provided UI class.
  253. *
  254. * @return the full URL for the test
  255. */
  256. protected String getTestURL(Class<?> uiClass) {
  257. return StringUtils.strip(getBaseURL(), "/")
  258. + getDeploymentPath(uiClass);
  259. }
  260. /**
  261. * Used to determine what URL to initially open for the test
  262. *
  263. * @return the host name of development server
  264. */
  265. protected abstract String getDeploymentHostname();
  266. /**
  267. * Used to determine what port the test is running on
  268. *
  269. * @return The port teh test is running on, by default 8888
  270. */
  271. protected abstract int getDeploymentPort();
  272. /**
  273. * Produces a collection of browsers to run the test on. This method is
  274. * executed by the test runner when determining how many test methods to
  275. * invoke and with what parameters. For each returned value a test method is
  276. * ran and before running that,
  277. * {@link #setDesiredCapabilities(DesiredCapabilities)} is invoked with the
  278. * value returned by this method.
  279. *
  280. * This method is not static to allow overriding it in sub classes. By
  281. * default runs the test only on Firefox
  282. *
  283. * @return The browsers to run the test on
  284. */
  285. @BrowserConfiguration
  286. public List<DesiredCapabilities> getBrowsersToTest() {
  287. return Collections
  288. .singletonList(Browser.FIREFOX.getDesiredCapabilities());
  289. }
  290. /**
  291. * Finds an element based on the part of a TB2 style locator following the
  292. * :: (e.g. vaadin=runLabelModes::PID_Scheckboxaction-Enabled/domChild[0] ->
  293. * PID_Scheckboxaction-Enabled/domChild[0]).
  294. *
  295. * @param vaadinLocator
  296. * The part following :: of the vaadin locator string
  297. * @return
  298. */
  299. protected WebElement vaadinElement(String vaadinLocator) {
  300. return driver.findElement(vaadinLocator(vaadinLocator));
  301. }
  302. /**
  303. * Uses JavaScript to determine the currently focused element.
  304. *
  305. * @return Focused element or null
  306. */
  307. protected WebElement getFocusedElement() {
  308. Object focusedElement = executeScript("return document.activeElement");
  309. if (null != focusedElement) {
  310. return (WebElement) focusedElement;
  311. } else {
  312. return null;
  313. }
  314. }
  315. /**
  316. * Executes the given Javascript
  317. *
  318. * @param script
  319. * the script to execute
  320. * @return whatever
  321. * {@link org.openqa.selenium.JavascriptExecutor#executeScript(String, Object...)}
  322. * returns
  323. */
  324. protected Object executeScript(String script, Object... args) {
  325. return ((JavascriptExecutor) getDriver()).executeScript(script, args);
  326. }
  327. /**
  328. * Find a Vaadin element based on its id given using Component.setId
  329. *
  330. * @param id
  331. * The id to locate
  332. * @return
  333. */
  334. public WebElement vaadinElementById(String id) {
  335. return driver.findElement(By.id(id));
  336. }
  337. /**
  338. * Finds a {@link By} locator based on the part of a TB2 style locator
  339. * following the :: (e.g.
  340. * vaadin=runLabelModes::PID_Scheckboxaction-Enabled/domChild[0] ->
  341. * PID_Scheckboxaction-Enabled/domChild[0]).
  342. *
  343. * @param vaadinLocator
  344. * The part following :: of the vaadin locator string
  345. * @return
  346. */
  347. public org.openqa.selenium.By vaadinLocator(String vaadinLocator) {
  348. String base = getApplicationId(getDeploymentPath());
  349. base += "::";
  350. return com.vaadin.testbench.By.vaadin(base + vaadinLocator);
  351. }
  352. /**
  353. * Constructs a {@link By} locator for the id given using Component.setId
  354. *
  355. * @param id
  356. * The id to locate
  357. * @return a locator for the given id
  358. */
  359. public By vaadinLocatorById(String id) {
  360. return vaadinLocator("PID_S" + id);
  361. }
  362. /**
  363. * Waits up to 10s for the given condition to become true. Use e.g. as
  364. * {@link #waitUntil(ExpectedConditions.textToBePresentInElement(by, text))}
  365. *
  366. * @param condition
  367. * the condition to wait for to become true
  368. */
  369. protected <T> void waitUntil(ExpectedCondition<T> condition) {
  370. waitUntil(condition, 10);
  371. }
  372. /**
  373. * Waits the given number of seconds for the given condition to become true.
  374. * Use e.g. as
  375. * {@link #waitUntil(ExpectedConditions.textToBePresentInElement(by, text))}
  376. *
  377. * @param condition
  378. * the condition to wait for to become true
  379. */
  380. protected <T> void waitUntil(ExpectedCondition<T> condition,
  381. long timeoutInSeconds) {
  382. new WebDriverWait(driver, timeoutInSeconds).until(condition);
  383. }
  384. /**
  385. * Waits up to 10s for the given condition to become false. Use e.g. as
  386. * {@link #waitUntilNot(ExpectedConditions.textToBePresentInElement(by,
  387. * text))}
  388. *
  389. * @param condition
  390. * the condition to wait for to become false
  391. */
  392. protected <T> void waitUntilNot(ExpectedCondition<T> condition) {
  393. waitUntilNot(condition, 10);
  394. }
  395. /**
  396. * Waits the given number of seconds for the given condition to become
  397. * false. Use e.g. as
  398. * {@link #waitUntilNot(ExpectedConditions.textToBePresentInElement(by,
  399. * text))}
  400. *
  401. * @param condition
  402. * the condition to wait for to become false
  403. */
  404. protected <T> void waitUntilNot(ExpectedCondition<T> condition,
  405. long timeoutInSeconds) {
  406. waitUntil(ExpectedConditions.not(condition), timeoutInSeconds);
  407. }
  408. protected void waitForElementPresent(final By by) {
  409. waitUntil(ExpectedConditions.presenceOfElementLocated(by));
  410. }
  411. protected void waitForElementNotPresent(final By by) {
  412. waitUntil(new ExpectedCondition<Boolean>() {
  413. @Override
  414. public Boolean apply(WebDriver input) {
  415. return input.findElements(by).isEmpty();
  416. }
  417. });
  418. }
  419. protected void waitForElementVisible(final By by) {
  420. waitUntil(ExpectedConditions.visibilityOfElementLocated(by));
  421. }
  422. /**
  423. * Checks if the given element has the given class name.
  424. *
  425. * Matches only full class names, i.e. has ("foo") does not match
  426. * class="foobar"
  427. *
  428. * @param element
  429. * @param className
  430. * @return
  431. */
  432. protected boolean hasCssClass(WebElement element, String className) {
  433. String classes = element.getAttribute("class");
  434. if (classes == null || classes.isEmpty()) {
  435. return (className == null || className.isEmpty());
  436. }
  437. for (String cls : classes.split(" ")) {
  438. if (className.equals(cls)) {
  439. return true;
  440. }
  441. }
  442. return false;
  443. }
  444. /**
  445. * For tests extending AbstractTestUIWithLog, returns the element for the
  446. * Nth log row
  447. *
  448. * @param rowNr
  449. * The log row to retrieve
  450. * @return the Nth log row
  451. */
  452. protected WebElement getLogRowElement(int rowNr) {
  453. return vaadinElementById("Log_row_" + rowNr);
  454. }
  455. /**
  456. * For tests extending AbstractTestUIWithLog, returns the text in the Nth
  457. * log row
  458. *
  459. * @param rowNr
  460. * The log row to retrieve text for
  461. * @return the text in the log row
  462. */
  463. protected String getLogRow(int rowNr) {
  464. return getLogRowElement(rowNr).getText();
  465. }
  466. /**
  467. * Asserts that {@literal a} is &gt;= {@literal b}
  468. *
  469. * @param message
  470. * The message to include in the {@link AssertionError}
  471. * @param a
  472. * @param b
  473. * @throws AssertionError
  474. * If comparison fails
  475. */
  476. public static final <T> void assertGreaterOrEqual(String message,
  477. Comparable<T> a, T b) throws AssertionError {
  478. if (a.compareTo(b) >= 0) {
  479. return;
  480. }
  481. throw new AssertionError(decorate(message, a, b));
  482. }
  483. /**
  484. * Asserts that {@literal a} is &gt; {@literal b}
  485. *
  486. * @param message
  487. * The message to include in the {@link AssertionError}
  488. * @param a
  489. * @param b
  490. * @throws AssertionError
  491. * If comparison fails
  492. */
  493. public static final <T> void assertGreater(String message, Comparable<T> a,
  494. T b) throws AssertionError {
  495. if (a.compareTo(b) > 0) {
  496. return;
  497. }
  498. throw new AssertionError(decorate(message, a, b));
  499. }
  500. /**
  501. * Asserts that {@literal a} is &lt;= {@literal b}
  502. *
  503. * @param message
  504. * The message to include in the {@link AssertionError}
  505. * @param a
  506. * @param b
  507. * @throws AssertionError
  508. * If comparison fails
  509. */
  510. public static final <T> void assertLessThanOrEqual(String message,
  511. Comparable<T> a, T b) throws AssertionError {
  512. if (a.compareTo(b) <= 0) {
  513. return;
  514. }
  515. throw new AssertionError(decorate(message, a, b));
  516. }
  517. /**
  518. * Asserts that {@literal a} is &lt; {@literal b}
  519. *
  520. * @param message
  521. * The message to include in the {@link AssertionError}
  522. * @param a
  523. * @param b
  524. * @throws AssertionError
  525. * If comparison fails
  526. */
  527. public static final <T> void assertLessThan(String message, Comparable<T> a,
  528. T b) throws AssertionError {
  529. if (a.compareTo(b) < 0) {
  530. return;
  531. }
  532. throw new AssertionError(decorate(message, a, b));
  533. }
  534. private static <T> String decorate(String message, Comparable<T> a, T b) {
  535. message = message.replace("{0}", a.toString());
  536. message = message.replace("{1}", b.toString());
  537. return message;
  538. }
  539. /**
  540. * Returns the path that should be used for the test. The path contains the
  541. * full path (appended to hostname+port) and must start with a slash.
  542. *
  543. * @param push
  544. * true if "?debug" should be added
  545. * @param debug
  546. * true if /run-push should be used instead of /run
  547. *
  548. * @return The URL path to the UI class to test
  549. */
  550. protected String getDeploymentPath() {
  551. Class<?> uiClass = getUIClass();
  552. if (uiClass != null) {
  553. return getDeploymentPath(uiClass);
  554. }
  555. throw new IllegalArgumentException("Unable to determine path for "
  556. + getClass().getCanonicalName());
  557. }
  558. /**
  559. * Returns the UI class the current test is connected to (or in special
  560. * cases UIProvider or LegacyApplication). Uses the enclosing class if the
  561. * test class is a static inner class to a UI class.
  562. *
  563. * Test which are not enclosed by a UI class must implement this method and
  564. * return the UI class they want to test.
  565. *
  566. * Note that this method will update the test name to the enclosing class to
  567. * be compatible with TB2 screenshot naming
  568. *
  569. * @return the UI class the current test is connected to
  570. */
  571. protected Class<?> getUIClass() {
  572. try {
  573. // Convention: SomeUITest uses the SomeUI UI class
  574. String uiClassName = getClass().getName().replaceFirst("Test$", "");
  575. Class<?> cls = Class.forName(uiClassName);
  576. if (isSupportedRunnerClass(cls)) {
  577. return cls;
  578. }
  579. } catch (Exception e) {
  580. }
  581. throw new RuntimeException(
  582. "Could not determine UI class. Ensure the test is named UIClassTest and is in the same package as the UIClass");
  583. }
  584. /**
  585. * @return true if the given class is supported by ApplicationServletRunner
  586. */
  587. @SuppressWarnings("deprecation")
  588. private boolean isSupportedRunnerClass(Class<?> cls) {
  589. if (UI.class.isAssignableFrom(cls)) {
  590. return true;
  591. }
  592. if (UIProvider.class.isAssignableFrom(cls)) {
  593. return true;
  594. }
  595. if (LegacyApplication.class.isAssignableFrom(cls)) {
  596. return true;
  597. }
  598. return false;
  599. }
  600. /**
  601. * Returns whether to run the test in debug mode (with the debug console
  602. * open) or not
  603. *
  604. * @return true to run with the debug window open, false by default
  605. */
  606. protected final boolean isDebug() {
  607. return debug;
  608. }
  609. /**
  610. * Sets whether to run the test in debug mode (with the debug console open)
  611. * or not.
  612. *
  613. * @param debug
  614. * true to open debug window, false otherwise
  615. */
  616. protected final void setDebug(boolean debug) {
  617. this.debug = debug;
  618. }
  619. /**
  620. * Returns whether to run the test with push enabled (using /run-push) or
  621. * not. Note that push tests can and should typically be created using @Push
  622. * on the UI instead of overriding this method
  623. *
  624. * @return true if /run-push is used, false otherwise
  625. */
  626. protected final boolean isPush() {
  627. return push;
  628. }
  629. /**
  630. * Sets whether to run the test with push enabled (using /run-push) or not.
  631. * Note that push tests can and should typically be created using @Push on
  632. * the UI instead of overriding this method
  633. *
  634. * @param push
  635. * true to use /run-push in the test, false otherwise
  636. */
  637. protected final void setPush(boolean push) {
  638. this.push = push;
  639. }
  640. /**
  641. * Returns the path for the given UI class when deployed on the test server.
  642. * The path contains the full path (appended to hostname+port) and must
  643. * start with a slash.
  644. *
  645. * This method takes into account {@link #isPush()} and {@link #isDebug()}
  646. * when the path is generated.
  647. *
  648. * @param uiClass
  649. * @param push
  650. * true if "?debug" should be added
  651. * @param debug
  652. * true if /run-push should be used instead of /run
  653. * @return The path to the given UI class
  654. */
  655. protected String getDeploymentPath(Class<?> uiClass) {
  656. String runPath = "/run";
  657. if (isPush()) {
  658. runPath = "/run-push";
  659. }
  660. if (UI.class.isAssignableFrom(uiClass)
  661. || UIProvider.class.isAssignableFrom(uiClass)
  662. || LegacyApplication.class.isAssignableFrom(uiClass)) {
  663. return runPath + "/" + uiClass.getCanonicalName();
  664. } else {
  665. throw new IllegalArgumentException(
  666. "Unable to determine path for enclosing class "
  667. + uiClass.getCanonicalName());
  668. }
  669. }
  670. /**
  671. * Used to determine what URL to initially open for the test
  672. *
  673. * @return The base URL for the test. Does not include a trailing slash.
  674. */
  675. protected String getBaseURL() {
  676. return "http://" + getDeploymentHostname() + ":" + getDeploymentPort();
  677. }
  678. /**
  679. * Generates the application id based on the URL in a way compatible with
  680. * VaadinServletService.
  681. *
  682. * @param pathWithQueryParameters
  683. * The path part of the URL, possibly still containing query
  684. * parameters
  685. * @return The application ID string used in Vaadin locators
  686. */
  687. private String getApplicationId(String pathWithQueryParameters) {
  688. // Remove any possible URL parameters
  689. String pathWithoutQueryParameters = pathWithQueryParameters
  690. .replaceAll("\\?.*", "");
  691. if ("".equals(pathWithoutQueryParameters)) {
  692. return "ROOT";
  693. }
  694. // Retain only a-z and numbers
  695. return pathWithoutQueryParameters.replaceAll("[^a-zA-Z0-9]", "");
  696. }
  697. /**
  698. * Sleeps for the given number of ms but ensures that the browser connection
  699. * does not time out.
  700. *
  701. * @param timeoutMillis
  702. * Number of ms to wait
  703. */
  704. protected void sleep(int timeoutMillis) {
  705. while (timeoutMillis > 0) {
  706. int d = Math.min(BROWSER_TIMEOUT_IN_MS, timeoutMillis);
  707. try {
  708. Thread.sleep(d);
  709. } catch (InterruptedException e) {
  710. throw new RuntimeException(e);
  711. }
  712. timeoutMillis -= d;
  713. // Do something to keep the connection alive
  714. getDriver().getTitle();
  715. }
  716. }
  717. /**
  718. * Called by the test runner whenever there is an exception in the test that
  719. * will cause termination of the test
  720. *
  721. * @param t
  722. * the throwable which caused the termination
  723. */
  724. public void onUncaughtException(Throwable t) {
  725. // Do nothing by default
  726. }
  727. /**
  728. * Returns the mouse object for doing mouse commands
  729. *
  730. * @return Returns the mouse
  731. */
  732. public Mouse getMouse() {
  733. return ((HasInputDevices) getDriver()).getMouse();
  734. }
  735. /**
  736. * Returns the keyboard object for controlling keyboard events
  737. *
  738. * @return Return the keyboard
  739. */
  740. public Keyboard getKeyboard() {
  741. return ((HasInputDevices) getDriver()).getKeyboard();
  742. }
  743. public void hitButton(String id) {
  744. driver.findElement(By.id(id)).click();
  745. }
  746. protected void openDebugLogTab() {
  747. waitUntil(new ExpectedCondition<Boolean>() {
  748. @Override
  749. public Boolean apply(WebDriver input) {
  750. WebElement element = getDebugLogButton();
  751. return element != null;
  752. }
  753. }, 15);
  754. getDebugLogButton().click();
  755. }
  756. private WebElement getDebugLogButton() {
  757. return findElement(By.xpath("//button[@title='Debug message log']"));
  758. }
  759. protected void assertNoDebugMessage(Level level) {
  760. // class="v-debugwindow-row Level.getName()"
  761. List<WebElement> logElements = driver.findElements(By.xpath(String
  762. .format("//div[@class='v-debugwindow-row %s']/span[@class='v-debugwindow-message']",
  763. level.getName())));
  764. if (!logElements.isEmpty()) {
  765. String logRows = "";
  766. for (WebElement e : logElements) {
  767. logRows += "\n" + e.getText();
  768. }
  769. Assert.fail("Found debug messages with level " + level.getName()
  770. + ": " + logRows);
  771. }
  772. }
  773. /**
  774. * Should the "require window focus" be enabled for Internet Explorer.
  775. * RequireWindowFocus makes tests more stable but seems to be broken with
  776. * certain commands such as sendKeys. Therefore it is not enabled by default
  777. * for all tests
  778. *
  779. * @return true, to use the "require window focus" feature, false otherwise
  780. */
  781. protected boolean requireWindowFocusForIE() {
  782. return false;
  783. }
  784. /**
  785. * Should the "enable persistent hover" be enabled for Internet Explorer.
  786. *
  787. * Persistent hovering causes continuous firing of mouse over events at the
  788. * last location the mouse cursor has been moved to. This is to avoid
  789. * problems where the real mouse cursor is inside the browser window and
  790. * Internet Explorer uses that location for some undefined operation
  791. * (http://
  792. * jimevansmusic.blogspot.fi/2012/06/whats-wrong-with-internet-explorer
  793. * .html)
  794. *
  795. * @return true, to use the "persistent hover" feature, false otherwise
  796. */
  797. protected boolean usePersistentHoverForIE() {
  798. return true;
  799. }
  800. /**
  801. * Should the "native events" be enabled for Internet Explorer.
  802. * <p>
  803. * Native events sometimes cause failure in clicking on buttons/checkboxes
  804. * but are possibly needed for some operations.
  805. *
  806. * @return true, to use "native events", false to use generated Javascript
  807. * events
  808. */
  809. protected boolean useNativeEventsForIE() {
  810. return true;
  811. }
  812. // FIXME: Remove this once TB4 getRemoteControlName works properly
  813. private RemoteWebDriver getRemoteDriver() {
  814. WebDriver d = getDriver();
  815. if (d instanceof TestBenchDriverProxy) {
  816. try {
  817. Field f = TestBenchDriverProxy.class
  818. .getDeclaredField("actualDriver");
  819. f.setAccessible(true);
  820. return (RemoteWebDriver) f.get(d);
  821. } catch (Exception e) {
  822. e.printStackTrace();
  823. }
  824. }
  825. if (d instanceof RemoteWebDriver) {
  826. return (RemoteWebDriver) d;
  827. }
  828. return null;
  829. }
  830. // FIXME: Remove this once TB4 getRemoteControlName works properly
  831. protected String getRemoteControlName() {
  832. try {
  833. RemoteWebDriver d = getRemoteDriver();
  834. if (d == null) {
  835. return null;
  836. }
  837. HttpCommandExecutor ce = (HttpCommandExecutor) d
  838. .getCommandExecutor();
  839. String hostName = ce.getAddressOfRemoteServer().getHost();
  840. int port = ce.getAddressOfRemoteServer().getPort();
  841. HttpHost host = new HttpHost(hostName, port);
  842. DefaultHttpClient client = new DefaultHttpClient();
  843. URL sessionURL = new URL("http://" + hostName + ":" + port
  844. + "/grid/api/testsession?session=" + d.getSessionId());
  845. BasicHttpEntityEnclosingRequest r = new BasicHttpEntityEnclosingRequest(
  846. "POST", sessionURL.toExternalForm());
  847. HttpResponse response = client.execute(host, r);
  848. JsonObject object = extractObject(response);
  849. URL myURL = new URL(object.getString("proxyId"));
  850. if ((myURL.getHost() != null) && (myURL.getPort() != -1)) {
  851. return myURL.getHost();
  852. }
  853. } catch (Exception e) {
  854. e.printStackTrace();
  855. }
  856. return null;
  857. }
  858. protected boolean logContainsText(String string) {
  859. List<String> logs = getLogs();
  860. for (String text : logs) {
  861. if (text.contains(string)) {
  862. return true;
  863. }
  864. }
  865. return false;
  866. }
  867. protected List<String> getLogs() {
  868. VerticalLayoutElement log = $(VerticalLayoutElement.class).id("Log");
  869. List<LabelElement> logLabels = log.$(LabelElement.class).all();
  870. List<String> logTexts = new ArrayList<>();
  871. for (LabelElement label : logLabels) {
  872. logTexts.add(label.getText());
  873. }
  874. return logTexts;
  875. }
  876. private static JsonObject extractObject(HttpResponse resp)
  877. throws IOException {
  878. InputStream contents = resp.getEntity().getContent();
  879. StringWriter writer = new StringWriter();
  880. IOUtils.copy(contents, writer, "UTF8");
  881. return JsonUtil.parse(writer.toString());
  882. }
  883. protected void click(CheckBoxElement checkbox) {
  884. WebElement cb = checkbox.findElement(By.xpath("input"));
  885. if (BrowserUtil.isChrome(getDesiredCapabilities())) {
  886. testBenchElement(cb).click(0, 0);
  887. } else if (BrowserUtil.isFirefox(getDesiredCapabilities())) {
  888. // Firefox workaround
  889. getCommandExecutor().executeScript("arguments[0].click()", cb);
  890. } else {
  891. cb.click();
  892. }
  893. }
  894. protected void clickElement(WebElement element) {
  895. if (BrowserUtil.isFirefox(getDesiredCapabilities())) {
  896. // Workaround for Selenium/TB and Firefox 45 issue
  897. ((TestBenchElement) (element)).clickHiddenElement();
  898. } else {
  899. element.click();
  900. }
  901. }
  902. protected boolean isLoadingIndicatorVisible() {
  903. WebElement loadingIndicator = findElement(
  904. By.className("v-loading-indicator"));
  905. return loadingIndicator.isDisplayed();
  906. }
  907. protected void waitUntilLoadingIndicatorVisible() {
  908. waitUntil(input -> isLoadingIndicatorVisible());
  909. }
  910. protected void waitUntilLoadingIndicatorNotVisible() {
  911. waitUntil(input -> !isLoadingIndicatorVisible());
  912. }
  913. /**
  914. * Selects a menu item. By default, this will click on the menu item.
  915. *
  916. * @param menuCaption
  917. * caption of the menu item
  918. */
  919. protected void selectMenu(String menuCaption) {
  920. selectMenu(menuCaption, true);
  921. }
  922. /**
  923. * Selects a menu item.
  924. *
  925. * @param menuCaption
  926. * caption of the menu item
  927. * @param click
  928. * <code>true</code> if should click the menu item;
  929. * <code>false</code> if not
  930. */
  931. protected void selectMenu(String menuCaption, boolean click) {
  932. WebElement menuElement = getMenuElement(menuCaption);
  933. Dimension size = menuElement.getSize();
  934. new Actions(getDriver())
  935. .moveToElement(menuElement, size.width - 10, size.height / 2)
  936. .perform();
  937. if (click) {
  938. new Actions(getDriver()).click().perform();
  939. }
  940. }
  941. /**
  942. * Finds the menu item from the DOM based on menu item caption.
  943. *
  944. * @param menuCaption
  945. * caption of the menu item
  946. * @return the found menu item
  947. * @throws NoSuchElementException
  948. * if menu item is not found
  949. */
  950. protected WebElement getMenuElement(String menuCaption)
  951. throws NoSuchElementException {
  952. return getDriver().findElement(
  953. By.xpath("//span[text() = '" + menuCaption + "']"));
  954. }
  955. /**
  956. * Selects a submenu described by a path of menus from the first MenuBar in
  957. * the UI.
  958. *
  959. * @param menuCaptions
  960. * array of menu captions
  961. */
  962. protected void selectMenuPath(String... menuCaptions) {
  963. selectMenu(menuCaptions[0], true);
  964. // Move to the menu item opened below the menu bar.
  965. new Actions(getDriver())
  966. .moveByOffset(0,
  967. getMenuElement(menuCaptions[0]).getSize().getHeight())
  968. .perform();
  969. for (int i = 1; i < menuCaptions.length - 1; i++) {
  970. selectMenu(menuCaptions[i]);
  971. new Actions(getDriver()).moveByOffset(40, 0).build().perform();
  972. }
  973. selectMenu(menuCaptions[menuCaptions.length - 1], true);
  974. }
  975. /**
  976. * Asserts that an element is present
  977. *
  978. * @param by
  979. * the locatore for the element
  980. */
  981. protected void assertElementPresent(By by) {
  982. Assert.assertTrue("Element is not present", isElementPresent(by));
  983. }
  984. /**
  985. * Asserts that an element is not present
  986. *
  987. * @param by
  988. * the locatore for the element
  989. */
  990. protected void assertElementNotPresent(By by) {
  991. Assert.assertFalse("Element is present", isElementPresent(by));
  992. }
  993. /**
  994. * Asserts that no error notifications are shown. Requires the use of
  995. * "?debug" as exceptions are otherwise not shown as notifications.
  996. */
  997. protected void assertNoErrorNotifications() {
  998. Assert.assertFalse(
  999. "Error notification with client side exception is shown",
  1000. isNotificationPresent("error"));
  1001. }
  1002. /**
  1003. * Asserts that no system notifications are shown.
  1004. */
  1005. protected void assertNoSystemNotifications() {
  1006. Assert.assertFalse(
  1007. "Error notification with system error exception is shown",
  1008. isNotificationPresent("system"));
  1009. }
  1010. /**
  1011. * Asserts that a system notification is shown.
  1012. */
  1013. protected void assertSystemNotification() {
  1014. Assert.assertTrue(
  1015. "Error notification with system error exception is not shown",
  1016. isNotificationPresent("system"));
  1017. }
  1018. private boolean isNotificationPresent(String type) {
  1019. if ("error".equals(type)) {
  1020. Assert.assertTrue(
  1021. "Debug window must be open to be able to see error notifications",
  1022. isDebugWindowOpen());
  1023. }
  1024. return isElementPresent(By.className("v-Notification-" + type));
  1025. }
  1026. private boolean isDebugWindowOpen() {
  1027. return isElementPresent(By.className("v-debugwindow"));
  1028. }
  1029. protected void assertNoHorizontalScrollbar(WebElement element,
  1030. String errorMessage) {
  1031. // IE rounds clientWidth/clientHeight down and scrollHeight/scrollWidth
  1032. // up, so using clientWidth/clientHeight will fail if the element height
  1033. // is not an integer
  1034. int clientWidth = getClientWidth(element);
  1035. int scrollWidth = getScrollWidth(element);
  1036. boolean hasScrollbar = scrollWidth > clientWidth;
  1037. Assert.assertFalse(
  1038. "The element should not have a horizontal scrollbar (scrollWidth: "
  1039. + scrollWidth + ", clientWidth: " + clientWidth + "): "
  1040. + errorMessage,
  1041. hasScrollbar);
  1042. }
  1043. protected void assertNoVerticalScrollbar(WebElement element,
  1044. String errorMessage) {
  1045. // IE rounds clientWidth/clientHeight down and scrollHeight/scrollWidth
  1046. // up, so using clientWidth/clientHeight will fail if the element height
  1047. // is not an integer
  1048. int clientHeight = getClientHeight(element);
  1049. int scrollHeight = getScrollHeight(element);
  1050. boolean hasScrollbar = scrollHeight > clientHeight;
  1051. Assert.assertFalse(
  1052. "The element should not have a vertical scrollbar (scrollHeight: "
  1053. + scrollHeight + ", clientHeight: " + clientHeight
  1054. + "): " + errorMessage,
  1055. hasScrollbar);
  1056. }
  1057. protected int getScrollHeight(WebElement element) {
  1058. return ((Number) executeScript("return arguments[0].scrollHeight;",
  1059. element)).intValue();
  1060. }
  1061. protected int getScrollWidth(WebElement element) {
  1062. return ((Number) executeScript("return arguments[0].scrollWidth;",
  1063. element)).intValue();
  1064. }
  1065. /**
  1066. * Returns client height rounded up instead of as double because of IE9
  1067. * issues: https://dev.vaadin.com/ticket/18469
  1068. */
  1069. protected int getClientHeight(WebElement e) {
  1070. String script = "var cs = window.getComputedStyle(arguments[0]);"
  1071. + "return Math.ceil(parseFloat(cs.height)+parseFloat(cs.paddingTop)+parseFloat(cs.paddingBottom));";
  1072. return ((Number) executeScript(script, e)).intValue();
  1073. }
  1074. /**
  1075. * Returns client width rounded up instead of as double because of IE9
  1076. * issues: https://dev.vaadin.com/ticket/18469
  1077. */
  1078. protected int getClientWidth(WebElement e) {
  1079. String script = "var cs = window.getComputedStyle(arguments[0]);"
  1080. + "var h = parseFloat(cs.width)+parseFloat(cs.paddingLeft)+parseFloat(cs.paddingRight);"
  1081. + "return Math.ceil(h);";
  1082. return ((Number) executeScript(script, e)).intValue();
  1083. }
  1084. protected void assertElementsEquals(WebElement expectedElement,
  1085. WebElement actualElement) {
  1086. while (expectedElement instanceof WrapsElement) {
  1087. expectedElement = ((WrapsElement) expectedElement)
  1088. .getWrappedElement();
  1089. }
  1090. while (actualElement instanceof WrapsElement) {
  1091. actualElement = ((WrapsElement) actualElement).getWrappedElement();
  1092. }
  1093. Assert.assertEquals(expectedElement, actualElement);
  1094. }
  1095. protected WebElement getActiveElement() {
  1096. return (WebElement) executeScript("return document.activeElement;");
  1097. }
  1098. protected void waitForThemeToChange(final String theme) {
  1099. final WebElement rootDiv = findElement(
  1100. By.xpath("//div[contains(@class,'v-app')]"));
  1101. waitUntil(new ExpectedCondition<Boolean>() {
  1102. @Override
  1103. public Boolean apply(WebDriver input) {
  1104. String rootClass = rootDiv.getAttribute("class").trim();
  1105. return rootClass.contains(theme);
  1106. }
  1107. }, 30);
  1108. }
  1109. }