Skip to content

Commit

Permalink
Post 34
Browse files Browse the repository at this point in the history
  • Loading branch information
mcasperson committed Oct 6, 2018
1 parent 6445fae commit 4dbed88
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ http://maven.apache.org/xsd/maven-4.0.0.xsd">
<cucumber.version>2.3.1</cucumber.version>
<shade.version>3.1.0</shade.version>
<aws.lambda.version>1.2.0</aws.lambda.version>
<aws.sdk.version>1.11.305</aws.sdk.version>
<aws.sdk.version>1.11.422</aws.sdk.version>
<commons.io.version>2.6</commons.io.version>
</properties>
<build>
Expand Down Expand Up @@ -61,6 +61,11 @@ http://maven.apache.org/xsd/maven-4.0.0.xsd">
<artifactId>aws-java-sdk-lambda</artifactId>
<version>${aws.sdk.version}</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-ses</artifactId>
<version>${aws.sdk.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ provider:
name: aws
runtime: java8
region: us-east-1
iamRoleStatements:
- Effect: Allow
Resource: "*"
Action:
- ses:SendEmail

package:
artifact: target/webdrivertraining-1.0-SNAPSHOT.jar
Expand Down
34 changes: 33 additions & 1 deletion src/main/java/com/octopus/LambdaEntry.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.octopus;

import com.amazonaws.regions.Regions;
import com.amazonaws.services.simpleemail.AmazonSimpleEmailService;
import com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClientBuilder;
import com.amazonaws.services.simpleemail.model.*;
import org.apache.commons.io.FileUtils;
import java.io.*;
import java.net.MalformedURLException;
Expand All @@ -15,32 +19,39 @@ public class LambdaEntry {
private static final String CHROME_DRIVER =
"https://s3.amazonaws.com/webdriver-testing-resources/chromedriver_linux64.zip";

public String runCucumber(final String feature) throws Throwable {
public String runCucumber(String feature) throws Throwable {

File driverDirectory = null;
File chromeDirectory = null;
File outputFile = null;
File txtOutputFile = null;
File featureFile = null;

try {
driverDirectory = downloadChromeDriver();
chromeDirectory = downloadChromeHeadless();
outputFile = Files.createTempFile("output", ".json").toFile();
txtOutputFile = Files.createTempFile("output", ".txt").toFile();
featureFile = writeFeatureToFile(feature);

cucumber.api.cli.Main.run(
new String[]{
"--monochrome",
"--glue", "com.octopus.decoratorbase",
"--format", "json:" + outputFile.toString(),
"--format", "pretty:" + txtOutputFile.toString(),
featureFile.getAbsolutePath()},
Thread.currentThread().getContextClassLoader());

sendEmail("admin@matthewcasperson.com", FileUtils.readFileToString(txtOutputFile, Charset.defaultCharset()));

return FileUtils.readFileToString(outputFile, Charset.defaultCharset());

} finally {
FileUtils.deleteQuietly(driverDirectory);
FileUtils.deleteQuietly(chromeDirectory);
FileUtils.deleteQuietly(outputFile);
FileUtils.deleteQuietly(txtOutputFile);
FileUtils.deleteQuietly(featureFile);
}
}
Expand Down Expand Up @@ -108,4 +119,25 @@ private File writeFeatureToFile(final String feature) throws IOException {
}
return featureFile;
}

private void sendEmail(final String to, final String results) {
try {
final AmazonSimpleEmailService client = AmazonSimpleEmailServiceClientBuilder.standard()
.withRegion(Regions.US_EAST_1).build();

final SendEmailRequest request = new SendEmailRequest()
.withDestination(new Destination()
.withToAddresses(to))
.withMessage(new Message()
.withBody(new Body()
.withText(new Content()
.withCharset("UTF-8").withData(results)))
.withSubject(new Content()
.withCharset("UTF-8").withData("WebDriver Test Results")))
.withSource("admin@matthewcasperson.com");
client.sendEmail(request);
} catch (final Exception ex) {
System.out.println("The email was not sent. Error message: " + ex.getMessage());
}
}
}

0 comments on commit 4dbed88

Please sign in to comment.