aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/test/java/com/vaadin/tests/components/table/TableInIframeRowClickScrollJumpTest.java
blob: cf6b6cd04f67694f9269f30596fb28f0bf1d95a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.vaadin.tests.components.table;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertFalse;

import java.util.List;

import org.junit.Test;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;

import com.vaadin.testbench.By;
import com.vaadin.testbench.elements.ButtonElement;
import com.vaadin.testbench.elements.TableElement;
import com.vaadin.tests.tb3.MultiBrowserTest;

/**
 * For testing that UI scroll does not jump back to up when: 1. UI is in iframe
 * 2. the window scrolled down 3. and table is clicked
 *
 * @author Vaadin Ltd
 */
public class TableInIframeRowClickScrollJumpTest extends MultiBrowserTest {

    private static final String TEST_URL = "statictestfiles/TableInIframeRowClickScrollJumpTest.html";

    @Test
    public void testRowClicking_WhenScrolledDown_shouldMaintainScrollPosition()
            throws InterruptedException {
        System.out.println(">>>" + getBaseURL() + TEST_URL);

        driver.get(getUrl());

        // using non-standard way because of iframe
        sleep(4000);

        // make sure we are in the "main content"
        driver.switchTo().defaultContent();
        sleep(2000);
        switchIntoIframe();

        // using non-standard way because of iframe
        waitForElementVisible(By.id("scroll-button"));

        ButtonElement scrollbutton = $(ButtonElement.class).id("scroll-button");
        scrollbutton.click();

        // using non-standard way because of iframe
        sleep(1000);

        Long scrollPosition = getWindowsScrollPosition();

        assertThat("Scroll position should be greater than 100 (it was "
                + scrollPosition + ")", scrollPosition > 100);

        TableElement table = $(TableElement.class).first();
        table.getRow(13).getCell(0).click();

        // using non-standard way because of iframe
        sleep(1000);

        Long scrollPosition2 = getWindowsScrollPosition();

        assertThat(
                "Scroll position should stay about the same. Old was "
                        + scrollPosition + " and new one " + scrollPosition2,
                Math.abs(scrollPosition - scrollPosition2) < 10);
    }

    private String getUrl() {
        String url;
        // using non-standard way because of iframe
        if (getBaseURL().charAt(getBaseURL().length() - 1) == '/') {
            url = getBaseURL() + TEST_URL;
        } else {
            // this one is for gerrit's teamcity :(
            url = getBaseURL() + '/' + TEST_URL;
        }
        return url;
    }

    public void switchIntoIframe() {
        List<WebElement> frames = driver.findElements(By.tagName("iframe"));
        assertFalse("No frames was found", frames.isEmpty());
        driver.switchTo().frame(frames.get(0));
    }

    private Long getWindowsScrollPosition() {
        // measure scroll pos in the main window
        driver.switchTo().defaultContent();

        JavascriptExecutor executor = (JavascriptExecutor) driver;
        Long value = (Long) executor.executeScript(
                "if (window.pageYOffset) return window.pageYOffset;else if (window.document.documentElement.scrollTop) return window.document.documentElement.scrollTop;else return window.document.body.scrollTop;");

        // back to the iframe
        switchIntoIframe();

        return value;
    }

    @Override
    // using non-standard way because of iframe
    protected void closeApplication() {
        if (driver != null) {
            try {
                driver.get(getUrl() + "?closeApplication");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}