2020-10-05 14:25:29 -07:00
|
|
|
/**
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
* <p>
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
* <p>
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
* <p>
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.microsoft.playwright;
|
|
|
|
|
|
|
|
import org.junit.jupiter.api.*;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
|
|
|
|
public class TestElementHandleClick {
|
|
|
|
private static Server server;
|
|
|
|
private static Browser browser;
|
|
|
|
private BrowserContext context;
|
|
|
|
private Page page;
|
|
|
|
|
|
|
|
@BeforeAll
|
|
|
|
static void launchBrowser() {
|
2020-10-05 14:30:10 -07:00
|
|
|
Playwright playwright = Playwright.create();
|
2020-10-05 14:25:29 -07:00
|
|
|
BrowserType.LaunchOptions options = new BrowserType.LaunchOptions();
|
|
|
|
browser = playwright.chromium().launch(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
@BeforeAll
|
|
|
|
static void startServer() throws IOException {
|
|
|
|
server = new Server(8907);
|
|
|
|
}
|
|
|
|
|
|
|
|
@AfterAll
|
|
|
|
static void stopServer() throws IOException {
|
|
|
|
browser.close();
|
|
|
|
server.stop();
|
|
|
|
server = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@BeforeEach
|
|
|
|
void setUp() {
|
2020-10-06 21:48:45 -07:00
|
|
|
server.reset();
|
2020-10-05 14:25:29 -07:00
|
|
|
context = browser.newContext();
|
|
|
|
page = context.newPage();
|
|
|
|
}
|
|
|
|
|
|
|
|
@AfterEach
|
|
|
|
void tearDown() {
|
|
|
|
context.close();
|
|
|
|
context = null;
|
|
|
|
page = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-10-06 10:40:47 -07:00
|
|
|
void shouldWork() {
|
2020-10-05 14:25:29 -07:00
|
|
|
page.navigate(server.PREFIX + "/input/button.html");
|
|
|
|
ElementHandle button = page.querySelector("button");
|
|
|
|
button.click();
|
|
|
|
assertEquals("Clicked", page.evaluate("() => window['result']"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-10-06 10:40:47 -07:00
|
|
|
void shouldWorkWithNodeRemoved() {
|
2020-10-05 14:25:29 -07:00
|
|
|
page.navigate(server.PREFIX + "/input/button.html");
|
|
|
|
page.evaluate("() => delete window['Node']");
|
|
|
|
ElementHandle button = page.querySelector("button");
|
|
|
|
button.click();
|
|
|
|
assertEquals("Clicked", page.evaluate("() => window['result']"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-10-06 10:40:47 -07:00
|
|
|
void shouldWorkForShadowDOMV1() {
|
2020-10-05 14:25:29 -07:00
|
|
|
page.navigate(server.PREFIX + "/shadow.html");
|
|
|
|
ElementHandle buttonHandle = page.evaluateHandle("() => window['button']").asElement();
|
|
|
|
buttonHandle.click();
|
|
|
|
assertEquals(true, page.evaluate("clicked"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-10-06 10:40:47 -07:00
|
|
|
void shouldWorkForTextNodes() {
|
2020-10-05 14:25:29 -07:00
|
|
|
page.navigate(server.PREFIX + "/input/button.html");
|
|
|
|
ElementHandle buttonTextNode = page.evaluateHandle("() => document.querySelector('button').firstChild").asElement();
|
|
|
|
buttonTextNode.click();
|
|
|
|
assertEquals("Clicked", page.evaluate("() => window['result']"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-10-06 10:40:47 -07:00
|
|
|
void shouldThrowForDetachedNodes() {
|
2020-10-05 14:25:29 -07:00
|
|
|
page.navigate(server.PREFIX + "/input/button.html");
|
|
|
|
ElementHandle button = page.querySelector("button");
|
|
|
|
page.evaluate("button => button.remove()", button);
|
|
|
|
try {
|
|
|
|
button.click();
|
|
|
|
fail("click should throw");
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
assertTrue(e.getMessage().contains("Element is not attached to the DOM"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-10-06 10:40:47 -07:00
|
|
|
void shouldThrowForHiddenNodesWithForce() {
|
2020-10-05 14:25:29 -07:00
|
|
|
page.navigate(server.PREFIX + "/input/button.html");
|
|
|
|
ElementHandle button = page.querySelector("button");
|
|
|
|
page.evaluate("button => button.style.display = 'none'", button);
|
|
|
|
try {
|
|
|
|
button.click(new ElementHandle.ClickOptions().withForce(true));
|
|
|
|
fail("click should throw");
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
assertTrue(e.getMessage().contains("Element is not visible"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-10-06 10:40:47 -07:00
|
|
|
void shouldThrowForRecursivelyHiddenNodesWithForce() {
|
2020-10-05 14:25:29 -07:00
|
|
|
page.navigate(server.PREFIX + "/input/button.html");
|
|
|
|
ElementHandle button = page.querySelector("button");
|
|
|
|
page.evaluate("button => button.parentElement.style.display = 'none'", button);
|
|
|
|
try {
|
|
|
|
button.click(new ElementHandle.ClickOptions().withForce(true));
|
|
|
|
fail("click should throw");
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
assertTrue(e.getMessage().contains("Element is not visible"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-10-06 10:40:47 -07:00
|
|
|
void shouldThrowForBrElementsWithForce() {
|
2020-10-05 14:25:29 -07:00
|
|
|
page.setContent("hello<br>goodbye");
|
|
|
|
ElementHandle br = page.querySelector("br");
|
|
|
|
try {
|
|
|
|
br.click(new ElementHandle.ClickOptions().withForce(true));
|
|
|
|
fail("click should throw");
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
assertTrue(e.getMessage().contains("Element is outside of the viewport"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-10-06 10:40:47 -07:00
|
|
|
void shouldDoubleClickTheButton() {
|
2020-10-05 14:25:29 -07:00
|
|
|
page.navigate(server.PREFIX + "/input/button.html");
|
|
|
|
page.evaluate("() => {\n" +
|
|
|
|
" window['double'] = false;\n" +
|
|
|
|
" const button = document.querySelector('button');\n" +
|
|
|
|
" button.addEventListener('dblclick', event => {\n" +
|
|
|
|
" window['double'] = true;\n" +
|
|
|
|
" });\n" +
|
|
|
|
"}");
|
|
|
|
ElementHandle button = page.querySelector("button");
|
|
|
|
button.dblclick();
|
|
|
|
assertEquals(true, page.evaluate("double"));
|
|
|
|
assertEquals("Clicked", page.evaluate("result"));
|
|
|
|
}
|
|
|
|
}
|