From 0d7e6db538e1b221bd19f5e8d6bf23521a3f549c Mon Sep 17 00:00:00 2001 From: Kyrylo Shpak Date: Fri, 16 Apr 2021 16:10:43 +0200 Subject: [PATCH] Enhance test for code.quarkus.redhat.com site Enhance testcase according to https://github.com/quarkus-qe/quarkus-test-plans/blob/main/QUARKUS-821.md --- pom.xml | 6 + .../ts/startstop/CodeQuarkusSiteTest.java | 123 +++++++++++++++--- 2 files changed, 112 insertions(+), 17 deletions(-) diff --git a/pom.xml b/pom.xml index 7791f6a9..886c6841 100755 --- a/pom.xml +++ b/pom.xml @@ -44,6 +44,7 @@ 3.9 3.4.1.Final 2.13.2 + 2.46.0 generator,startstop,bomtests,codequarkus,special-chars @@ -65,6 +66,11 @@ log4j-core ${log4j.version} + + net.sourceforge.htmlunit + htmlunit + ${htmlunit.version} + diff --git a/testsuite/src/it/java/io/quarkus/ts/startstop/CodeQuarkusSiteTest.java b/testsuite/src/it/java/io/quarkus/ts/startstop/CodeQuarkusSiteTest.java index 7e191e6e..a862353f 100644 --- a/testsuite/src/it/java/io/quarkus/ts/startstop/CodeQuarkusSiteTest.java +++ b/testsuite/src/it/java/io/quarkus/ts/startstop/CodeQuarkusSiteTest.java @@ -1,12 +1,31 @@ package io.quarkus.ts.startstop; +import com.gargoylesoftware.htmlunit.BrowserVersion; +import com.gargoylesoftware.htmlunit.WebClient; +import com.gargoylesoftware.htmlunit.html.HtmlElement; +import com.gargoylesoftware.htmlunit.html.HtmlImage; +import com.gargoylesoftware.htmlunit.html.HtmlPage; +import com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJobManager; import io.quarkus.ts.startstop.utils.Commands; -import io.quarkus.ts.startstop.utils.WebpageTester; import org.jboss.logging.Logger; import org.junit.jupiter.api.Tag; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.TestInfo; -import static org.junit.jupiter.api.Assertions.assertTrue; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; +import java.util.logging.Level; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; /** @@ -20,30 +39,100 @@ public class CodeQuarkusSiteTest { private static final Logger LOGGER = Logger.getLogger(CodeQuarkusSiteTest.class.getName()); public static final String webPageUrl = Commands.getCodeQuarkusURL("https://code.quarkus.redhat.com/"); - public static final String elementIcon = ""; - public static final String elementTitle = "Quarkus - Start coding with code.quarkus.redhat.com"; + public static final String elementTitleByText = "Quarkus - Start coding with code.quarkus.redhat.com"; + public static final String elementIconByXpath = "//link[@rel=\"shortcut icon\"][@href=\"https://www.redhat.com/misc/favicon.ico\"]"; + public static final String elementRedHatLogoByXpath= "//img[@class=\"logo\"][@alt=\"Red Hat Logo\"]"; + public static final String elementSupportedFlagByXpath = "//a[@class=\"extension-tag supported\"]"; + public static final String elementQuarkusPlatformVersionByXpath = "normalize-space(//div[@class=\"quarkus-version\"]/span/text()[last()])"; - public void testRuntime(TestInfo testInfo, String searchElement) throws Exception { + private WebClient webClient; - try { - long timeoutS = 1 * 60; - LOGGER.info("Timeout: " + timeoutS + "s. Waiting for the web to load..."); - long totalTimeMillis = WebpageTester.testWeb(webPageUrl, timeoutS, searchElement, true); - LOGGER.info("Element has been found! Total time: " + totalTimeMillis); - assertTrue(timeoutS * 1000 > totalTimeMillis); - } finally { - LOGGER.info("Test is finished!"); - } + @BeforeEach + public void init(){ + webClient = new WebClient(BrowserVersion.CHROME); + webClient.getOptions().setThrowExceptionOnScriptError(false); + webClient.getOptions().setJavaScriptEnabled(true); + java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF); + } + + @AfterEach + public void close(){ + webClient.close(); } @Test public void validatePresenceOfIcon(TestInfo testInfo) throws Exception { - testRuntime(testInfo, elementIcon); + final HtmlPage page = loadPage(webPageUrl, 60); + LOGGER.info("Trying to find element: " + elementIconByXpath); + HtmlElement shortcutIcon = page.getFirstByXPath(elementIconByXpath); + assertNotNull(shortcutIcon, "Element: " + elementIconByXpath + " is missing!"); } @Test public void validatePresenceOfTitle(TestInfo testInfo) throws Exception { - testRuntime(testInfo, elementTitle); + final HtmlPage page = loadPage(webPageUrl, 60); + LOGGER.info("Verify page title is: " + elementTitleByText); + assertEquals(page.getTitleText(), elementTitleByText, + "Title doesn't match. Found on the web: " + page.getTitleText() + ". Expected: " + elementTitleByText); + } + + @Test + public void validatePresenceOfRedHatLogo(TestInfo testInfo) throws Exception { + final HtmlPage page = loadPage(webPageUrl, 60); + LOGGER.info("Trying to find element: " + elementRedHatLogoByXpath); + HtmlImage redHatLogo = page.getFirstByXPath(elementRedHatLogoByXpath); + assertNotNull(redHatLogo, "Element: " + elementRedHatLogoByXpath + " is missing!"); + } + + @Test + public void validatePresenceOfSupportedFlags(TestInfo testInfo) throws Exception { + final HtmlPage page = loadPage(webPageUrl, 60); + LOGGER.info("Trying to find element: " + elementSupportedFlagByXpath); + List supportedExtensions = page.getByXPath(elementSupportedFlagByXpath); + assertNotNull(supportedExtensions, "Element: " + elementSupportedFlagByXpath + " is missing!"); + } + + @Test + public void validateQuarkusVersionMatch(TestInfo testInfo) throws Exception{ + String quarkusPlatformVersion = ""; + if(System.getProperty("maven.repo.local") == null){ + LOGGER.warn("System property 'maven.repo.local' is not specified. Skip test execution."); + return; + } + Path quarkusProductBomPath = Paths.get(System.getProperty("maven.repo.local")).resolve("com/redhat/quarkus/quarkus-product-bom"); + try (Stream paths = Files.walk(quarkusProductBomPath)) { + List folders = paths.filter(Files::isDirectory).collect(Collectors.toList()); + quarkusPlatformVersion = folders.get(1).getFileName().toString(); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + + final HtmlPage page = loadPage(webPageUrl, 60); + LOGGER.info("Trying to find element: " + elementQuarkusPlatformVersionByXpath); + String quarkusPlatformVersionFromWeb = page.getFirstByXPath(elementQuarkusPlatformVersionByXpath); + + assertNotNull(quarkusPlatformVersionFromWeb, "Element: " + elementQuarkusPlatformVersionByXpath + " is missing!"); + assertEquals(quarkusPlatformVersion, quarkusPlatformVersionFromWeb, + "Quarkus versions doesn't match. Found on the web: " + quarkusPlatformVersionFromWeb + ". Expected: " + quarkusPlatformVersion); + } + + + public HtmlPage loadPage(String url, int timeoutSeconds) throws InterruptedException { + HtmlPage page = null; + long startTime = System.currentTimeMillis(); + long endTime = startTime + timeoutSeconds * 1000; + LOGGER.info("Loading web page " + url); + try { + page = webClient.getPage(url); + } catch (Exception e) { + throw new RuntimeException("Cannot load the page" + webPageUrl); + } + JavaScriptJobManager manager = page.getEnclosingWindow().getJobManager(); + while (manager.getJobCount() > 0 && System.currentTimeMillis() < endTime) { + Thread.sleep(1000); + } + + return page; } } \ No newline at end of file