Skip to content

Commit

Permalink
Post 32
Browse files Browse the repository at this point in the history
  • Loading branch information
mcasperson committed Oct 6, 2018
1 parent f784d5a commit 6445fae
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/main/java/com/octopus/AutomatedBrowserFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public AutomatedBrowser getAutomatedBrowser(final String browser) {
return getBrowserStackAndroidNoImplicitWait();
}

if ("ChromeNoImplicitWaitLambda".equalsIgnoreCase(browser)) {
return getChromeBrowserNoImplicitWaitLambda();
}

throw new IllegalArgumentException("Unknown browser " + browser);

}
Expand Down Expand Up @@ -121,4 +125,10 @@ private AutomatedBrowser getBrowserStackAndroidNoImplicitWait() {
)
);
}

private AutomatedBrowser getChromeBrowserNoImplicitWaitLambda() {
return new ChromeHeadlessLambdaDecorator(
new WebDriverDecorator()
);
}
}
108 changes: 105 additions & 3 deletions src/main/java/com/octopus/LambdaEntry.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
23 changes: 23 additions & 0 deletions src/test/resources/convert.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<html>
<head>
<script>
function convert() {
var output = document.getElementById("input")
.value
.replace(/\\/g, "\\\\")
.replace(/"/g, "\\\"")
.split("\n")
.join("\\n");

document.getElementById("output").value = "\"" + output + "\"";
}
</script>
</head>
<body>
<textarea id="input" style="width: 100%;" rows="20"></textarea>
<br/>
<input type="button" onclick="convert()" value="Convert" style="width: 100%">
<br/>
<input type="text" id="output" style="width: 100%">
</body>
</html>

0 comments on commit 6445fae

Please sign in to comment.