-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f784d5a
commit 6445fae
Showing
4 changed files
with
175 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/octopus/decorators/ChromeHeadlessLambdaDecorator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |