-
Notifications
You must be signed in to change notification settings - Fork 18
Add tests and CI integration #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,9 @@ | ||
| version: 2 | ||
| jobs: | ||
| build: | ||
| docker: | ||
| - image: circleci/openjdk:8-node-browsers | ||
| steps: | ||
| - checkout | ||
| - run: npm install | ||
| - run: ./run-snapshots.sh |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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,6 +1,7 @@ | ||
| { | ||
| "private": true, | ||
| "devDependencies": { | ||
| "@percy/agent": "^0.1.0" | ||
| "@percy/agent": "^0.1.0", | ||
| "har-validator": "^5.1.3" | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,13 @@ | ||
| #!/bin/bash | ||
|
|
||
| set -o pipefail | ||
| set -e | ||
|
|
||
| # If we don't have a local Percy agent binary, run npm install. | ||
| if [ ! -f ./node_modules/.bin/percy ]; then | ||
| echo "*** Percy agent not installed. Installing now. ***" | ||
| npm install | ||
| fi | ||
|
|
||
| # Run the tests with snapshots. | ||
| ./node_modules/.bin/percy exec -- mvn test |
This file contains hidden or 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,85 @@ | ||
| package io.percy.selenium; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Arrays; | ||
|
|
||
| import org.junit.jupiter.api.AfterAll; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.openqa.selenium.By; | ||
| import org.openqa.selenium.JavascriptExecutor; | ||
| import org.openqa.selenium.Keys; | ||
| import org.openqa.selenium.WebDriver; | ||
| import org.openqa.selenium.WebElement; | ||
| import org.openqa.selenium.chrome.ChromeDriver; | ||
| import org.openqa.selenium.chrome.ChromeOptions; | ||
|
|
||
| public class SdkTest { | ||
| private static final String TEST_URL = "http://localhost:8000"; | ||
| private static WebDriver driver; | ||
| private static Percy percy; | ||
|
|
||
| @BeforeAll | ||
| public static void openServerAndBrowser() throws IOException { | ||
| TestServer.startServer(); | ||
| ChromeOptions options = new ChromeOptions(); | ||
| options.addArguments( | ||
| "--headless", | ||
| "--disable-web-security", | ||
| "--allow-running-insecure-content", | ||
| "--ignore-certificate-errors"); | ||
| driver = new ChromeDriver(options); | ||
| percy = new Percy(driver); | ||
| } | ||
|
|
||
| @AfterAll | ||
| public static void closeServerAndBrowser() { | ||
| // Close our test browser. | ||
| driver.quit(); | ||
| // Shutdown our server and make sure the threadpool also terminates. | ||
| TestServer.shutdown(); | ||
| } | ||
|
|
||
| @AfterEach | ||
| public void clearLocalStorage() { | ||
| ((JavascriptExecutor) driver).executeScript("window.localStorage.clear()"); | ||
| } | ||
|
|
||
| @Test | ||
| public void takesLocalAppSnapshotWithProvidedName() { | ||
| driver.get(TEST_URL); | ||
| percy.snapshot("Snapshot with provided name"); | ||
| } | ||
|
|
||
| @Test | ||
| public void takesLocalAppSnapshotWithProvidedNameAndWidths() { | ||
| driver.get(TEST_URL); | ||
| percy.snapshot("Snapshot with provided name and widths", Arrays.asList(768, 992, 1200)); | ||
| } | ||
|
|
||
| @Test | ||
| public void takesLocalAppSnapshotWithProvidedNameAndMinHeight() { | ||
| driver.get(TEST_URL); | ||
| percy.snapshot("Snapshot with provided name and min height", null, 2000); | ||
| } | ||
|
|
||
| @Test | ||
| public void takesMultipleSnapshotsInOneTestCase() { | ||
| driver.get(TEST_URL); | ||
|
|
||
| WebElement newTodoEl = driver.findElement(By.className("new-todo")); | ||
| newTodoEl.sendKeys("A new todo to check off"); | ||
| newTodoEl.sendKeys(Keys.RETURN); | ||
| percy.snapshot("Multiple snapshots in one test case -- #1", Arrays.asList(768, 992, 1200)); | ||
|
|
||
| driver.findElement(By.cssSelector("input.toggle")).click(); | ||
| percy.snapshot("Multiple snapshots in one test case -- #2", Arrays.asList(768, 992, 1200)); | ||
| } | ||
|
|
||
| @Test | ||
| public void snapshotsLiveHTTPSSite() { | ||
| driver.get("https://www.google.com"); | ||
| percy.snapshot("Live HTTPS site", Arrays.asList(768, 992, 1200)); | ||
| } | ||
| } |
This file contains hidden or 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,98 @@ | ||
| package io.percy.selenium; | ||
|
|
||
| import java.io.File; | ||
| import java.io.FileInputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.OutputStream; | ||
| import java.net.InetSocketAddress; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
|
|
||
| import com.sun.net.httpserver.HttpContext; | ||
| import com.sun.net.httpserver.HttpExchange; | ||
| import com.sun.net.httpserver.HttpServer; | ||
|
|
||
| /** | ||
| * HTTP server that serves the static files that make our test app. | ||
| */ | ||
| class TestServer { | ||
| // Server port. | ||
| private static final Integer PORT = 8000; | ||
|
|
||
| // Location for static files in our test app. | ||
| private static final String TESTAPP_DIR = "src/test/resources/testapp/"; | ||
| private static final String INDEX_FILE = "index.html"; | ||
|
|
||
| // Recognized Mime type map (extension -> mimetype) | ||
| private static final Map<String, String> MIME_MAP = new HashMap<String, String>(); | ||
| static { | ||
| MIME_MAP.put("html", "text/html"); | ||
| MIME_MAP.put("js", "application/javascript"); | ||
| MIME_MAP.put("css", "text/css"); | ||
| } | ||
|
|
||
| private static ExecutorService executor; | ||
| private static HttpServer server; | ||
|
|
||
| public static HttpServer startServer() throws IOException { | ||
| executor = Executors.newFixedThreadPool(1); | ||
| server = HttpServer.create(new InetSocketAddress(PORT), 0); | ||
| HttpContext context = server.createContext("/"); | ||
| context.setHandler(TestServer::handleRequest); | ||
| server.setExecutor(executor); | ||
| server.start(); | ||
| return server; | ||
| } | ||
|
|
||
| public static void shutdown() { | ||
| if (server != null) { | ||
| server.stop(1); | ||
| } | ||
| if (executor != null) { | ||
| executor.shutdownNow(); | ||
| } | ||
| } | ||
|
|
||
| private static void handleRequest(HttpExchange exchange) throws IOException { | ||
| String requestedPath = exchange.getRequestURI().getPath(); | ||
| if (requestedPath.equals("/")) { | ||
| serveStaticFile(exchange, INDEX_FILE); | ||
| } else { | ||
| if (requestedPath.startsWith("/")) { | ||
| requestedPath = requestedPath.substring(1); | ||
| } | ||
| serveStaticFile(exchange, requestedPath); | ||
| } | ||
| } | ||
|
|
||
| private static void serveStaticFile(HttpExchange exchange, String resourcePath) throws IOException { | ||
| byte[] response; | ||
| int responseCode; | ||
| File file = new File(String.format("%s/%s", TESTAPP_DIR, resourcePath)); | ||
| if (!file.canRead()) { | ||
| response = "404 - File Not Found".getBytes(); | ||
| responseCode = 404; | ||
| } else { | ||
| InputStream in = new FileInputStream(file); | ||
| response = new byte[in.available()]; | ||
| in.read(response); | ||
| responseCode = 200; | ||
| in.close(); | ||
| } | ||
|
|
||
| exchange.getResponseHeaders().add("Content-Type", getMimeType(resourcePath)); | ||
| exchange.sendResponseHeaders(responseCode, response.length); | ||
| OutputStream os = exchange.getResponseBody(); | ||
| os.write(response); | ||
| os.close(); | ||
| } | ||
|
|
||
| private static String getMimeType(String resourcePath) { | ||
| int lastDotIndex = resourcePath.lastIndexOf('.'); | ||
| String extension = lastDotIndex > 0 ? resourcePath.substring(lastDotIndex + 1) : ""; | ||
| return MIME_MAP.getOrDefault(extension, "text/plain"); | ||
| } | ||
| } | ||
This file contains hidden or 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,11 @@ | ||
| App for testing the SDK. Based on the | ||
| [TodoMVC](https://github.com/tastejs/todomvc) | ||
| [Vanilla-ES6](https://github.com/tastejs/todomvc/tree/master/examples/vanilla-es6) | ||
| app, forked at commit | ||
| [c78ae12a1834a11da6236c64a0c0fb06b20b7c51](https://github.com/tastejs/todomvc/tree/c78ae12a1834a11da6236c64a0c0fb06b20b7c51). | ||
|
|
||
| To see the app in action: | ||
|
|
||
| ```bash | ||
| $ open index.html | ||
| ``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow ok, I guess this is a thing! Thanks for making your way through this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW, this is almost identical to what we have in the example app (just lightly simplified here). I could not find a convenient library to do programmatic simple static file serving over HTTP.