From 28035f09bba44f5ae2cdd06ac0133339c18e02b0 Mon Sep 17 00:00:00 2001 From: Simon Stewart Date: Mon, 18 Jul 2016 23:55:30 +0100 Subject: [PATCH] Hook up the original results write to the new htmlrunner. Although it doesn't actually report the correct results yet. --- .../selenium/server/htmlrunner/CoreTest.java | 52 ---------------- .../server/htmlrunner/CoreTestCase.java | 11 +++- .../server/htmlrunner/CoreTestSuite.java | 2 +- .../server/htmlrunner/HTMLLauncher.java | 21 ++++++- .../server/htmlrunner/NextStepDecorator.java | 24 ++++++-- .../selenium/server/htmlrunner/Results.java | 60 ++++++++++++++++++- 6 files changed, 105 insertions(+), 65 deletions(-) delete mode 100644 java/server/src/org/openqa/selenium/server/htmlrunner/CoreTest.java diff --git a/java/server/src/org/openqa/selenium/server/htmlrunner/CoreTest.java b/java/server/src/org/openqa/selenium/server/htmlrunner/CoreTest.java deleted file mode 100644 index a35d73d0300a7..0000000000000 --- a/java/server/src/org/openqa/selenium/server/htmlrunner/CoreTest.java +++ /dev/null @@ -1,52 +0,0 @@ -// Licensed to the Software Freedom Conservancy (SFC) under one -// or more contributor license agreements. See the NOTICE file -// distributed with this work for additional information -// regarding copyright ownership. The SFC licenses this file -// to you 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 org.openqa.selenium.server.htmlrunner; - -import com.google.common.base.Preconditions; - -import com.thoughtworks.selenium.Selenium; - -import org.openqa.selenium.By; -import org.openqa.selenium.WebDriver; -import org.openqa.selenium.WebElement; - -import java.util.List; - -public class CoreTest { - - private final String url; - - public CoreTest(String url) { - this.url = Preconditions.checkNotNull(url); - } - - public void run(Results results, WebDriver driver, Selenium selenium) { - if (!driver.getCurrentUrl().equals(url)) { - driver.get(url); - } - - // Are we running a suite or an individual test? - List allTables = driver.findElements(By.id("suiteTable")); - - if (allTables.isEmpty()) { - new CoreTestCase(url).run(results, driver, selenium); - } else { - new CoreTestSuite(url).run(results, driver, selenium); - } - } -} diff --git a/java/server/src/org/openqa/selenium/server/htmlrunner/CoreTestCase.java b/java/server/src/org/openqa/selenium/server/htmlrunner/CoreTestCase.java index 22cc8ac7fdf2e..147d520e77e67 100644 --- a/java/server/src/org/openqa/selenium/server/htmlrunner/CoreTestCase.java +++ b/java/server/src/org/openqa/selenium/server/htmlrunner/CoreTestCase.java @@ -54,18 +54,23 @@ public void run(Results results, WebDriver driver, Selenium selenium) { driver.get(url); } + String rawSource = driver.getPageSource(); List steps = findCommands(driver); TestState state = new TestState(); - List testResults = new ArrayList<>(steps.size()); + List stepResults = new ArrayList<>(steps.size()); NextStepDecorator decorator = NextStepDecorator.IDENTITY; for (LoggableStep step : steps) { LOG.info(step.toString()); decorator = Preconditions.checkNotNull(decorator.evaluate(step, selenium, state), step); - testResults.add(new StepResult(step, null)); + stepResults.add(new StepResult(step, decorator.getCause())); if (!decorator.isOkayToContinueTest()) { break; + } else { + stepResults.add(new StepResult(step, null)); } } + + results.addTest(rawSource, stepResults); } private List findCommands(WebDriver driver) { @@ -124,7 +129,7 @@ public String toString() { } } - private static class StepResult { + static class StepResult { private final LoggableStep step; private final Throwable cause; diff --git a/java/server/src/org/openqa/selenium/server/htmlrunner/CoreTestSuite.java b/java/server/src/org/openqa/selenium/server/htmlrunner/CoreTestSuite.java index 47434c0958dcf..a3112e4e56381 100644 --- a/java/server/src/org/openqa/selenium/server/htmlrunner/CoreTestSuite.java +++ b/java/server/src/org/openqa/selenium/server/htmlrunner/CoreTestSuite.java @@ -62,7 +62,7 @@ public void run(Results results, WebDriver driver, Selenium selenium) { allTables.get(0)); for (String testUrl : allTestUrls) { - new CoreTest(testUrl).run(results, driver, selenium); + new CoreTestCase(testUrl).run(results, driver, selenium); } } } diff --git a/java/server/src/org/openqa/selenium/server/htmlrunner/HTMLLauncher.java b/java/server/src/org/openqa/selenium/server/htmlrunner/HTMLLauncher.java index 2a70f89dad0e0..5cfa2e7330ff9 100644 --- a/java/server/src/org/openqa/selenium/server/htmlrunner/HTMLLauncher.java +++ b/java/server/src/org/openqa/selenium/server/htmlrunner/HTMLLauncher.java @@ -22,7 +22,10 @@ import com.thoughtworks.selenium.Selenium; import com.thoughtworks.selenium.webdriven.WebDriverBackedSelenium; +import org.openqa.selenium.By; +import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.edge.EdgeDriver; import org.openqa.selenium.firefox.FirefoxDriver; @@ -43,12 +46,14 @@ import java.io.File; import java.io.IOException; +import java.io.Writer; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; 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.logging.Logger; @@ -146,9 +151,21 @@ private String runHTMLSuite(String browser, String browserURL, String suiteURL, driver = createDriver(browser); URL suiteUrl = determineSuiteUrl(browserURL, suiteURL); + driver.get(suiteUrl.toString()); Selenium selenium = new WebDriverBackedSelenium(driver, browserURL); - Results results = new Results(); - new CoreTest(suiteUrl.toString()).run(results, driver, selenium); + List allTables = driver.findElements(By.id("suiteTable")); + if (allTables.isEmpty()) { + throw new RuntimeException("Unable to find suite table: " + driver.getPageSource()); + } + String rawSuite = + (String) ((JavascriptExecutor) driver).executeScript("return arguments[0].outerHTML", allTables.get(0)); + Results results = new Results(rawSuite); + new CoreTestSuite(suiteUrl.toString()).run(results, driver, selenium); + + HTMLTestResults htmlResults = results.toSuiteResult(); + try (Writer writer = Files.newBufferedWriter(outputFile.toPath())) { + htmlResults.write(writer); + } return results.isSuccessful() ? "PASSED" : "FAILED"; } finally { diff --git a/java/server/src/org/openqa/selenium/server/htmlrunner/NextStepDecorator.java b/java/server/src/org/openqa/selenium/server/htmlrunner/NextStepDecorator.java index 35e2156b20260..d6c716213f529 100644 --- a/java/server/src/org/openqa/selenium/server/htmlrunner/NextStepDecorator.java +++ b/java/server/src/org/openqa/selenium/server/htmlrunner/NextStepDecorator.java @@ -22,7 +22,7 @@ abstract class NextStepDecorator { - static NextStepDecorator IDENTITY = new NextStepDecorator() { + static NextStepDecorator IDENTITY = new NextStepDecorator(null) { @Override public boolean isOkayToContinueTest() { @@ -30,7 +30,7 @@ public boolean isOkayToContinueTest() { } }; - static NextStepDecorator ASSERTION_FAILED = new NextStepDecorator() { + static NextStepDecorator ASSERTION_FAILED = new NextStepDecorator(null) { @Override public boolean isOkayToContinueTest() { @@ -38,7 +38,7 @@ public boolean isOkayToContinueTest() { } }; - static NextStepDecorator VERIFICATION_FAILED = new NextStepDecorator() { + static NextStepDecorator VERIFICATION_FAILED = new NextStepDecorator(null) { @Override public boolean isOkayToContinueTest() { @@ -46,14 +46,28 @@ public boolean isOkayToContinueTest() { } }; - public abstract boolean isOkayToContinueTest(); + private final Throwable cause; + + public NextStepDecorator() { + this(null); + } + + public NextStepDecorator(Throwable cause) { + this.cause = cause; + } + + public abstract boolean isOkayToContinueTest(); public NextStepDecorator evaluate(CoreStep nextStep, Selenium selenium, TestState state) { return nextStep.execute(selenium, state); } + public Throwable getCause() { + return cause; + } + public static NextStepDecorator ERROR(Throwable cause) { - return new NextStepDecorator() { + return new NextStepDecorator(cause) { @Override public boolean isOkayToContinueTest() { return false; diff --git a/java/server/src/org/openqa/selenium/server/htmlrunner/Results.java b/java/server/src/org/openqa/selenium/server/htmlrunner/Results.java index b7f7bc66a79a9..c7eefde44012a 100644 --- a/java/server/src/org/openqa/selenium/server/htmlrunner/Results.java +++ b/java/server/src/org/openqa/selenium/server/htmlrunner/Results.java @@ -17,15 +17,71 @@ package org.openqa.selenium.server.htmlrunner; +import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static java.util.concurrent.TimeUnit.SECONDS; + +import org.openqa.selenium.internal.BuildInfo; + +import java.util.LinkedList; +import java.util.List; + public class Results { + private final String suiteSource; + private final List results = new LinkedList<>(); + private final List allTables = new LinkedList<>(); + private final StringBuilder log = new StringBuilder(); + private final long start = System.currentTimeMillis(); + private boolean succeeded = true; + private long numberOfPasses; + private long commandPasses; + private long commandFailures; + private long commandErrors; + + public Results(String suiteSource) { + this.suiteSource = suiteSource; + } public boolean isSuccessful() { return succeeded; } - public void addTestFailure() { - succeeded = false; + public void addTest(String rawSource, List stepResults) { + allTables.add(rawSource); + boolean passed = true; + for (CoreTestCase.StepResult stepResult : stepResults) { + passed &= stepResult.isSuccessful(); + if (stepResult.isSuccessful()) { + commandPasses++; + } else if (stepResult.isError()) { + commandErrors++; + } else { + commandFailures++; + } + } + + if (passed) { + numberOfPasses++; + } + } + + public HTMLTestResults toSuiteResult() { + BuildInfo buildInfo = new BuildInfo(); + + return new HTMLTestResults( + buildInfo.getReleaseLabel(), + buildInfo.getBuildRevision(), + isSuccessful() ? "PASS" : "FAIL", + String.valueOf(SECONDS.convert(System.currentTimeMillis() - start, MILLISECONDS)), + String.valueOf(results.size()), + String.valueOf(numberOfPasses), + String.valueOf(results.size() - numberOfPasses), + String.valueOf(commandPasses), + String.valueOf(commandFailures), + String.valueOf(commandErrors), + suiteSource, + allTables, + log.toString()); } }