From fcfe9245cf191e689e53e0c88fcd4d972cc498bb Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Wed, 21 Oct 2020 13:56:59 -0700 Subject: [PATCH] test: convert ElementHandle.{content,owner}Frame tests (#30) --- .../playwright/impl/ElementHandleImpl.java | 10 +- .../TestBrowserContextExposeFunction.java | 1 - .../TestElementHandleContentFrame.java | 76 ++++++++++++ .../TestElementHandleOwnerFrame.java | 115 ++++++++++++++++++ 4 files changed, 199 insertions(+), 3 deletions(-) create mode 100644 playwright/src/test/java/com/microsoft/playwright/TestElementHandleContentFrame.java create mode 100644 playwright/src/test/java/com/microsoft/playwright/TestElementHandleOwnerFrame.java diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/ElementHandleImpl.java b/playwright/src/main/java/com/microsoft/playwright/impl/ElementHandleImpl.java index 70e120f0..11d7f04d 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/ElementHandleImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/ElementHandleImpl.java @@ -125,8 +125,11 @@ class ElementHandleImpl extends JSHandleImpl implements ElementHandle { @Override public Frame contentFrame() { - JsonObject result = sendMessage("contentFrame", new JsonObject()).getAsJsonObject(); - return connection.getExistingObject(result.getAsJsonObject("frame").get("guid").getAsString()); + JsonObject json = sendMessage("contentFrame", new JsonObject()).getAsJsonObject(); + if (!json.has("frame")) { + return null; + } + return connection.getExistingObject(json.getAsJsonObject("frame").get("guid").getAsString()); } @Override @@ -204,6 +207,9 @@ class ElementHandleImpl extends JSHandleImpl implements ElementHandle { @Override public Frame ownerFrame() { JsonObject json = sendMessage("ownerFrame", new JsonObject()).getAsJsonObject(); + if (!json.has("frame")) { + return null; + } return connection.getExistingObject(json.getAsJsonObject("frame").get("guid").getAsString()); } diff --git a/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextExposeFunction.java b/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextExposeFunction.java index f261a1a6..2c5bfa32 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextExposeFunction.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextExposeFunction.java @@ -77,7 +77,6 @@ public class TestBrowserContextExposeFunction extends TestBase { context.exposeFunction("baz", args -> null); fail("did not throw"); } catch (RuntimeException e) { - System.out.println(e); assertTrue(e.getMessage().contains("Function \"baz\" has been already registered in one of the pages")); } } diff --git a/playwright/src/test/java/com/microsoft/playwright/TestElementHandleContentFrame.java b/playwright/src/test/java/com/microsoft/playwright/TestElementHandleContentFrame.java new file mode 100644 index 00000000..cdcdad42 --- /dev/null +++ b/playwright/src/test/java/com/microsoft/playwright/TestElementHandleContentFrame.java @@ -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()); + } +} diff --git a/playwright/src/test/java/com/microsoft/playwright/TestElementHandleOwnerFrame.java b/playwright/src/test/java/com/microsoft/playwright/TestElementHandleOwnerFrame.java new file mode 100644 index 00000000..4b67fa8a --- /dev/null +++ b/playwright/src/test/java/com/microsoft/playwright/TestElementHandleOwnerFrame.java @@ -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> 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()); + } +}