diff --git a/playwright/src/test/java/com/microsoft/playwright/TestEvalOnSelectorAll.java b/playwright/src/test/java/com/microsoft/playwright/TestEvalOnSelectorAll.java new file mode 100644 index 00000000..ec2efca1 --- /dev/null +++ b/playwright/src/test/java/com/microsoft/playwright/TestEvalOnSelectorAll.java @@ -0,0 +1,136 @@ +/** + * 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.*; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Collections; + +import static org.junit.jupiter.api.Assertions.*; + +public class TestEvalOnSelectorAll { + private static Playwright playwright; + private static Server server; + private static Browser browser; + private static boolean isChromium; + private static boolean isWebKit; + private static boolean headful; + private BrowserContext context; + private Page page; + + @BeforeAll + static void launchBrowser() { + playwright = Playwright.create(); + BrowserType.LaunchOptions options = new BrowserType.LaunchOptions(); + browser = playwright.chromium().launch(options); + isChromium = true; + isWebKit = false; + headful = false; + } + + @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() { + server.reset(); + context = browser.newContext(); + page = context.newPage(); + } + + @AfterEach + void tearDown() { + context.close(); + context = null; + page = null; + } + @Test + void shouldWorkWithCssSelector() { + page.setContent("
hello
beautiful
world!
"); + Object divsCount = page.evalOnSelectorAll("css=div", "divs => divs.length"); + assertEquals(3, divsCount); + } + + @Test + void shouldWorkWithTextSelector() { + page.setContent("
hello
beautiful
beautiful
world!
"); + Object divsCount = page.evalOnSelectorAll("text='beautiful'", "divs => divs.length"); + assertEquals(2, divsCount); + } + + @Test + void shouldWorkWithXpathSelector() { + page.setContent("
hello
beautiful
world!
"); + Object divsCount = page.evalOnSelectorAll("xpath=/html/body/div", "divs => divs.length"); + assertEquals(3, divsCount); + } + + @Test + void shouldAutoDetectCssSelector() { + page.setContent("
hello
beautiful
world!
"); + Object divsCount = page.evalOnSelectorAll("div", "divs => divs.length"); + assertEquals(3, divsCount); + } + + @Test + void shouldSupportSyntax() { + page.setContent("
hello
beautiful
world!
Not this one"); + Object spansCount = page.evalOnSelectorAll("css=div >> css=span", "spans => spans.length"); + assertEquals(3, spansCount); + } + @Test + void shouldSupportCapture() { + page.setContent("
a
b
"); + assertEquals(1, page.evalOnSelectorAll("*css=div >> 'b'", "els => els.length")); + assertEquals(1, page.evalOnSelectorAll("section >> *css=div >> 'b'", "els => els.length")); + assertEquals(4, page.evalOnSelectorAll("section >> *", "els => els.length")); + + page.setContent("
aa
"); + assertEquals(1, page.evalOnSelectorAll("*css=div >> 'a'", "els => els.length")); + assertEquals(1, page.evalOnSelectorAll("section >> *css=div >> 'a'", "els => els.length")); + + page.setContent("
a
a
a
"); + assertEquals(3, page.evalOnSelectorAll("*css=div >> 'a'", "els => els.length")); + assertEquals(1, page.evalOnSelectorAll("section >> *css=div >> 'a'", "els => els.length")); + } + + @Test + void shouldSupportCaptureWhenMultiplePathsMatch() { + page.setContent("
"); + assertEquals(2, page.evalOnSelectorAll("*css=div >> span", "els => els.length")); + page.setContent("
"); + assertEquals(2, page.evalOnSelectorAll("*css=div >> span", "els => els.length")); + } + + @Test + void shouldReturnComplexValues() { + page.setContent("
hello
beautiful
world!
"); + Object texts = page.evalOnSelectorAll("css=div", "divs => divs.map(div => div.textContent)"); + assertEquals(Arrays.asList("hello", "beautiful", "world!"), texts); + } +}