|
| 1 | +/* |
| 2 | + * Copyright (c) Microsoft Corporation. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.microsoft.playwright; |
| 18 | + |
| 19 | +import com.microsoft.playwright.Overlay; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +import static org.junit.jupiter.api.Assertions.*; |
| 23 | + |
| 24 | +// Note: The overlay elements are rendered inside a closed shadow root in driver mode, |
| 25 | +// so locator-based DOM assertions are not possible in Java. These tests verify that |
| 26 | +// the protocol calls succeed without errors. |
| 27 | +public class TestOverlay extends TestBase { |
| 28 | + @Test |
| 29 | + void shouldAddAndRemoveOverlay() throws Exception { |
| 30 | + page.navigate(server.EMPTY_PAGE); |
| 31 | + AutoCloseable disposable = page.overlay().show("<div id=\"my-overlay\">Hello Overlay</div>"); |
| 32 | + assertNotNull(disposable); |
| 33 | + disposable.close(); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + void shouldAddMultipleOverlays() throws Exception { |
| 38 | + page.navigate(server.EMPTY_PAGE); |
| 39 | + AutoCloseable d1 = page.overlay().show("<div id=\"overlay-1\">First</div>"); |
| 40 | + AutoCloseable d2 = page.overlay().show("<div id=\"overlay-2\">Second</div>"); |
| 41 | + d1.close(); |
| 42 | + d2.close(); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void shouldHideAndShowOverlays() throws Exception { |
| 47 | + page.navigate(server.EMPTY_PAGE); |
| 48 | + page.overlay().show("<div id=\"my-overlay\">Visible</div>"); |
| 49 | + page.overlay().setVisible(false); |
| 50 | + page.overlay().setVisible(true); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + void shouldSurviveNavigation() { |
| 55 | + page.navigate(server.EMPTY_PAGE); |
| 56 | + page.overlay().show("<div id=\"persistent\">Survives Reload</div>"); |
| 57 | + page.navigate(server.EMPTY_PAGE); |
| 58 | + page.reload(); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void shouldRemoveOverlayAndNotRestoreAfterNavigation() throws Exception { |
| 63 | + page.navigate(server.EMPTY_PAGE); |
| 64 | + AutoCloseable disposable = page.overlay().show("<div id=\"temp\">Temporary</div>"); |
| 65 | + disposable.close(); |
| 66 | + page.reload(); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void shouldSanitizeScriptsFromOverlayHtml() { |
| 71 | + page.navigate(server.EMPTY_PAGE); |
| 72 | + page.overlay().show("<div id=\"safe\">Safe</div><script>window.__injected = true</script>"); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void shouldStripEventHandlersFromOverlayHtml() { |
| 77 | + page.navigate(server.EMPTY_PAGE); |
| 78 | + page.overlay().show("<div id=\"clean\" onclick=\"window.__clicked=true\">Click me</div>"); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void shouldAutoRemoveOverlayAfterTimeout() { |
| 83 | + page.navigate(server.EMPTY_PAGE); |
| 84 | + page.overlay().show("<div id=\"timed\">Temporary</div>", new Overlay.ShowOptions().setDuration(1)); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void shouldAllowStylesInOverlayHtml() { |
| 89 | + page.navigate(server.EMPTY_PAGE); |
| 90 | + page.overlay().show("<div id=\"styled\" style=\"color: red; font-size: 20px;\">Styled</div>"); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void shouldShowChapter() { |
| 95 | + page.navigate(server.EMPTY_PAGE); |
| 96 | + page.overlay().chapter("Chapter Title"); |
| 97 | + page.overlay().chapter("With Description", new Overlay.ChapterOptions().setDescription("Some details").setDuration(100)); |
| 98 | + } |
| 99 | +} |
0 commit comments