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.

WindowHeaderButtonKeyboardActionsTest.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. package com.vaadin.tests.components.window;
  2. import java.io.IOException;
  3. import org.junit.Test;
  4. import org.openqa.selenium.JavascriptExecutor;
  5. import org.openqa.selenium.Keys;
  6. import org.openqa.selenium.WebElement;
  7. import org.openqa.selenium.interactions.Actions;
  8. import com.vaadin.testbench.By;
  9. import com.vaadin.testbench.elements.WindowElement;
  10. import com.vaadin.tests.tb3.SingleBrowserTest;
  11. import static org.junit.Assert.assertTrue;
  12. public class WindowHeaderButtonKeyboardActionsTest extends SingleBrowserTest {
  13. private static final String HEADER_CLASS = "v-window-header";
  14. private static final String RESTORE_BOX_CLASS = "v-window-restorebox";
  15. private static final String MAXIMIZE_BOX_CLASS = "v-window-maximizebox";
  16. private static final String CLOSE_BOX_CLASS = "v-window-closebox";
  17. @Override
  18. public void setup() throws Exception {
  19. super.setup();
  20. openTestURL();
  21. // open window before each test case
  22. waitForElementPresent(By.id("firstButton"));
  23. WebElement button = findElement(By.id("firstButton"));
  24. button.click();
  25. waitForElementPresent(By.id("testWindow"));
  26. }
  27. /**
  28. * Scenario: focus the close button of the opened window -> press ENTER key
  29. * -> window should be closed
  30. */
  31. @Test
  32. public void testCloseWindowWithEnter() throws IOException {
  33. assertTrue("Window is not open",
  34. findElements(By.id("testWindow")).size() == 1);
  35. WindowElement windowElement = $(WindowElement.class).first();
  36. WebElement closeButton = windowElement
  37. .findElement(By.className(CLOSE_BOX_CLASS));
  38. setFocusToElementAndWait(closeButton);
  39. assertTrue("Window's close button is not the focused element",
  40. closeButton.equals(driver.switchTo().activeElement()));
  41. pressKeyAndWait(Keys.ENTER);
  42. assertTrue("Window is not closed",
  43. findElements(By.className("v-window")).size() == 0);
  44. }
  45. /**
  46. * Scenario: focus the close button of the opened window -> press SPACE key
  47. * -> window should be closed
  48. */
  49. @Test
  50. public void testCloseWindowWithSpace() throws IOException {
  51. assertTrue("Window is not open",
  52. findElements(By.id("testWindow")).size() == 1);
  53. WindowElement windowElement = $(WindowElement.class).first();
  54. WebElement closeButton = windowElement
  55. .findElement(By.className(CLOSE_BOX_CLASS));
  56. setFocusToElementAndWait(closeButton);
  57. assertTrue("Window's close button is not the focused element",
  58. closeButton.equals(driver.switchTo().activeElement()));
  59. pressKeyAndWait(Keys.SPACE);
  60. assertTrue("Window is not closed",
  61. findElements(By.className("v-window")).size() == 0);
  62. }
  63. /**
  64. * Scenario: focus close button of opened window -> press keys DELETE,
  65. * ARROW_LEFT, and END -> window should remain open after all actions
  66. */
  67. @Test
  68. public void testIncorrectKeyInputDoesntFireClose() throws IOException {
  69. assertTrue("Window is not open",
  70. findElements(By.id("testWindow")).size() == 1);
  71. WindowElement windowElement = $(WindowElement.class).first();
  72. WebElement closeButton = windowElement
  73. .findElement(By.className(CLOSE_BOX_CLASS));
  74. setFocusToElementAndWait(closeButton);
  75. assertTrue("Window's close button is not the focused element",
  76. closeButton.equals(driver.switchTo().activeElement()));
  77. pressKeyAndWait(Keys.DELETE);
  78. assertTrue(
  79. "Window is closed by DELETE when close button is the focused element",
  80. findElements(By.className("v-window")).size() > 0);
  81. pressKeyAndWait(Keys.ARROW_LEFT);
  82. assertTrue(
  83. "Window is closed by ARROW_LEFT when close button is the focused element",
  84. findElements(By.className("v-window")).size() > 0);
  85. pressKeyAndWait(Keys.END);
  86. assertTrue(
  87. "Window is closed by END when close button is the focused element",
  88. findElements(By.className("v-window")).size() > 0);
  89. }
  90. /**
  91. * Scenario: close button of opened window is not focused -> press keys
  92. * ENTER and SPACE -> window should remain open after all actions
  93. */
  94. @Test
  95. public void testNonfocusedKeyDoesntCloseWindow() throws IOException {
  96. assertTrue("Window is not open",
  97. findElements(By.id("testWindow")).size() == 1);
  98. WindowElement windowElement = $(WindowElement.class).first();
  99. WebElement closeButton = windowElement
  100. .findElement(By.className(CLOSE_BOX_CLASS));
  101. assertTrue("Window's close button is the focused element",
  102. !closeButton.equals(driver.switchTo().activeElement()));
  103. pressKeyAndWait(Keys.ENTER);
  104. assertTrue(
  105. "Window is closed by ENTER when close button is not the focused element",
  106. findElements(By.className("v-window")).size() > 0);
  107. pressKeyAndWait(Keys.SPACE);
  108. assertTrue(
  109. "Window is closed by SPACE when close button is not the focused element",
  110. findElements(By.className("v-window")).size() > 0);
  111. }
  112. /**
  113. * Scenario: focus close button of opened window -> press keys TAB, and
  114. * TAB+SHIFT in succession, shifting focus from and back to the button ->
  115. * press ENTER key -> window should be closed
  116. */
  117. @Test
  118. public void testShiftFocusAndCloseWindow() throws IOException {
  119. assertTrue("Window is not open",
  120. findElements(By.id("testWindow")).size() == 1);
  121. WindowElement windowElement = $(WindowElement.class).first();
  122. WebElement closeButton = windowElement
  123. .findElement(By.className(CLOSE_BOX_CLASS));
  124. setFocusToElementAndWait(closeButton);
  125. assertTrue("Window's close button is not the focused element",
  126. closeButton.equals(driver.switchTo().activeElement()));
  127. pressKeyAndWait(Keys.TAB);
  128. assertTrue("Window's close button is the focused element",
  129. !closeButton.equals(driver.switchTo().activeElement()));
  130. pressKeyCombinations(Keys.SHIFT, Keys.TAB);
  131. assertTrue("Window's close button is not the focused element",
  132. closeButton.equals(driver.switchTo().activeElement()));
  133. pressKeyAndWait(Keys.ENTER);
  134. assertTrue("Window is not closed when focus is shifted back-and-forth",
  135. findElements(By.className("v-window")).size() == 0);
  136. }
  137. /**
  138. * Scenario: focus close button of opened window -> click close button with
  139. * the mouse cursor -> window should be closed
  140. */
  141. @Test
  142. public void testMouseClickClosesWindowOnFocus() throws IOException {
  143. assertTrue("Window is not open",
  144. findElements(By.id("testWindow")).size() == 1);
  145. WindowElement windowElement = $(WindowElement.class).first();
  146. WebElement closeButton = windowElement
  147. .findElement(By.className(CLOSE_BOX_CLASS));
  148. setFocusToElementAndWait(closeButton);
  149. assertTrue("Window's close button is not the focused element",
  150. closeButton.equals(driver.switchTo().activeElement()));
  151. // click button with mouse and wait
  152. closeButton.click();
  153. sleep(200);
  154. assertTrue("Window is not closed when focused element is clicked",
  155. findElements(By.className("v-window")).size() == 0);
  156. }
  157. // Tests for maximize-restore button
  158. /**
  159. * Scenario: focus the maximize button of the opened window -> press ENTER
  160. * key -> window should be maximized
  161. */
  162. @Test
  163. public void testMaximizeWindowWithEnter() throws IOException {
  164. assertTrue("Window is not open",
  165. findElements(By.id("testWindow")).size() == 1);
  166. WindowElement windowElement = $(WindowElement.class).first();
  167. WebElement closeButton = windowElement
  168. .findElement(By.className(MAXIMIZE_BOX_CLASS));
  169. setFocusToElementAndWait(closeButton);
  170. assertTrue("Window's maximize button is not the focused element",
  171. closeButton.equals(driver.switchTo().activeElement()));
  172. pressKeyAndWait(Keys.ENTER);
  173. assertTrue("Window is not maximized", windowElement.isMaximized());
  174. }
  175. /**
  176. * Scenario: focus the maximize button of the opened window -> press SPACE
  177. * key -> window should be maximized
  178. */
  179. @Test
  180. public void testMaximizeWindowWithSpace() throws IOException {
  181. assertTrue("Window is not open",
  182. findElements(By.id("testWindow")).size() == 1);
  183. WindowElement windowElement = $(WindowElement.class).first();
  184. WebElement closeButton = windowElement
  185. .findElement(By.className(MAXIMIZE_BOX_CLASS));
  186. setFocusToElementAndWait(closeButton);
  187. assertTrue("Window's maximize button is not the focused element",
  188. closeButton.equals(driver.switchTo().activeElement()));
  189. pressKeyAndWait(Keys.SPACE);
  190. assertTrue("Window is not maximized", windowElement.isMaximized());
  191. }
  192. /**
  193. * Scenario: focus maximize button of opened window -> press keys DELETE,
  194. * ARROW_UP, and ADD -> window should remain open after all actions
  195. */
  196. @Test
  197. public void testIncorrectKeyInputDoesntFireMaximize() throws IOException {
  198. assertTrue("Window is not open",
  199. findElements(By.id("testWindow")).size() == 1);
  200. WindowElement windowElement = $(WindowElement.class).first();
  201. WebElement maximizeButton = windowElement
  202. .findElement(By.className(MAXIMIZE_BOX_CLASS));
  203. setFocusToElementAndWait(maximizeButton);
  204. assertTrue("Window's maximize button is not the focused element",
  205. maximizeButton.equals(driver.switchTo().activeElement()));
  206. pressKeyAndWait(Keys.DELETE);
  207. assertTrue(
  208. "Window is maximized by DELETE when maximize button is the focused element",
  209. !windowElement.isMaximized());
  210. pressKeyAndWait(Keys.ARROW_UP);
  211. assertTrue(
  212. "Window is cmaximized by ARROW_UP when maximize button is the focused element",
  213. !windowElement.isMaximized());
  214. pressKeyAndWait(Keys.ADD);
  215. assertTrue(
  216. "Window is maximized by ADD when maximize button is the focused element",
  217. !windowElement.isMaximized());
  218. }
  219. /**
  220. * Scenario: close button of opened window is not focused -> press keys
  221. * ENTER and SPACE -> window should remain non-maximized after all actions
  222. */
  223. @Test
  224. public void testNonfocusedKeyDoesntMaximizeWindow() throws IOException {
  225. assertTrue("Window is not open",
  226. findElements(By.id("testWindow")).size() == 1);
  227. WindowElement windowElement = $(WindowElement.class).first();
  228. WebElement maximizeButton = windowElement
  229. .findElement(By.className(MAXIMIZE_BOX_CLASS));
  230. assertTrue("Window's close button is the focused element",
  231. !maximizeButton.equals(driver.switchTo().activeElement()));
  232. pressKeyAndWait(Keys.ENTER);
  233. assertTrue(
  234. "Window is maximized by ENTER when maximize button is not the focused element",
  235. !windowElement.isMaximized());
  236. pressKeyAndWait(Keys.SPACE);
  237. assertTrue(
  238. "Window is maximized by SPACE when maximize button is not the focused element",
  239. !windowElement.isMaximized());
  240. }
  241. /**
  242. * Scenario: focus maximize button of opened window -> press keys TAB, and
  243. * TAB+SHIFT in succession, shifting focus from and back to the button ->
  244. * press ENTER key -> window should be maximized
  245. */
  246. @Test
  247. public void testShiftFocusAndMaximizeWindow() throws IOException {
  248. assertTrue("Window is not open",
  249. findElements(By.id("testWindow")).size() == 1);
  250. WindowElement windowElement = $(WindowElement.class).first();
  251. WebElement maximizeButton = windowElement
  252. .findElement(By.className(MAXIMIZE_BOX_CLASS));
  253. setFocusToElementAndWait(maximizeButton);
  254. assertTrue("Window's maximize button is not the focused element",
  255. maximizeButton.equals(driver.switchTo().activeElement()));
  256. pressKeyAndWait(Keys.TAB);
  257. assertTrue("Window's maximize button is the focused element",
  258. !maximizeButton.equals(driver.switchTo().activeElement()));
  259. pressKeyCombinations(Keys.SHIFT, Keys.TAB);
  260. assertTrue("Window's maximize button is not the focused element",
  261. maximizeButton.equals(driver.switchTo().activeElement()));
  262. pressKeyAndWait(Keys.ENTER);
  263. assertTrue(
  264. "Window is not maximized when focus is shifted back-and-forth",
  265. windowElement.isMaximized());
  266. }
  267. /**
  268. * Scenario: focus maximize button of opened window -> click maximize button
  269. * with mouse cursor -> window should be maximized
  270. */
  271. @Test
  272. public void testMouseClickMaximizesWindowOnFocus() throws IOException {
  273. assertTrue("Window is not open",
  274. findElements(By.id("testWindow")).size() == 1);
  275. WindowElement windowElement = $(WindowElement.class).first();
  276. WebElement maximizeButton = windowElement
  277. .findElement(By.className(MAXIMIZE_BOX_CLASS));
  278. setFocusToElementAndWait(maximizeButton);
  279. assertTrue("Window's maximize button is not the focused element",
  280. maximizeButton.equals(driver.switchTo().activeElement()));
  281. // click button with mouse and wait
  282. maximizeButton.click();
  283. sleep(100);
  284. assertTrue("Window is not maximized when focused element is clicked",
  285. windowElement.isMaximized());
  286. }
  287. /**
  288. * Scenario: focus the maximize button of the opened window -> press ENTER
  289. * key -> window should be maximized -> press ENTER key again -> window
  290. * should be restored
  291. */
  292. @Test
  293. public void testMaximizeAndRestoreWindowWithEnter() throws IOException {
  294. assertTrue("Window is not open",
  295. findElements(By.id("testWindow")).size() == 1);
  296. WindowElement windowElement = $(WindowElement.class).first();
  297. WebElement closeButton = windowElement
  298. .findElement(By.className(MAXIMIZE_BOX_CLASS));
  299. setFocusToElementAndWait(closeButton);
  300. assertTrue("Window's maximize button is not the focused element",
  301. closeButton.equals(driver.switchTo().activeElement()));
  302. pressKeyAndWait(Keys.ENTER);
  303. assertTrue("Window is not maximized", windowElement.isMaximized());
  304. assertTrue("Window's maximize button is not the focused element",
  305. closeButton.equals(driver.switchTo().activeElement()));
  306. pressKeyAndWait(Keys.ENTER);
  307. assertTrue("Window remains maximized", !windowElement.isMaximized());
  308. }
  309. protected void setFocusToElementAndWait(WebElement element) {
  310. String elementId = element.getAttribute("id");
  311. ((JavascriptExecutor) getDriver()).executeScript(
  312. "document.getElementById('" + elementId + "').focus();",
  313. element);
  314. sleep(100);
  315. }
  316. protected void pressKeyAndWait(Keys... key) {
  317. new Actions(driver).sendKeys(key).build().perform();
  318. sleep(1000);
  319. }
  320. // That is a workaround after Chrome 75, sendKeys(Keys.shift, Keys.Tab)
  321. // doesn't work
  322. protected void pressKeyCombinations(Keys keyModifier, Keys key) {
  323. new Actions(getDriver()).keyDown(keyModifier).perform();
  324. new Actions(getDriver()).sendKeys(Keys.chord(key)).perform();
  325. new Actions(getDriver()).keyUp(keyModifier).perform();
  326. }
  327. }