Search in sources :

Example 11 with ChromeDriverService

use of org.openqa.selenium.chrome.ChromeDriverService in project Bytecoder by mirkosertic.

the class BytecoderMavenMojo method wat2wasm.

private int[] wat2wasm(WASMCompileResult aResult) throws IOException {
    String theChromeDriverBinary = System.getenv("CHROMEDRIVER_BINARY");
    if (theChromeDriverBinary == null || theChromeDriverBinary.isEmpty()) {
        throw new RuntimeException("No chromedriver binary found! Please set CHROMEDRIVER_BINARY environment variable!");
    }
    ChromeDriverService.Builder theDriverServiceBuilder = new ChromeDriverService.Builder();
    theDriverServiceBuilder = theDriverServiceBuilder.withVerbose(false);
    theDriverServiceBuilder = theDriverServiceBuilder.usingDriverExecutable(new File(theChromeDriverBinary));
    ChromeDriverService theDriverService = theDriverServiceBuilder.build();
    theDriverService.start();
    File theTempDirectory = Files.createTempDir();
    File theGeneratedFile = new File(theTempDirectory, "compile.html");
    // Copy WABT Tools
    File theWABTFile = new File(theTempDirectory, "libwabt.js");
    try (FileOutputStream theOS = new FileOutputStream(theWABTFile)) {
        IOUtils.copy(getClass().getResourceAsStream("/libwabt.js"), theOS);
    }
    PrintWriter theWriter = new PrintWriter(theGeneratedFile);
    theWriter.println("<html>");
    theWriter.println("    <body>");
    theWriter.println("        <h1>Module code</h1>");
    theWriter.println("        <pre id=\"modulecode\">");
    theWriter.println(aResult.getData());
    theWriter.println("        </pre>");
    theWriter.println("        <h1>Compilation result</h1>");
    theWriter.println("        <pre id=\"compileresult\">");
    theWriter.println("        </pre>");
    theWriter.println("        <script src=\"libwabt.js\">");
    theWriter.println("        </script>");
    theWriter.println("        <script>");
    theWriter.println("            function compile() {");
    theWriter.println("                console.log('Compilation started');");
    theWriter.println("                try {");
    theWriter.println("                    var module = wabt.parseWat('test.wast', document.getElementById(\"modulecode\").innerText);");
    theWriter.println("                    module.resolveNames();");
    theWriter.println("                    module.validate();");
    theWriter.println("                    var binaryOutput = module.toBinary({log: true, write_debug_names:true});");
    theWriter.println("                    document.getElementById(\"compileresult\").innerText = binaryOutput.log;");
    theWriter.println("                    return binaryOutput.buffer;");
    theWriter.println("                } catch (e) {");
    theWriter.println("                    document.getElementById(\"compileresult\").innerText = e.toString();");
    theWriter.println("                    console.log(e.toString());");
    theWriter.println("                    console.log(e.stack);");
    theWriter.println("                }");
    theWriter.println("            }");
    theWriter.println("        </script>");
    theWriter.println("    </body>");
    theWriter.println("</html>");
    theWriter.flush();
    theWriter.close();
    ChromeOptions theOptions = new ChromeOptions();
    theOptions.addArguments("headless");
    theOptions.addArguments("disable-gpu");
    LoggingPreferences theLoggingPreferences = new LoggingPreferences();
    theLoggingPreferences.enable(LogType.BROWSER, Level.ALL);
    theOptions.setCapability(CapabilityType.LOGGING_PREFS, theLoggingPreferences);
    DesiredCapabilities theCapabilities = DesiredCapabilities.chrome();
    theCapabilities.setCapability(ChromeOptions.CAPABILITY, theOptions);
    RemoteWebDriver theDriver = new RemoteWebDriver(theDriverService.getUrl(), theCapabilities);
    theDriver.get(theGeneratedFile.toURI().toURL().toString());
    ArrayList<Long> theResult = (ArrayList<Long>) theDriver.executeScript("return compile();");
    int[] theBinaryDara = new int[theResult.size()];
    for (int i = 0; i < theResult.size(); i++) {
        long theLongValue = theResult.get(i);
        theBinaryDara[i] = (int) (theLongValue);
    }
    List<LogEntry> theAll = theDriver.manage().logs().get(LogType.BROWSER).getAll();
    for (LogEntry theEntry : theAll) {
        System.out.println(theEntry.getMessage());
    }
    theDriver.close();
    theDriverService.stop();
    return theBinaryDara;
}
Also used : RemoteWebDriver(org.openqa.selenium.remote.RemoteWebDriver) DesiredCapabilities(org.openqa.selenium.remote.DesiredCapabilities) ArrayList(java.util.ArrayList) LoggingPreferences(org.openqa.selenium.logging.LoggingPreferences) ChromeDriverService(org.openqa.selenium.chrome.ChromeDriverService) FileOutputStream(java.io.FileOutputStream) ChromeOptions(org.openqa.selenium.chrome.ChromeOptions) SourceFile(com.google.javascript.jscomp.SourceFile) File(java.io.File) LogEntry(org.openqa.selenium.logging.LogEntry) PrintWriter(java.io.PrintWriter)

Example 12 with ChromeDriverService

use of org.openqa.selenium.chrome.ChromeDriverService in project flow by vaadin.

the class ChromeBrowserTest method tryCreateHeadlessChromeDriver.

private static WebDriver tryCreateHeadlessChromeDriver(Consumer<ChromeOptions> optionsUpdater) {
    ChromeOptions headlessOptions = createHeadlessChromeOptions();
    optionsUpdater.accept(headlessOptions);
    int port = findFreePort();
    ChromeDriverService service = new ChromeDriverService.Builder().usingPort(port).build();
    ChromeDriver chromeDriver = new ChromeDriver(service, headlessOptions);
    return TestBench.createDriver(chromeDriver);
}
Also used : ChromeOptions(org.openqa.selenium.chrome.ChromeOptions) ChromeDriverService(org.openqa.selenium.chrome.ChromeDriverService) ChromeDriver(org.openqa.selenium.chrome.ChromeDriver)

Aggregations

ChromeDriverService (org.openqa.selenium.chrome.ChromeDriverService)12 ChromeDriver (org.openqa.selenium.chrome.ChromeDriver)9 Test (org.junit.Test)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 File (java.io.File)4 ChromeOptions (org.openqa.selenium.chrome.ChromeOptions)4 DesiredCapabilities (org.openqa.selenium.remote.DesiredCapabilities)3 TechnicalException (com.github.noraui.exception.TechnicalException)1 SourceFile (com.google.javascript.jscomp.SourceFile)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Capabilities (org.openqa.selenium.Capabilities)1 WebDriver (org.openqa.selenium.WebDriver)1 LogEntry (org.openqa.selenium.logging.LogEntry)1 LoggingPreferences (org.openqa.selenium.logging.LoggingPreferences)1 RemoteWebDriver (org.openqa.selenium.remote.RemoteWebDriver)1