123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617 |
- package com.vaadin.tests.components.grid.basicfeatures.escalator;
-
- import static org.junit.Assert.assertEquals;
- import static org.junit.Assert.assertFalse;
- import static org.junit.Assert.assertNotEquals;
- import static org.junit.Assert.assertNotNull;
- import static org.junit.Assert.assertNull;
- import static org.junit.Assert.assertTrue;
-
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
-
- import org.junit.Before;
- import org.junit.ComparisonFailure;
- import org.junit.Test;
- import org.openqa.selenium.By;
- import org.openqa.selenium.Keys;
- import org.openqa.selenium.WebElement;
-
- import com.vaadin.client.WidgetUtil;
- import com.vaadin.shared.Range;
- import com.vaadin.testbench.TestBenchElement;
- import com.vaadin.testbench.elements.NotificationElement;
- import com.vaadin.testbench.parallel.BrowserUtil;
- import com.vaadin.tests.components.grid.basicfeatures.EscalatorBasicClientFeaturesTest;
-
- @SuppressWarnings("boxing")
- public class EscalatorSpacerTest extends EscalatorBasicClientFeaturesTest {
-
- //@formatter:off
- // separate strings made so that eclipse can show the concatenated string by hovering the mouse over the constant
-
- // translate3d(0px, 40px, 123px);
- // translate3d(24px, 15.251px, 0);
- // translate(0, 40px);
- private static final String TRANSLATE_VALUE_REGEX =
- "translate(?:3d|)" // "translate" or "translate3d"
- + "\\(" // literal "("
- + "(" // start capturing the x argument
- + "[0-9]+" // the integer part of the value
- + "(?:" // start of the subpixel part of the value
- + "\\.[0-9]" // if we have a period, there must be at least one number after it
- + "[0-9]*" // any amount of accuracy afterwards is fine
- + ")?" // the subpixel part is optional
- + ")"
- + "(?:px)?" // we don't care if the values are suffixed by "px" or not.
- + ", "
- + "(" // start capturing the y argument
- + "[0-9]+" // the integer part of the value
- + "(?:" // start of the subpixel part of the value
- + "\\.[0-9]" // if we have a period, there must be at least one number after it
- + "[0-9]*" // any amount of accuracy afterwards is fine
- + ")?" // the subpixel part is optional
- + ")"
- + "(?:px)?" // we don't care if the values are suffixed by "px" or not.
- + "(?:, .*?)?" // the possible z argument, uninteresting (translate doesn't have one, translate3d does)
- + "\\)" // literal ")"
- + ";?"; // optional ending semicolon
-
- // 40px;
- // 12.34px
- private static final String PIXEL_VALUE_REGEX =
- "(" // capture the pixel value
- + "[0-9]+" // the pixel argument
- + "(?:" // start of the subpixel part of the value
- + "\\.[0-9]" // if we have a period, there must be at least one number after it
- + "[0-9]*" // any amount of accuracy afterwards is fine
- + ")?" // the subpixel part is optional
- + ")"
- + "(?:px)?" // optional "px" string
- + ";?"; // optional semicolon
- //@formatter:on
-
- // also matches "-webkit-transform";
- private static final Pattern TRANSFORM_CSS_PATTERN = Pattern
- .compile("transform: (.*?);");
- private static final Pattern TOP_CSS_PATTERN = Pattern.compile(
- "top: ([0-9]+(?:\\.[0-9]+)?(?:px)?);?", Pattern.CASE_INSENSITIVE);
- private static final Pattern LEFT_CSS_PATTERN = Pattern.compile(
- "left: ([0-9]+(?:\\.[0-9]+)?(?:px)?);?", Pattern.CASE_INSENSITIVE);
-
- private static final Pattern TRANSLATE_VALUE_PATTERN = Pattern
- .compile(TRANSLATE_VALUE_REGEX);
- private static final Pattern PIXEL_VALUE_PATTERN = Pattern
- .compile(PIXEL_VALUE_REGEX, Pattern.CASE_INSENSITIVE);
-
- @Before
- public void before() {
- setDebug(true);
- openTestURL("theme=reindeer");
- selectMenuPath(COLUMNS_AND_ROWS, BODY_ROWS, "Set 20px default height");
- populate();
- }
-
- @Test
- public void openVisibleSpacer() {
- assertFalse("No spacers should be shown at the start",
- spacersAreFoundInDom());
- selectMenuPath(FEATURES, SPACERS, ROW_1, SET_100PX);
- assertNotNull("Spacer should be shown after setting it", getSpacer(1));
- }
-
- @Test
- public void closeVisibleSpacer() {
- selectMenuPath(FEATURES, SPACERS, ROW_1, SET_100PX);
- selectMenuPath(FEATURES, SPACERS, ROW_1, REMOVE);
- assertNull("Spacer should not exist after removing it", getSpacer(1));
- }
-
- @Test
- public void spacerPushesVisibleRowsDown() {
- double oldTop = getElementTop(getBodyRow(2));
- selectMenuPath(FEATURES, SPACERS, ROW_1, SET_100PX);
- double newTop = getElementTop(getBodyRow(2));
-
- assertGreater("Row below a spacer was not pushed down", newTop, oldTop);
- }
-
- @Test
- public void addingRowAboveSpacerPushesItDown() {
- selectMenuPath(COLUMNS_AND_ROWS, BODY_ROWS, REMOVE_ALL_ROWS);
- selectMenuPath(COLUMNS_AND_ROWS, BODY_ROWS, ADD_ONE_ROW_TO_BEGINNING);
- selectMenuPath(COLUMNS_AND_ROWS, BODY_ROWS, ADD_ONE_ROW_TO_BEGINNING);
-
- selectMenuPath(FEATURES, SPACERS, ROW_1, SET_100PX);
- double oldTop = getElementTop(getSpacer(1));
- selectMenuPath(COLUMNS_AND_ROWS, BODY_ROWS, ADD_ONE_ROW_TO_BEGINNING);
- double newTop = getElementTop(getSpacer(2));
-
- assertGreater("Spacer should've been pushed down (oldTop: " + oldTop
- + ", newTop: " + newTop + ")", newTop, oldTop);
- }
-
- @Test
- public void addingRowBelowSpacerDoesNotPushItDown() {
- selectMenuPath(COLUMNS_AND_ROWS, BODY_ROWS, REMOVE_ALL_ROWS);
- selectMenuPath(COLUMNS_AND_ROWS, BODY_ROWS, ADD_ONE_ROW_TO_BEGINNING);
- selectMenuPath(COLUMNS_AND_ROWS, BODY_ROWS, ADD_ONE_ROW_TO_BEGINNING);
-
- selectMenuPath(FEATURES, SPACERS, ROW_1, SET_100PX);
- double oldTop = getElementTop(getSpacer(1));
- selectMenuPath(COLUMNS_AND_ROWS, BODY_ROWS, ADD_ONE_ROW_TO_END);
- double newTop = getElementTop(getSpacer(1));
-
- assertEquals("Spacer should've not been pushed down", newTop, oldTop,
- WidgetUtil.PIXEL_EPSILON);
- }
-
- @Test
- public void addingRowBelowSpacerIsActuallyRenderedBelowWhenEscalatorIsEmpty() {
- selectMenuPath(COLUMNS_AND_ROWS, BODY_ROWS, REMOVE_ALL_ROWS);
- selectMenuPath(COLUMNS_AND_ROWS, BODY_ROWS, ADD_ONE_ROW_TO_BEGINNING);
- selectMenuPath(COLUMNS_AND_ROWS, BODY_ROWS, ADD_ONE_ROW_TO_BEGINNING);
-
- selectMenuPath(FEATURES, SPACERS, ROW_1, SET_100PX);
- double spacerTop = getElementTop(getSpacer(1));
- selectMenuPath(COLUMNS_AND_ROWS, BODY_ROWS, ADD_ONE_ROW_TO_END);
- double rowTop = getElementTop(getBodyRow(2));
-
- assertEquals("Next row should've been rendered below the spacer",
- spacerTop + 100, rowTop, WidgetUtil.PIXEL_EPSILON);
- }
-
- @Test
- public void addSpacerAtBottomThenScrollThere() {
- selectMenuPath(FEATURES, SPACERS, ROW_99, SET_100PX);
- scrollVerticallyTo(999999);
-
- assertFalse("Did not expect a notification",
- $(NotificationElement.class).exists());
- }
-
- @Test
- public void scrollToBottomThenAddSpacerThere() {
- scrollVerticallyTo(999999);
- long oldBottomScrollTop = getScrollTop();
- selectMenuPath(FEATURES, SPACERS, ROW_99, SET_100PX);
-
- assertEquals(
- "Adding a spacer underneath the current viewport should "
- + "not scroll anywhere",
- oldBottomScrollTop, getScrollTop());
- assertFalse("Got an unexpected notification",
- $(NotificationElement.class).exists());
-
- scrollVerticallyTo(999999);
-
- assertFalse("Got an unexpected notification",
- $(NotificationElement.class).exists());
- assertGreater("Adding a spacer should've made the scrollbar scroll "
- + "further", getScrollTop(), oldBottomScrollTop);
- }
-
- @Test
- public void removingRowAboveSpacerMovesSpacerUp() {
- selectMenuPath(FEATURES, SPACERS, ROW_1, SET_100PX);
- WebElement spacer = getSpacer(1);
- double originalElementTop = getElementTop(spacer);
-
- selectMenuPath(COLUMNS_AND_ROWS, BODY_ROWS,
- REMOVE_ONE_ROW_FROM_BEGINNING);
- assertLessThan("spacer should've moved up", getElementTop(spacer),
- originalElementTop);
- assertNull("No spacer for row 1 should be found after removing the "
- + "top row", getSpacer(1));
- }
-
- @Test
- public void removingSpacedRowRemovesSpacer() {
- selectMenuPath(FEATURES, SPACERS, ROW_1, SET_100PX);
- assertTrue("Spacer should've been found in the DOM",
- spacersAreFoundInDom());
-
- selectMenuPath(COLUMNS_AND_ROWS, BODY_ROWS,
- REMOVE_ONE_ROW_FROM_BEGINNING);
- selectMenuPath(COLUMNS_AND_ROWS, BODY_ROWS,
- REMOVE_ONE_ROW_FROM_BEGINNING);
-
- assertFalse("No spacers should be in the DOM after removing "
- + "associated spacer", spacersAreFoundInDom());
-
- }
-
- @Test
- public void spacersAreFixedInViewport_firstFreezeThenScroll() {
- selectMenuPath(FEATURES, FROZEN_COLUMNS, FREEZE_1_COLUMN);
- selectMenuPath(FEATURES, SPACERS, ROW_1, SET_100PX);
- assertEquals(
- "Spacer's left position should've been 0 at the " + "beginning",
- 0d, getElementLeft(getSpacer(1)), WidgetUtil.PIXEL_EPSILON);
-
- int scrollTo = 10;
- scrollHorizontallyTo(scrollTo);
- assertEquals(
- "Spacer's left position should've been " + scrollTo
- + " after scrolling " + scrollTo + "px",
- scrollTo, getElementLeft(getSpacer(1)),
- WidgetUtil.PIXEL_EPSILON);
- }
-
- @Test
- public void spacersAreFixedInViewport_firstScrollThenFreeze() {
- selectMenuPath(FEATURES, FROZEN_COLUMNS, FREEZE_1_COLUMN);
- int scrollTo = 10;
- scrollHorizontallyTo(scrollTo);
- selectMenuPath(FEATURES, SPACERS, ROW_1, SET_100PX);
- assertEquals(
- "Spacer's left position should've been " + scrollTo
- + " after scrolling " + scrollTo + "px",
- scrollTo, getElementLeft(getSpacer(1)),
- WidgetUtil.PIXEL_EPSILON);
- }
-
- @Test
- public void addingMinusOneSpacerDoesNotScrollWhenScrolledAtTop() {
- scrollVerticallyTo(5);
- selectMenuPath(FEATURES, SPACERS, ROW_MINUS1, SET_100PX);
- assertEquals(
- "No scroll adjustment should've happened when adding the -1 spacer",
- 5, getScrollTop());
- }
-
- @Test
- public void removingMinusOneSpacerScrolls() {
- scrollVerticallyTo(5);
- selectMenuPath(FEATURES, SPACERS, ROW_MINUS1, SET_100PX);
- selectMenuPath(FEATURES, SPACERS, ROW_MINUS1, REMOVE);
- assertEquals("Scroll adjustment should've happened when removing the "
- + "-1 spacer", 0, getScrollTop());
- }
-
- @Test
- public void scrollToRowWorksProperlyWithSpacers() throws Exception {
- selectMenuPath(FEATURES, SPACERS, ROW_MINUS1, SET_100PX);
- selectMenuPath(FEATURES, SPACERS, ROW_1, SET_100PX);
-
- /*
- * we check for row -2 instead of -1, because escalator has one row
- * buffered underneath the footer
- */
- selectMenuPath(COLUMNS_AND_ROWS, BODY_ROWS, SCROLL_TO, ROW_75);
- Thread.sleep(500);
- TestBenchElement cell75 = getBodyCell(-2, 0);
- assertEquals("Row 75: 0,75", cell75.getText());
- // confirm the scroll position
- WebElement footer = findElement(By.className("v-escalator-footer"));
- assertEquals(footer.getLocation().y,
- cell75.getLocation().y + cell75.getSize().height);
-
- selectMenuPath(COLUMNS_AND_ROWS, BODY_ROWS, SCROLL_TO, ROW_25);
- Thread.sleep(500);
-
- try {
- assertEquals("Row 25: 0,25", getBodyCell(0, 0).getText());
- } catch (ComparisonFailure retryForIE10andIE11) {
- /*
- * This seems to be some kind of subpixel/off-by-one-pixel error.
- * Everything's scrolled correctly, but Escalator still loads one
- * row above to the DOM, underneath the header. It's there, but it's
- * not visible. We'll allow for that one pixel error.
- */
- assertEquals("Row 24: 0,24", getBodyCell(0, 0).getText());
- }
- }
-
- @Test
- public void scrollToSpacerFromAbove() throws Exception {
- selectMenuPath(FEATURES, SPACERS, ROW_50, SET_100PX);
- selectMenuPath(FEATURES, SPACERS, ROW_50, SCROLL_HERE_ANY_0PADDING);
-
- // Browsers might vary with a few pixels.
- Range allowableScrollRange = Range.between(765, 780);
- int scrollTop = (int) getScrollTop();
- assertTrue("Scroll position was not " + allowableScrollRange + ", but "
- + scrollTop, allowableScrollRange.contains(scrollTop));
- }
-
- @Test
- public void scrollToSpacerFromBelow() throws Exception {
- selectMenuPath(FEATURES, SPACERS, ROW_50, SET_100PX);
- scrollVerticallyTo(999999);
- selectMenuPath(FEATURES, SPACERS, ROW_50, SCROLL_HERE_ANY_0PADDING);
-
- // Browsers might vary with a few pixels.
- Range allowableScrollRange = Range.between(1015, 1025);
- int scrollTop = (int) getScrollTop();
- assertTrue("Scroll position was not " + allowableScrollRange + ", but "
- + scrollTop, allowableScrollRange.contains(scrollTop));
- }
-
- @Test
- public void scrollToSpacerAlreadyInViewport() throws Exception {
- selectMenuPath(FEATURES, SPACERS, ROW_50, SET_100PX);
- scrollVerticallyTo(1000);
- selectMenuPath(FEATURES, SPACERS, ROW_50, SCROLL_HERE_ANY_0PADDING);
-
- assertEquals(getScrollTop(), 1000);
- }
-
- @Test
- public void scrollToRowAndSpacerFromAbove() throws Exception {
- selectMenuPath(FEATURES, SPACERS, ROW_50, SET_100PX);
- selectMenuPath(FEATURES, SPACERS, ROW_50,
- SCROLL_HERE_SPACERBELOW_ANY_0PADDING);
-
- // Browsers might vary with a few pixels.
- Range allowableScrollRange = Range.between(765, 780);
- int scrollTop = (int) getScrollTop();
- assertTrue("Scroll position was not " + allowableScrollRange + ", but "
- + scrollTop, allowableScrollRange.contains(scrollTop));
- }
-
- @Test
- public void scrollToRowAndSpacerFromBelow() throws Exception {
- selectMenuPath(FEATURES, SPACERS, ROW_50, SET_100PX);
- scrollVerticallyTo(999999);
- selectMenuPath(FEATURES, SPACERS, ROW_50,
- SCROLL_HERE_SPACERBELOW_ANY_0PADDING);
-
- // Browsers might vary with a few pixels.
- Range allowableScrollRange = Range.between(995, 1005);
- int scrollTop = (int) getScrollTop();
- assertTrue("Scroll position was not " + allowableScrollRange + ", but "
- + scrollTop, allowableScrollRange.contains(scrollTop));
- }
-
- @Test
- public void scrollToRowAndSpacerAlreadyInViewport() throws Exception {
- selectMenuPath(FEATURES, SPACERS, ROW_50, SET_100PX);
- scrollVerticallyTo(950);
- selectMenuPath(FEATURES, SPACERS, ROW_50,
- SCROLL_HERE_SPACERBELOW_ANY_0PADDING);
-
- assertEquals(getScrollTop(), 950);
- }
-
- @Test
- public void domCanBeSortedWithFocusInSpacer() throws InterruptedException {
-
- // Firefox behaves badly with focus-related tests - skip it.
- if (BrowserUtil.isFirefox(super.getDesiredCapabilities())) {
- return;
- }
-
- selectMenuPath(FEATURES, SPACERS, FOCUSABLE_UPDATER);
- selectMenuPath(FEATURES, SPACERS, ROW_1, SET_100PX);
-
- WebElement inputElement = getEscalator()
- .findElement(By.tagName("input"));
- inputElement.click();
- scrollVerticallyTo(30);
-
- // Sleep needed because of all the JS we're doing, and to let
- // the DOM reordering to take place.
- Thread.sleep(500);
-
- assertFalse("Error message detected",
- $(NotificationElement.class).exists());
- }
-
- @Test
- public void spacersAreInsertedInCorrectDomPosition() {
- selectMenuPath(FEATURES, SPACERS, ROW_1, SET_100PX);
-
- WebElement tbody = getEscalator().findElement(By.tagName("tbody"));
- WebElement spacer = getChild(tbody, 2);
- String cssClass = spacer.getAttribute("class");
- assertTrue(
- "element index 2 was not a spacer (class=\"" + cssClass + "\")",
- cssClass.contains("-spacer"));
- }
-
- @Test
- public void spacersAreInCorrectDomPositionAfterScroll()
- throws InterruptedException {
- selectMenuPath(FEATURES, SPACERS, ROW_1, SET_100PX);
-
- scrollVerticallyTo(40); // roughly two rows' worth
-
- // both rows should still be within DOM after this little scrolling, so
- // the spacer should be the third element within the body (index: 2)
- WebElement tbody = getEscalator().findElement(By.tagName("tbody"));
- WebElement spacer = getChild(tbody, 2);
- String cssClass = spacer.getAttribute("class");
- assertTrue(
- "element index 2 was not a spacer (class=\"" + cssClass + "\")",
- cssClass.contains("-spacer"));
-
- // Scroll to last DOM row (Row 20). The exact position varies a bit
- // depending on the browser.
- int scrollTo = 172;
- while (scrollTo < 176) {
- scrollVerticallyTo(scrollTo);
- Thread.sleep(500);
-
- // if spacer is still the third (index: 2) body element, i.e. not
- // enough scrolling to re-purpose any rows, scroll a bit further
- spacer = getChild(tbody, 2);
- cssClass = spacer.getAttribute("class");
- if (cssClass.contains("-spacer")) {
- ++scrollTo;
- } else {
- break;
- }
- }
- if (getChild(tbody, 20).getText().startsWith("Row 22:")) {
- // Some browsers scroll too much, spacer should be out of visual
- // range
- assertNull("Element found where there should be none",
- getChild(tbody, 21));
- } else {
- // second row should still be within DOM but the first row out of
- // it, so the spacer should be the second element within the body
- // (index: 1)
- spacer = getChild(tbody, 1);
- cssClass = spacer.getAttribute("class");
- assertTrue("element index 1 was not a spacer (class=\"" + cssClass
- + "\")", cssClass.contains("-spacer"));
- }
- }
-
- @Test
- public void spacerScrolledIntoViewGetsFocus() {
- selectMenuPath(FEATURES, SPACERS, FOCUSABLE_UPDATER);
- selectMenuPath(FEATURES, SPACERS, ROW_50, SET_100PX);
- selectMenuPath(FEATURES, SPACERS, ROW_50, SCROLL_HERE_ANY_0PADDING);
-
- tryToTabIntoFocusUpdaterElement();
- assertEquals("input", getFocusedElement().getTagName());
- }
-
- @Test
- public void spacerScrolledOutOfViewDoesNotGetFocus() {
- selectMenuPath(FEATURES, SPACERS, FOCUSABLE_UPDATER);
- selectMenuPath(FEATURES, SPACERS, ROW_1, SET_100PX);
- selectMenuPath(FEATURES, SPACERS, ROW_50, SCROLL_HERE_ANY_0PADDING);
-
- tryToTabIntoFocusUpdaterElement();
- assertNotEquals("input", getFocusedElement().getTagName());
- }
-
- @Test
- public void spacerOpenedInViewGetsFocus() {
- selectMenuPath(FEATURES, SPACERS, FOCUSABLE_UPDATER);
- selectMenuPath(FEATURES, SPACERS, ROW_1, SET_100PX);
- tryToTabIntoFocusUpdaterElement();
- WebElement focusedElement = getFocusedElement();
- assertEquals("input", focusedElement.getTagName());
- }
-
- @Test
- public void spacerOpenedOutOfViewDoesNotGetFocus() {
- selectMenuPath(FEATURES, SPACERS, FOCUSABLE_UPDATER);
- selectMenuPath(FEATURES, SPACERS, ROW_50, SET_100PX);
-
- tryToTabIntoFocusUpdaterElement();
- assertNotEquals("input", getFocusedElement().getTagName());
- }
-
- @Test
- public void spacerOpenedInViewAndScrolledOutAndBackAgainGetsFocus() {
- selectMenuPath(FEATURES, SPACERS, FOCUSABLE_UPDATER);
- selectMenuPath(FEATURES, SPACERS, ROW_1, SET_100PX);
- selectMenuPath(COLUMNS_AND_ROWS, BODY_ROWS, SCROLL_TO, ROW_50);
- selectMenuPath(FEATURES, SPACERS, ROW_1, SCROLL_HERE_ANY_0PADDING);
-
- tryToTabIntoFocusUpdaterElement();
- assertEquals("input", getFocusedElement().getTagName());
- }
-
- @Test
- public void spacerOpenedOutOfViewAndScrolledInAndBackAgainDoesNotGetFocus() {
- selectMenuPath(FEATURES, SPACERS, FOCUSABLE_UPDATER);
- selectMenuPath(FEATURES, SPACERS, ROW_50, SET_100PX);
- selectMenuPath(FEATURES, SPACERS, ROW_50, SCROLL_HERE_ANY_0PADDING);
- selectMenuPath(COLUMNS_AND_ROWS, BODY_ROWS, SCROLL_TO, ROW_0);
-
- tryToTabIntoFocusUpdaterElement();
- assertNotEquals("input", getFocusedElement().getTagName());
- }
-
- private void tryToTabIntoFocusUpdaterElement() {
- ((TestBenchElement) findElement(By.className("gwt-MenuBar"))).focus();
- WebElement focusedElement = getFocusedElement();
- focusedElement.sendKeys(Keys.TAB);
- }
-
- private WebElement getChild(WebElement parent, int childIndex) {
- return (WebElement) executeScript(
- "return arguments[0].children[" + childIndex + "];", parent);
- }
-
- private static double[] getElementDimensions(WebElement element) {
- /*
- * we need to parse the style attribute, since using getCssValue gets a
- * normalized value that is harder to parse.
- */
- String style = element.getAttribute("style");
-
- String transform = getTransformFromStyle(style);
- if (transform != null) {
- return getTranslateValues(transform);
- }
-
- double[] result = { -1, -1 };
- String left = getLeftFromStyle(style);
- if (left != null) {
- result[0] = getPixelValue(left);
- }
- String top = getTopFromStyle(style);
- if (top != null) {
- result[1] = getPixelValue(top);
- }
-
- if (result[0] != -1 && result[1] != -1) {
- return result;
- } else {
- throw new IllegalArgumentException("Could not parse the position "
- + "information from the CSS \"" + style + "\"");
- }
- }
-
- private static double getElementTop(WebElement element) {
- return getElementDimensions(element)[1];
- }
-
- private static double getElementLeft(WebElement element) {
- return getElementDimensions(element)[0];
- }
-
- private static String getTransformFromStyle(String style) {
- return getFromStyle(TRANSFORM_CSS_PATTERN, style);
- }
-
- private static String getTopFromStyle(String style) {
- return getFromStyle(TOP_CSS_PATTERN, style);
- }
-
- private static String getLeftFromStyle(String style) {
- return getFromStyle(LEFT_CSS_PATTERN, style);
- }
-
- private static String getFromStyle(Pattern pattern, String style) {
- Matcher matcher = pattern.matcher(style);
- if (matcher.find()) {
- assertEquals("wrong amount of groups matched in " + style, 1,
- matcher.groupCount());
- return matcher.group(1);
- } else {
- return null;
- }
- }
-
- /**
- * @return {@code [0] == x}, {@code [1] == y}
- */
- private static double[] getTranslateValues(String translate) {
- Matcher matcher = TRANSLATE_VALUE_PATTERN.matcher(translate);
- assertTrue("no matches for " + translate + " against "
- + TRANSLATE_VALUE_PATTERN, matcher.find());
- assertEquals("wrong amout of groups matched in " + translate, 2,
- matcher.groupCount());
-
- return new double[] { Double.parseDouble(matcher.group(1)),
- Double.parseDouble(matcher.group(2)) };
- }
-
- private static double getPixelValue(String top) {
- Matcher matcher = PIXEL_VALUE_PATTERN.matcher(top);
- assertTrue(
- "no matches for \"" + top + "\" against " + PIXEL_VALUE_PATTERN,
- matcher.find());
- assertEquals("wrong amount of groups matched in " + top, 1,
- matcher.groupCount());
- return Double.parseDouble(matcher.group(1));
- }
- }
|