From 6445fae3047fbe5a65d17500bb57bfcbfce7fd21 Mon Sep 17 00:00:00 2001 From: Matthew Casperson Date: Sat, 6 Oct 2018 11:41:40 +1000 Subject: [PATCH] Post 32 --- .../com/octopus/AutomatedBrowserFactory.java | 10 ++ src/main/java/com/octopus/LambdaEntry.java | 108 +++++++++++++++++- .../ChromeHeadlessLambdaDecorator.java | 37 ++++++ src/test/resources/convert.html | 23 ++++ 4 files changed, 175 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/octopus/decorators/ChromeHeadlessLambdaDecorator.java create mode 100644 src/test/resources/convert.html diff --git a/src/main/java/com/octopus/AutomatedBrowserFactory.java b/src/main/java/com/octopus/AutomatedBrowserFactory.java index c92fae61..34ab3e71 100644 --- a/src/main/java/com/octopus/AutomatedBrowserFactory.java +++ b/src/main/java/com/octopus/AutomatedBrowserFactory.java @@ -46,6 +46,10 @@ public AutomatedBrowser getAutomatedBrowser(final String browser) { return getBrowserStackAndroidNoImplicitWait(); } + if ("ChromeNoImplicitWaitLambda".equalsIgnoreCase(browser)) { + return getChromeBrowserNoImplicitWaitLambda(); + } + throw new IllegalArgumentException("Unknown browser " + browser); } @@ -121,4 +125,10 @@ private AutomatedBrowser getBrowserStackAndroidNoImplicitWait() { ) ); } + + private AutomatedBrowser getChromeBrowserNoImplicitWaitLambda() { + return new ChromeHeadlessLambdaDecorator( + new WebDriverDecorator() + ); + } } \ No newline at end of file diff --git a/src/main/java/com/octopus/LambdaEntry.java b/src/main/java/com/octopus/LambdaEntry.java index 5af16faf..8280cb6a 100644 --- a/src/main/java/com/octopus/LambdaEntry.java +++ b/src/main/java/com/octopus/LambdaEntry.java @@ -1,9 +1,111 @@ package com.octopus; -import com.amazonaws.services.lambda.runtime.Context; +import org.apache.commons.io.FileUtils; +import java.io.*; +import java.net.MalformedURLException; +import java.net.URL; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; public class LambdaEntry { - public boolean runCucumber(String feature) throws Throwable { - return true; + private static final String CHROME_HEADLESS_PACKAGE = + "https://s3.amazonaws.com/webdriver-testing-resources/stable-headless-chromium-amazonlinux-2017-03.zip"; + private static final String CHROME_DRIVER = + "https://s3.amazonaws.com/webdriver-testing-resources/chromedriver_linux64.zip"; + + public String runCucumber(final String feature) throws Throwable { + + File driverDirectory = null; + File chromeDirectory = null; + File outputFile = null; + File featureFile = null; + + try { + driverDirectory = downloadChromeDriver(); + chromeDirectory = downloadChromeHeadless(); + outputFile = Files.createTempFile("output", ".json").toFile(); + featureFile = writeFeatureToFile(feature); + + cucumber.api.cli.Main.run( + new String[]{ + "--monochrome", + "--glue", "com.octopus.decoratorbase", + "--format", "json:" + outputFile.toString(), + featureFile.getAbsolutePath()}, + Thread.currentThread().getContextClassLoader()); + + return FileUtils.readFileToString(outputFile, Charset.defaultCharset()); + } finally { + FileUtils.deleteQuietly(driverDirectory); + FileUtils.deleteQuietly(chromeDirectory); + FileUtils.deleteQuietly(outputFile); + FileUtils.deleteQuietly(featureFile); + } + } + + private File downloadChromeDriver() throws IOException { + final File extractedDir = downloadAndExtractFile(CHROME_DRIVER, "chrome_driver"); + final String driver = extractedDir.getAbsolutePath() + "/chromedriver"; + System.setProperty("webdriver.chrome.driver", driver); + new File(driver).setExecutable(true); + return extractedDir; + } + + private File downloadChromeHeadless() throws IOException { + final File extractedDir = downloadAndExtractFile(CHROME_HEADLESS_PACKAGE, "chrome_headless"); + final String chrome = extractedDir.getAbsolutePath() + "/headless-chromium"; + System.setProperty("chrome.binary", chrome); + new File(chrome).setExecutable(true); + return extractedDir; + } + + private File downloadAndExtractFile(final String download, final String tempDirPrefix) throws IOException { + File downloadedFile = null; + try { + downloadedFile = File.createTempFile("download", ".zip"); + FileUtils.copyURLToFile(new URL(download), downloadedFile); + final File extractedDir = Files.createTempDirectory(tempDirPrefix).toFile(); + unzipFile(downloadedFile.getAbsolutePath(), extractedDir.getAbsolutePath()); + return extractedDir; + } finally { + FileUtils.deleteQuietly(downloadedFile); + } + + } + + private void unzipFile(final String fileZip, final String outputDirectory) throws IOException { + + final byte[] buffer = new byte[1024]; + + try (final ZipInputStream zis = new ZipInputStream(new FileInputStream(fileZip))) { + ZipEntry zipEntry = zis.getNextEntry(); + while (zipEntry != null) { + final String fileName = zipEntry.getName(); + final File newFile = new File(outputDirectory + "/" + fileName); + try (final FileOutputStream fos = new FileOutputStream(newFile)) { + int len; + while ((len = zis.read(buffer)) > 0) { + fos.write(buffer, 0, len); + } + } + zipEntry = zis.getNextEntry(); + } + zis.closeEntry(); + } + } + + private File writeFeatureToFile(final String feature) throws IOException { + final File featureFile = File.createTempFile("cucumber", ".feature"); + try { + final URL url = new URL(feature); + FileUtils.copyURLToFile(url, featureFile); + } catch (final MalformedURLException ex) { + try (PrintWriter out = new PrintWriter(featureFile)) { + out.println(feature); + } + } + return featureFile; } } \ No newline at end of file diff --git a/src/main/java/com/octopus/decorators/ChromeHeadlessLambdaDecorator.java b/src/main/java/com/octopus/decorators/ChromeHeadlessLambdaDecorator.java new file mode 100644 index 00000000..5f17d85d --- /dev/null +++ b/src/main/java/com/octopus/decorators/ChromeHeadlessLambdaDecorator.java @@ -0,0 +1,37 @@ +package com.octopus.decorators; + +import com.octopus.AutomatedBrowser; +import com.octopus.decoratorbase.AutomatedBrowserBase; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.chrome.ChromeDriver; +import org.openqa.selenium.chrome.ChromeOptions; + +public class ChromeHeadlessLambdaDecorator extends AutomatedBrowserBase +{ + public ChromeHeadlessLambdaDecorator(final AutomatedBrowser automatedBrowser) { + super(automatedBrowser); + } + + @Override + public void init() { + final ChromeOptions options = new ChromeOptions(); + options.addArguments("--disable-gpu"); + options.addArguments("--headless"); + options.addArguments("--window-size=1366,768"); + options.addArguments("--single-process"); + options.addArguments("--no-sandbox"); + options.addArguments("--user-data-dir=/tmp/user-data"); + options.addArguments("--data-path=/tmp/data-path"); + options.addArguments("--homedir=/tmp"); + options.addArguments("--disk-cache-dir=/tmp/cache-dir"); + + if (System.getProperty("chrome.binary") != null) { + options.setBinary(System.getProperty("chrome.binary")); + } + + options.merge(getDesiredCapabilities()); + final WebDriver webDriver = new ChromeDriver(options); + getAutomatedBrowser().setWebDriver(webDriver); + getAutomatedBrowser().init(); + } +} \ No newline at end of file diff --git a/src/test/resources/convert.html b/src/test/resources/convert.html new file mode 100644 index 00000000..83f384af --- /dev/null +++ b/src/test/resources/convert.html @@ -0,0 +1,23 @@ + + + + + + +
+ +
+ + + \ No newline at end of file