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.

TabKeyboardNavigationTest.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright 2000-2014 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.components.tabsheet;
  17. import java.io.IOException;
  18. import org.junit.Assert;
  19. import org.junit.Test;
  20. import org.openqa.selenium.By;
  21. import org.openqa.selenium.Keys;
  22. import org.openqa.selenium.WebElement;
  23. import com.vaadin.testbench.TestBenchElement;
  24. import com.vaadin.tests.tb3.MultiBrowserTest;
  25. /**
  26. * Add TB3 test as the TB2 one failed on keyboard events.
  27. *
  28. * @since
  29. * @author Vaadin Ltd
  30. */
  31. public class TabKeyboardNavigationTest extends MultiBrowserTest {
  32. @Test
  33. public void testFocus() throws InterruptedException, IOException {
  34. openTestURL();
  35. click(1);
  36. sendKeys(1, Keys.ARROW_RIGHT);
  37. assertSheet(1);
  38. sendKeys(2, Keys.SPACE);
  39. assertSheet(2);
  40. compareScreen("tab2");
  41. sendKeys(2, Keys.ARROW_RIGHT);
  42. sendKeys(3, Keys.ARROW_RIGHT);
  43. assertSheet(2);
  44. sendKeys(5, Keys.SPACE);
  45. assertSheet(5);
  46. compareScreen("skip-disabled-to-tab5");
  47. TestBenchElement addTabButton = (TestBenchElement) getDriver()
  48. .findElements(By.className("v-button")).get(0);
  49. click(addTabButton);
  50. click(5);
  51. sendKeys(5, Keys.ARROW_RIGHT);
  52. assertSheet(5);
  53. sendKeys(6, Keys.SPACE);
  54. assertSheet(6);
  55. click(addTabButton);
  56. click(addTabButton);
  57. click(addTabButton);
  58. click(addTabButton);
  59. click(addTabButton);
  60. click(addTabButton);
  61. click(8);
  62. compareScreen("click-tab-8");
  63. sendKeys(8, Keys.ARROW_RIGHT);
  64. sendKeys(9, Keys.SPACE);
  65. click(9);
  66. compareScreen("tab-9");
  67. sendKeys(9, Keys.ARROW_RIGHT);
  68. Thread.sleep(DELAY);
  69. sendKeys(10, Keys.ARROW_RIGHT);
  70. // Here PhantomJS used to fail. Or when accessing tab2. The fix was to
  71. // call the elem.click(x, y) using the (x, y) position instead of the
  72. // elem.click() without any arguments.
  73. sendKeys(11, Keys.ARROW_RIGHT);
  74. assertSheet(9);
  75. sendKeys(12, Keys.SPACE);
  76. assertSheet(12);
  77. compareScreen("scrolled-right-to-tab-12");
  78. click(5);
  79. sendKeys(5, Keys.ARROW_LEFT);
  80. // Here IE8 used to fail. A hidden <div> in IE8 would have the bounds of
  81. // it's parent, and when trying to see in which direction to scroll
  82. // (left or right) to make the key selected tab visible, the
  83. // VTabSheet.scrollIntoView(Tab) used to check first whether the tab
  84. // isClipped. On IE8 this will always return true for both hidden tabs
  85. // on the left and clipped tabs on the right. So instead of going to
  86. // left, it'll search all the way to the right.
  87. sendKeys(3, Keys.ARROW_LEFT);
  88. sendKeys(2, Keys.ARROW_LEFT);
  89. assertSheet(5);
  90. sendKeys(1, Keys.SPACE);
  91. assertSheet(1);
  92. compareScreen("scrolled-left-to-tab-1");
  93. }
  94. /*
  95. * Press key on the element.
  96. */
  97. private void sendKeys(int tabIndex, Keys key) throws InterruptedException {
  98. sendKeys(tab(tabIndex), key);
  99. }
  100. /*
  101. * Press key on the element.
  102. */
  103. private void sendKeys(TestBenchElement element, Keys key)
  104. throws InterruptedException {
  105. element.sendKeys(key);
  106. if (DELAY > 0) {
  107. sleep(DELAY);
  108. }
  109. }
  110. /*
  111. * Click on the element.
  112. */
  113. private void click(int tabIndex) throws InterruptedException {
  114. click(tab(tabIndex));
  115. }
  116. /*
  117. * Click on the element.
  118. */
  119. private void click(TestBenchElement element) throws InterruptedException {
  120. element.click(10, 10);
  121. if (DELAY > 0) {
  122. sleep(DELAY);
  123. }
  124. }
  125. /*
  126. * Delay for PhantomJS.
  127. */
  128. private final static int DELAY = 10;
  129. private void assertSheet(int index) {
  130. String labelCaption = "Tab " + index;
  131. By id = By.id(TabKeyboardNavigation.labelID(index));
  132. WebElement labelElement = getDriver().findElement(id);
  133. waitForElementPresent(id);
  134. Assert.assertEquals(labelCaption, labelCaption, labelElement.getText());
  135. }
  136. /*
  137. * Provide the tab at specified index.
  138. */
  139. private TestBenchElement tab(int index) {
  140. By by = By.className("v-tabsheet-tabitemcell");
  141. TestBenchElement element = (TestBenchElement) getDriver().findElements(
  142. by).get(index - 1);
  143. String expected = "Tab " + index;
  144. Assert.assertEquals(expected,
  145. element.getText().substring(0, expected.length()));
  146. return element;
  147. }
  148. }