Skip to content

Instantly share code, notes, and snippets.

@mike10004
Created February 5, 2020 20:55
Show Gist options
  • Save mike10004/7999b0b25d1a9eb62851d1d4dbe24666 to your computer and use it in GitHub Desktop.
Save mike10004/7999b0b25d1a9eb62851d1d4dbe24666 to your computer and use it in GitHub Desktop.
Program that allows you to test out webdriver API while the browser is open
package manualwebdrivingharness;
import com.github.mike10004.seleniumhelp.*;
import io.github.mike10004.nanochamp.server.NanoControl;
import io.github.mike10004.nanochamp.server.NanoResponse;
import io.github.mike10004.nanochamp.server.NanoServer;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collection;
import java.util.Set;
public class ManualWebdrivingHarness {
public static void main(String[] args) throws Exception {
String html = "<!DOCTYPE html>\n" +
"<html>\n" +
"<head>\n" +
"<style>\n" +
"body {\n" +
" margin: 15px;\n" +
"}\n" +
"</style>\n" +
"</head>\n" +
"<body>\n" +
"<h1>Hello, world</h1>\n" +
"<div>\n" +
"This is some text\n" +
"</div>\n" +
"</body>\n" +
"</html>";
File tmpdir = new File(System.getProperty("java.io.tmpdir"));
File indexFile = new File(tmpdir, "manual.html");
Charset charset = StandardCharsets.UTF_8;
if (!indexFile.exists()) {
java.nio.file.Files.write(indexFile.toPath(), html.getBytes(charset));
}
System.out.format("%s is index file%n", indexFile);
UnitTests.setupRecommendedGeckoDriver();
WebDriverFactory wdf = FirefoxWebDriverFactory.builder()
.acceptInsecureCerts()
.build();
Collection<String> indexPaths = Arrays.asList("/", "/index.html", "/index.htm");
NanoServer server = NanoServer.builder()
.getPath("/next", s -> NanoResponse.status(200).plainTextUtf8("Followed to next page"))
.get(request -> {
if (indexPaths.contains(request.uri.getPath())) {
try {
String text = new String(java.nio.file.Files.readAllBytes(indexFile.toPath()), charset);
return NanoResponse.status(200).htmlUtf8(text);
} catch (IOException e) {
e.printStackTrace(System.err);
return null;
}
}
return null;
})
.build();
try (NanoControl nanoCtrl = server.startServer();
WebdrivingSession session = wdf.startWebdriving(WebdrivingConfig.inactive())) {
WebDriver driver = session.getWebDriver();
String baseUrl = nanoCtrl.baseUri().toString();
driver.get(baseUrl);
String line;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
System.out.println("waiting for input on stdin");
while ((line = reader.readLine()) != null) {
try {
if ("quit".equalsIgnoreCase(line)) {
break;
} else if ("test".equalsIgnoreCase(line)) {
System.out.println("I am listening");
} else if (line.startsWith("click:")) {
String selector = line.substring("click:".length());
WebElement element = driver.findElement(By.cssSelector(selector));
element.click();
System.out.println("sent click element to " + selector);
} else if (line.startsWith("show:")) {
String selector = line.substring("show:".length());
WebElement element = driver.findElement(By.cssSelector(selector));
System.out.format("css: %s%ndisplayed=%s; enabled=%s; selected=%s%n", selector, element.isDisplayed(), element.isEnabled(), element.isSelected());
} else {
System.err.println("not understood: " + line);
}
} catch (RuntimeException e) {
System.err.println(e.toString());
System.err.flush();
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment