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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
package com.vaadin.tests.contextclick;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.DesiredCapabilities;
import com.vaadin.testbench.elements.AbstractComponentElement;
import com.vaadin.testbench.elements.ButtonElement;
import com.vaadin.testbench.parallel.BrowserUtil;
import com.vaadin.testbench.parallel.TestCategory;
import com.vaadin.tests.tb3.MultiBrowserTest;
@TestCategory("contextclick")
public abstract class AbstractContextClickTest extends MultiBrowserTest {
private Pattern defaultLog = Pattern
.compile("[0-9]+. ContextClickEvent: [(]([0-9]+), ([0-9]+)[)]");
@Override
public List<DesiredCapabilities> getBrowsersToTest() {
return getBrowsersSupportingContextMenu();
}
@Before
public void setUp() {
openTestURL();
}
@Test
public void testDefaultListener() {
addOrRemoveDefaultListener();
assertDefaultContextClickListener(1);
}
protected void assertNoContextClickHandler() {
AbstractComponentElement component = $(AbstractComponentElement.class)
.id("testComponent");
String log = getLogRow(0);
contextClick(component);
assertEquals("Log entry without a context click listener.", log,
getLogRow(0));
}
protected void assertDefaultContextClickListener(int index) {
AbstractComponentElement component = $(AbstractComponentElement.class)
.id("testComponent");
int x = 20;
int y = 20;
contextClick(component, x, y);
Point l = component.getLocation();
Matcher matcher = defaultLog.matcher(getLogRow(0));
assertTrue("Log row content did not match default listener output: "
+ getLogRow(0), matcher.find());
int xCoord = Integer.parseInt(matcher.group(1));
int yCoord = Integer.parseInt(matcher.group(2));
int xExpected = l.getX() + x;
int yExpected = l.getY() + y;
assertTrue(
"X Coordinate differs too much from expected. Expected: "
+ xExpected + ", actual: " + xCoord,
Math.abs(xExpected - xCoord) <= 1);
assertTrue(
"Y Coordinate differs too much from expected. Expected: "
+ yExpected + ", actual: " + yCoord,
Math.abs(yExpected - yCoord) <= 1);
}
protected void addOrRemoveDefaultListener() {
$(ButtonElement.class).caption("Add/Remove default listener").first()
.click();
}
protected void addOrRemoveTypedListener() {
$(ButtonElement.class).caption("Add/Remove typed listener").first()
.click();
}
/**
* Performs a context click on given element at coordinates 10, 10 followed
* by a regular click. This prevents browser context menu from blocking
* future operations.
*
* @param e
* web element
*/
protected void contextClick(WebElement e) {
contextClick(e, 10, 10);
}
/**
* Performs a context click on given element at given coordinates followed
* by a regular click. This prevents browser context menu from blocking
* future operations.
*
* @param e
* web element
* @param xCoord
* x coordinate relative to the top-left corner of the element
* @param yCoord
* y coordinate relative to the top-left corner of the element
*/
protected void contextClick(WebElement e, int xCoord, int yCoord) {
if (BrowserUtil.isFirefox(getDesiredCapabilities())) {
// Workaround for Selenium/TB and Firefox 45 issue
int x = e.getLocation().getX() + xCoord;
int y = e.getLocation().getY() + yCoord;
getCommandExecutor().executeScript(
"var ev = document.createEvent('MouseEvents'); ev.initMouseEvent('contextmenu', true, true, document.defaultView, 1, arguments[1], arguments[2], arguments[1], arguments[2], false, false, false, false, 2, null); arguments[0].dispatchEvent(ev);",
e, x, y);
// make sure browser context menu does not block the test
getCommandExecutor().executeScript(
"var ev = document.createEvent('MouseEvents'); ev.initMouseEvent('click', true, true, document.defaultView, 1, arguments[1]-5, arguments[2]-5, arguments[1]-5, arguments[2]-5, false, false, false, false, 1, null); arguments[0].dispatchEvent(ev);",
e, x, y);
} else {
new Actions(getDriver()).moveToElement(e, xCoord, yCoord)
.contextClick().moveByOffset(-5, -5).click().perform();
}
}
}
|