mirror of
https://github.com/microsoft/playwright-java.git
synced 2025-09-08 21:01:00 +00:00
test: convert ElementHandle.{content,owner}Frame tests (#30)
This commit is contained in:
parent
5e02d0ae49
commit
fcfe9245cf
@ -125,8 +125,11 @@ class ElementHandleImpl extends JSHandleImpl implements ElementHandle {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Frame contentFrame() {
|
public Frame contentFrame() {
|
||||||
JsonObject result = sendMessage("contentFrame", new JsonObject()).getAsJsonObject();
|
JsonObject json = sendMessage("contentFrame", new JsonObject()).getAsJsonObject();
|
||||||
return connection.getExistingObject(result.getAsJsonObject("frame").get("guid").getAsString());
|
if (!json.has("frame")) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return connection.getExistingObject(json.getAsJsonObject("frame").get("guid").getAsString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -204,6 +207,9 @@ class ElementHandleImpl extends JSHandleImpl implements ElementHandle {
|
|||||||
@Override
|
@Override
|
||||||
public Frame ownerFrame() {
|
public Frame ownerFrame() {
|
||||||
JsonObject json = sendMessage("ownerFrame", new JsonObject()).getAsJsonObject();
|
JsonObject json = sendMessage("ownerFrame", new JsonObject()).getAsJsonObject();
|
||||||
|
if (!json.has("frame")) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
return connection.getExistingObject(json.getAsJsonObject("frame").get("guid").getAsString());
|
return connection.getExistingObject(json.getAsJsonObject("frame").get("guid").getAsString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,7 +77,6 @@ public class TestBrowserContextExposeFunction extends TestBase {
|
|||||||
context.exposeFunction("baz", args -> null);
|
context.exposeFunction("baz", args -> null);
|
||||||
fail("did not throw");
|
fail("did not throw");
|
||||||
} catch (RuntimeException e) {
|
} catch (RuntimeException e) {
|
||||||
System.out.println(e);
|
|
||||||
assertTrue(e.getMessage().contains("Function \"baz\" has been already registered in one of the pages"));
|
assertTrue(e.getMessage().contains("Function \"baz\" has been already registered in one of the pages"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,76 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) Microsoft Corporation.
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* 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.Test;
|
||||||
|
|
||||||
|
import static com.microsoft.playwright.Utils.attachFrame;
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
public class TestElementHandleContentFrame extends TestBase {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldWork() {
|
||||||
|
page.navigate(server.EMPTY_PAGE);
|
||||||
|
attachFrame(page, "frame1", server.EMPTY_PAGE);
|
||||||
|
ElementHandle elementHandle = page.querySelector("#frame1");
|
||||||
|
Frame frame = elementHandle.contentFrame();
|
||||||
|
assertEquals(page.frames().get(1), frame);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldWorkForCrossProcessIframes() {
|
||||||
|
page.navigate(server.EMPTY_PAGE);
|
||||||
|
attachFrame(page, "frame1", server.CROSS_PROCESS_PREFIX + "/empty.html");
|
||||||
|
ElementHandle elementHandle = page.querySelector("#frame1");
|
||||||
|
Frame frame = elementHandle.contentFrame();
|
||||||
|
assertEquals(page.frames().get(1), frame);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldWorkForCrossFrameEvaluations() {
|
||||||
|
page.navigate(server.EMPTY_PAGE);
|
||||||
|
attachFrame(page, "frame1", server.EMPTY_PAGE);
|
||||||
|
Frame frame = page.frames().get(1);
|
||||||
|
JSHandle jsHandle = frame.evaluateHandle("() => window.top.document.querySelector('#frame1')");
|
||||||
|
ElementHandle elementHandle = jsHandle.asElement();
|
||||||
|
assertNotNull(elementHandle);
|
||||||
|
assertEquals(frame, elementHandle.contentFrame());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldReturnNullForNonIframes() {
|
||||||
|
page.navigate(server.EMPTY_PAGE);
|
||||||
|
attachFrame(page, "frame1", server.EMPTY_PAGE);
|
||||||
|
Frame frame = page.frames().get(1);
|
||||||
|
JSHandle jsHandle = frame.evaluateHandle("() => document.body");
|
||||||
|
ElementHandle elementHandle = jsHandle.asElement();
|
||||||
|
assertNotNull(elementHandle);
|
||||||
|
assertNull(elementHandle.contentFrame());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldReturnNullForDocumentDocumentElement() {
|
||||||
|
page.navigate(server.EMPTY_PAGE);
|
||||||
|
attachFrame(page, "frame1", server.EMPTY_PAGE);
|
||||||
|
Frame frame = page.frames().get(1);
|
||||||
|
JSHandle jsHandle = frame.evaluateHandle("() => document.documentElement");
|
||||||
|
ElementHandle elementHandle = jsHandle.asElement();
|
||||||
|
assertNotNull(elementHandle);
|
||||||
|
assertNull(elementHandle.contentFrame());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,115 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) Microsoft Corporation.
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* 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.Test;
|
||||||
|
|
||||||
|
import static com.microsoft.playwright.Utils.attachFrame;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
|
||||||
|
public class TestElementHandleOwnerFrame extends TestBase {
|
||||||
|
@Test
|
||||||
|
void shouldWork() {
|
||||||
|
page.navigate(server.EMPTY_PAGE);
|
||||||
|
attachFrame(page, "frame1", server.EMPTY_PAGE);
|
||||||
|
Frame frame = page.frames().get(1);
|
||||||
|
JSHandle jsHandle = frame.evaluateHandle("() => document.body");
|
||||||
|
ElementHandle elementHandle = jsHandle.asElement();
|
||||||
|
assertNotNull(elementHandle);
|
||||||
|
assertEquals(frame, elementHandle.ownerFrame());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldWorkForCrossProcessIframes() {
|
||||||
|
page.navigate(server.EMPTY_PAGE);
|
||||||
|
attachFrame(page, "frame1", server.CROSS_PROCESS_PREFIX + "/empty.html");
|
||||||
|
Frame frame = page.frames().get(1);
|
||||||
|
JSHandle jsHandle = frame.evaluateHandle("() => document.body");
|
||||||
|
ElementHandle elementHandle = jsHandle.asElement();
|
||||||
|
assertNotNull(elementHandle);
|
||||||
|
assertEquals(frame, elementHandle.ownerFrame());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldWorkForDocument() {
|
||||||
|
// TODO: test.flaky(platform === "win32" && browserName === "webkit");
|
||||||
|
page.navigate(server.EMPTY_PAGE);
|
||||||
|
attachFrame(page, "frame1", server.EMPTY_PAGE);
|
||||||
|
Frame frame = page.frames().get(1);
|
||||||
|
JSHandle jsHandle = frame.evaluateHandle("document");
|
||||||
|
ElementHandle elementHandle = jsHandle.asElement();
|
||||||
|
assertNotNull(elementHandle);
|
||||||
|
assertEquals(frame, elementHandle.ownerFrame());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldWorkForIframeElements() {
|
||||||
|
page.navigate(server.EMPTY_PAGE);
|
||||||
|
attachFrame(page, "frame1", server.EMPTY_PAGE);
|
||||||
|
Frame frame = page.mainFrame();
|
||||||
|
JSHandle jsHandle = frame.evaluateHandle("() => document.querySelector('#frame1')");
|
||||||
|
ElementHandle elementHandle = jsHandle.asElement();
|
||||||
|
assertNotNull(elementHandle);
|
||||||
|
assertEquals(frame, elementHandle.ownerFrame());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldWorkForCrossFrameEvaluations() {
|
||||||
|
page.navigate(server.EMPTY_PAGE);
|
||||||
|
attachFrame(page, "frame1", server.EMPTY_PAGE);
|
||||||
|
Frame frame = page.mainFrame();
|
||||||
|
JSHandle elementHandle = frame.evaluateHandle( "() => document.querySelector('iframe').contentWindow.document.body");
|
||||||
|
assertEquals(frame.childFrames().get(0), elementHandle.asElement().ownerFrame());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldWorkForDetachedElements() {
|
||||||
|
page.navigate(server.EMPTY_PAGE);
|
||||||
|
JSHandle divHandle = page.evaluateHandle("() => {\n" +
|
||||||
|
" const div = document.createElement('div');\n" +
|
||||||
|
" document.body.appendChild(div);\n" +
|
||||||
|
" return div;\n" +
|
||||||
|
"}");
|
||||||
|
assertEquals(page.mainFrame(), divHandle.asElement().ownerFrame());
|
||||||
|
page.evaluate("() => {\n" +
|
||||||
|
" const div = document.querySelector('div');\n" +
|
||||||
|
" document.body.removeChild(div);\n" +
|
||||||
|
"}");
|
||||||
|
assertEquals(page.mainFrame(), divHandle.asElement().ownerFrame());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldWorkForAdoptedElements() {
|
||||||
|
page.navigate(server.EMPTY_PAGE);
|
||||||
|
Deferred<Event<Page.EventType>> popupEvent = page.waitForEvent(Page.EventType.POPUP);
|
||||||
|
page.evaluate("url => window['__popup'] = window.open(url)", server.EMPTY_PAGE);
|
||||||
|
JSHandle divHandle = page.evaluateHandle("() => {\n" +
|
||||||
|
" const div = document.createElement('div');\n" +
|
||||||
|
" document.body.appendChild(div);\n" +
|
||||||
|
" return div;\n" +
|
||||||
|
"}");
|
||||||
|
assertEquals(page.mainFrame(), divHandle.asElement().ownerFrame());
|
||||||
|
Page popup = (Page) popupEvent.get().data();
|
||||||
|
popup.waitForLoadState(Page.LoadState.DOMCONTENTLOADED).get();
|
||||||
|
page.evaluate("() => {\n" +
|
||||||
|
" const div = document.querySelector('div');\n" +
|
||||||
|
" window['__popup'].document.body.appendChild(div);\n" +
|
||||||
|
"}");
|
||||||
|
assertEquals(popup.mainFrame(), divHandle.asElement().ownerFrame());
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user