Best Selenium code snippet using org.openqa.selenium.os.ExecutableFinder.find
Source:BuckBuild.java
...47 // we should only need to do this when we're in dev mode48 // when running in a test suite, our dependencies should already49 // be listed.50 log.info("Not in dev mode. Ignoring attempt to build: " + target);51 return findOutput(projectRoot);52 }53 if (target == null || "".equals(target)) {54 throw new IllegalStateException("No targets specified");55 }56 System.out.println("\nBuilding " + target + " ...");57 ImmutableList.Builder<String> builder = ImmutableList.builder();58 findBuck(projectRoot, builder);59 builder.add("build", "--config", "color.ui=never", target);60 ImmutableList<String> command = builder.build();61 CommandLine commandLine = new CommandLine(command.toArray(new String[command.size()]));62 commandLine.copyOutputTo(System.err);63 commandLine.execute();64 if (!commandLine.isSuccessful()) {65 throw new WebDriverException("Build failed! " + target);66 }67 return findOutput(projectRoot);68 }69 private Path findOutput(Path projectRoot) throws IOException {70 ImmutableList.Builder<String> builder = ImmutableList.builder();71 findBuck(projectRoot, builder);72 builder.add("targets", "--show-full-output", "--config", "color.ui=never", target);73 ImmutableList<String> command = builder.build();74 CommandLine commandLine = new CommandLine(command.toArray(new String[command.size()]));75 commandLine.copyOutputTo(System.err);76 commandLine.execute();77 if (!commandLine.isSuccessful()) {78 throw new WebDriverException("Unable to find output! " + target);79 }80 String stdOut = commandLine.getStdOut();81 String[] allLines = stdOut.split(LINE_SEPARATOR.value());82 String lastLine = null;83 for (String line : allLines) {84 if (line.startsWith(target)) {85 lastLine = line;86 break;87 }88 }89 Preconditions.checkNotNull(lastLine, "Value read: %s", stdOut);90 List<String> outputs = Splitter.on(' ').limit(2).splitToList(lastLine);91 if (outputs.size() != 2) {92 throw new WebDriverException(93 String.format("Unable to find output! %s, %s", target, lastLine));94 }95 Path output = projectRoot.resolve(outputs.get(1));96 if (!Files.exists(output)) {97 throw new WebDriverException(98 String.format("Found output, but it does not exist: %s, %s", target, output));99 }100 return output;101 }102 private void findBuck(Path projectRoot, ImmutableList.Builder<String> builder) throws IOException {103 Path noBuckCheck = projectRoot.resolve(".nobuckcheck");104 // If there's a .nobuckcheck in the root of the file, and we can execute "buck", then assume105 // that the developer knows what they're doing. Ha! Ahaha! Ahahahaha!106 if (Files.exists(noBuckCheck)) {107 String buckCommand = new ExecutableFinder().find("buck");108 if (buckCommand != null) {109 builder.add(buckCommand);110 return;111 }112 }113 downloadBuckPexIfNecessary(builder);114 }115 private void downloadBuckPexIfNecessary(ImmutableList.Builder<String> builder)116 throws IOException {117 Path projectRoot = InProject.locate("Rakefile").getParent();118 String buckVersion = new String(Files.readAllBytes(projectRoot.resolve(".buckversion"))).trim();119 Path pex = projectRoot.resolve("buck-out/crazy-fun/" + buckVersion + "/buck.pex");120 String expectedHash = new String(Files.readAllBytes(projectRoot.resolve(".buckhash"))).trim();121 HashCode md5 = Files.exists(pex) ?122 Hashing.md5().hashBytes(Files.readAllBytes(pex)) :123 HashCode.fromString("aa"); // So we have a non-null value124 if (!Files.exists(pex) || !expectedHash.equals(md5.toString())) {125 log.warning("Downloading PEX");126 if (!Files.exists(pex.getParent())) {127 Files.createDirectories(pex.getParent());128 }129 URL url = new URL(String.format(130 "https://github.com/SeleniumHQ/buck/releases/download/buck-release-%s/buck.pex",131 buckVersion));132 HttpURLConnection connection = (HttpURLConnection) url.openConnection();133 connection.setInstanceFollowRedirects(true);134 Files.copy(connection.getInputStream(), pex, REPLACE_EXISTING);135 // Do our best to make this executable136 pex.toFile().setExecutable(true);137 }138 md5 = Hashing.md5().hashBytes(Files.readAllBytes(pex));139 if (!expectedHash.equals(md5.toString())) {140 throw new WebDriverException("Unable to confirm that download is valid");141 }142 if (Platform.getCurrent().is(WINDOWS)) {143 String python = new ExecutableFinder().find("python2");144 if (python == null) {145 python = new ExecutableFinder().find("python");146 }147 Preconditions.checkNotNull(python, "Unable to find python executable");148 builder.add(python);149 }150 builder.add(pex.toAbsolutePath().toString());151 }152 public static void main(String[] args) throws IOException {153 new BuckBuild().of("se3-server").go();154 }155}...
Source:DriverService.java
...47 {48 return url;49 }50 51 protected static File findExecutable(String exeName, String exeProperty, String exeDocs, String exeDownload)52 {53 String defaultPath = new ExecutableFinder().find(exeName);54 String exePath = System.getProperty(exeProperty, defaultPath);55 Preconditions.checkState(exePath != null, "The path to the driver executable must be set by the %s system property; for more information, see %s. The latest version can be downloaded from %s", exeProperty, exeDocs, exeDownload);56 57 File exe = new File(exePath);58 checkExecutable(exe);59 return exe;60 }61 62 protected static void checkExecutable(File exe) {63 Preconditions.checkState(exe.exists(), "The driver executable does not exist: %s", exe64 .getAbsolutePath());65 Preconditions.checkState(!exe.isDirectory(), "The driver executable is a directory: %s", exe66 .getAbsolutePath());67 Preconditions.checkState(exe.canExecute(), "The driver is not executable: %s", exe68 .getAbsolutePath());69 }70 71 public boolean isRunning()72 {73 lock.lock();74 try { boolean bool1;75 if (process == null) {76 return false;77 }78 return process.isRunning();79 } catch (IllegalThreadStateException e) {80 return true;81 } finally {82 lock.unlock();83 }84 }85 86 public void start()87 throws IOException88 {89 lock.lock();90 try {91 if (process != null) {92 return;93 }94 process = new CommandLine(executable, (String[])args.toArray(new String[0]));95 process.setEnvironmentVariables(environment);96 process.copyOutputTo(getOutputStream());97 process.executeAsync();98 99 waitUntilAvailable();100 } finally {101 lock.unlock();102 }103 }104 105 protected void waitUntilAvailable() throws MalformedURLException {106 try {107 URL status = new URL(url.toString() + "/status");108 new UrlChecker().waitUntilAvailable(20L, TimeUnit.SECONDS, new URL[] { status });109 } catch (UrlChecker.TimeoutException e) {110 process.checkForError();111 throw new WebDriverException("Timed out waiting for driver server to start.", e);112 }113 }114 115 public void stop()116 {117 lock.lock();118 119 WebDriverException toThrow = null;120 try {121 if (process == null) {122 return;123 }124 try125 {126 URL killUrl = new URL(url.toString() + "/shutdown");127 new UrlChecker().waitUntilUnavailable(3L, TimeUnit.SECONDS, killUrl);128 } catch (MalformedURLException e) {129 toThrow = new WebDriverException(e);130 } catch (UrlChecker.TimeoutException e) {131 toThrow = new WebDriverException("Timed out waiting for driver server to shutdown.", e);132 }133 134 process.destroy();135 } finally {136 process = null;137 lock.unlock();138 }139 140 if (toThrow != null) {141 throw toThrow;142 }143 }144 145 public void sendOutputTo(OutputStream outputStream) {146 this.outputStream = ((OutputStream)Preconditions.checkNotNull(outputStream));147 }148 149 protected OutputStream getOutputStream() {150 return outputStream;151 }152 153 public static abstract class Builder<DS extends DriverService, B extends Builder<?, ?>>154 {155 private int port = 0;156 private File exe = null;157 private ImmutableMap<String, String> environment = ImmutableMap.of();158 159 private File logFile;160 161 public Builder() {}162 163 public B usingDriverExecutable(File file)164 {165 Preconditions.checkNotNull(file);166 DriverService.checkExecutable(file);167 exe = file;168 return this;169 }170 171 public B usingPort(int port)172 {173 Preconditions.checkArgument(port >= 0, "Invalid port number: %s", port);174 this.port = port;175 return this;176 }177 178 protected int getPort() {179 return port;180 }181 182 public B usingAnyFreePort()183 {184 port = 0;185 return this;186 }187 188 @Beta189 public B withEnvironment(Map<String, String> environment)190 {191 this.environment = ImmutableMap.copyOf(environment);192 return this;193 }194 195 public B withLogFile(File logFile)196 {197 this.logFile = logFile;198 return this;199 }200 201 protected File getLogFile() {202 return logFile;203 }204 205 public DS build()206 {207 if (port == 0) {208 port = PortProber.findFreePort();209 }210 211 if (exe == null) {212 exe = findDefaultExecutable();213 }214 215 ImmutableList<String> args = createArgs();216 217 return createDriverService(exe, port, args, environment);218 }219 220 protected abstract File findDefaultExecutable();221 222 protected abstract ImmutableList<String> createArgs();223 224 protected abstract DS createDriverService(File paramFile, int paramInt, ImmutableList<String> paramImmutableList, ImmutableMap<String, String> paramImmutableMap);225 }226}...
Source:FirefoxExecutable.java
...65 }66 if (PLATFORM_BINARY != null && PLATFORM_BINARY.exists()) {67 return PLATFORM_BINARY;68 }69 throw new WebDriverException("Cannot find firefox binary in PATH. "70 + "Make sure firefox is installed. OS appears to be: " + Platform.getCurrent());71 }72 private static File locateFirefoxBinaryFromSystemProperty() {73 String binaryName = System.getProperty(FirefoxDriver.SystemProperty.BROWSER_BINARY);74 if (binaryName == null)75 return null;76 File binary = new File(binaryName);77 if (binary.exists())78 return binary;79 Platform current = Platform.getCurrent();80 if (current.is(WINDOWS)) {81 if (!binaryName.endsWith(".exe"))82 binaryName += ".exe";83 } else if (current.is(MAC)) {84 if (!binaryName.endsWith(".app"))85 binaryName += ".app";86 binaryName += "/Contents/MacOS/firefox-bin";87 }88 binary = new File(binaryName);89 if (binary.exists())90 return binary;91 throw new WebDriverException(String.format("'%s' property set, but unable to locate the requested binary: %s",92 FirefoxDriver.SystemProperty.BROWSER_BINARY, binaryName));93 }94 /**95 * Locates the firefox binary by platform.96 */97 @SuppressWarnings("deprecation")98 private static File locateFirefoxBinaryFromPlatform() {99 File binary = null;100 Platform current = Platform.getCurrent();101 if (current.is(WINDOWS)) {102 binary = findExistingBinary(WindowsUtils.getPathsInProgramFiles("Mozilla Firefox\\firefox.exe"));103 } else if (current.is(MAC)) {104 binary = new File("/Applications/Firefox.app/Contents/MacOS/firefox-bin");105 // fall back to homebrew install location if default is not found106 if (!binary.exists()) {107 binary = new File(System.getProperty("user.home") + binary.getAbsolutePath());108 }109 }110 if (binary != null && binary.exists()) {111 return binary;112 }113 ExecutableFinder binaryFinder = new ExecutableFinder();114 if (current.is(UNIX)) {115 String systemFirefox = binaryFinder.find("firefox-bin");116 if (systemFirefox != null) {117 return new File(systemFirefox);118 }119 }120 String systemFirefox = binaryFinder.find("firefox");121 if (systemFirefox != null) {122 return new File(systemFirefox);123 }124 return null;125 }126 private static File findExistingBinary(final ImmutableList<String> paths) {127 for (String path : paths) {128 File file = new File(path);129 if (file.exists()) {130 return file;131 }132 }133 return null;134 }135}...
Source:CoreSelfTest.java
...37 public void detectBrowser() {38 browser = System.getProperty("selenium.browser", "*googlechrome");39 switch (browser) {40 case "*firefox":41 assumeNotNull(new ExecutableFinder().find("geckodriver"));42 break;43 case "*googlechrome":44 assumeNotNull(new ExecutableFinder().find("chromedriver"));45 break;46 default:47 assumeFalse("No known driver able to be found", false);48 }49 }50 @Before51 public void startTestServer() {52 server = new SeleniumAppServer();53 server.start();54 }55 @After56 public void stopTestServer() {57 server.stop();58 }...
Source:ExecutableFinder.java
...16 private final ImmutableSet.Builder<String> pathSegmentBuilder = new ImmutableSet.Builder();17 18 public ExecutableFinder() {}19 20 public String find(String named)21 {22 File file = new File(named);23 if (canExecute(file)) {24 return named;25 }26 27 if (Platform.getCurrent().is(Platform.WINDOWS)) {28 file = new File(named + ".exe");29 if (canExecute(file)) {30 return named + ".exe";31 }32 }33 34 addPathFromEnvironment();...
Source:BaseTest.java
...24 public static void tearDown(){25 webDriver.quit();26 }27 private static boolean executableNotPresentInPath() {28 return new ExecutableFinder().find("chromedriver") == null;29 }30 private static boolean systemPropertyNotSet() {31 return System.getProperty(CHROME_DRIVER_PROPERTY) == null;32 }33 private static void setSystemProperty() {34 System.setProperty(CHROME_DRIVER_PROPERTY, PATH_TO_CHROME_DRIVER);35 }36}...
Source:AbstractChromeTest.java
...12 setSystemProperty();13 }14 }15 private static boolean executableNotPresentInPath() {16 return new ExecutableFinder().find("chromedriver") == null;17 }18 private static boolean systemPropertyNotSet() {19 return System.getProperty(CHROME_DRIVER_PROPERTY) == null;20 }21 private static void setSystemProperty() {22 System.setProperty(CHROME_DRIVER_PROPERTY, PATH_TO_CHROME_DRIVER);23 }24}...
Source:AbstractFirefoxTest.java
...11 setSystemProperty();12 }13 }14 private static boolean executableNotPresentInPath() {15 return new ExecutableFinder().find("geckodriver") == null;16 }17 private static boolean systemPropertyNotSet() {18 return System.getProperty(GECKO_DRIVER_PROPERTY) == null;19 }20 private static void setSystemProperty() {21 System.setProperty(GECKO_DRIVER_PROPERTY, PATH_TO_GECKO_DRIVER);22 }23}...
find
Using AI Code Generation
1import org.openqa.selenium.os.ExecutableFinder;2import java.io.File;3import java.util.List;4import java.util.ArrayList;5public class ExecutableFinderDemo {6 public static void main(String[] args) {7 String path = "C:\\Program Files\\Mozilla Firefox";8 System.setProperty("PATH", path);9 ExecutableFinder finder = new ExecutableFinder();10 List<File> filesToSearch = new ArrayList<File>();11 filesToSearch.add(new File("C:\\Program Files\\Mozilla Firefox\\firefox.exe"));12 filesToSearch.add(new File("C:\\Program Files\\Mozilla Firefox\\notepad.exe"));13 filesToSearch.add(new File("C:\\Program Files\\Mozilla Firefox\\notepad++.exe"));14 String pathOfExecutable = finder.find(filesToSearch);15 System.out.println("Path of executable is: " + pathOfExecutable);16 }17}
find
Using AI Code Generation
1import org.openqa.selenium.chrome.ChromeDriver;2import org.openqa.selenium.os.ExecutableFinder;3import java.io.File;4public class FindChromeDriver {5 public static void main(String[] args) {6 ExecutableFinder finder = new ExecutableFinder();7 File chromeDriver = finder.find("chromedriver");8 System.setProperty("webdriver.chrome.driver", chromeDriver.getAbsolutePath());9 new ChromeDriver();10 }11}
find
Using AI Code Generation
1package org.openqa.selenium.os;2import java.io.BufferedReader;3import java.io.File;4import java.io.IOException;5import java.io.InputStreamReader;6public class ExecutableFinder {7 public String find(String executableName) {8 String[] paths = System.getenv("PATH").split(File.pathSeparator);9 for (String path : paths) {10 File file = new File(path, executableName);11 if (file.isFile() && file.canExecute()) {12 return file.getAbsolutePath();13 }14 }15 return null;16 }17 public static void main(String[] args) throws IOException {18 String executableName = "java";19 String executablePath = new ExecutableFinder().find(executableName);20 System.out.println("Executable path: " + executablePath);21 ProcessBuilder pb = new ProcessBuilder(executablePath, "-version");22 Process process = pb.start();23 BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));24 String line;25 while ((line = br.readLine()) != null) {26 System.out.println(line);27 }28 br.close();29 }30}31Java(TM) SE Runtime Environment (build 1.8.0_161-b12)32Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)
find
Using AI Code Generation
1package com.selenium4beginners.java.findpath;2import org.openqa.selenium.os.ExecutableFinder;3import java.io.File;4{5 public static void main (String[] args) 6 {7 ExecutableFinder finder = new ExecutableFinder();8 File file = finder.find("chromedriver");9 System.out.println(file.getAbsolutePath());10 }11}
find
Using AI Code Generation
1package org.openqa.selenium.example;2import org.openqa.selenium.os.ExecutableFinder;3import org.openqa.selenium.server.RemoteControlConfiguration;4import org.openqa.selenium.server.SeleniumServer;5public class FindFirefox {6 public static void main(String[] args) throws Exception {7 RemoteControlConfiguration rcConfig = new RemoteControlConfiguration();8 SeleniumServer server = new SeleniumServer(rcConfig);9 String firefoxPath = new ExecutableFinder().find("firefox");10 rcConfig.setFirefoxProfileTemplate(firefoxPath);11 server.start();12 }13}
LambdaTest’s Selenium 4 tutorial is covering every aspects of Selenium 4 testing with examples and best practices. Here you will learn basics, such as how to upgrade from Selenium 3 to Selenium 4, to some advanced concepts, such as Relative locators and Selenium Grid 4 for Distributed testing. Also will learn new features of Selenium 4, such as capturing screenshots of specific elements, opening a new tab or window on the browser, and new protocol adoptions.
Upgrading From Selenium 3 To Selenium 4?: In this chapter, learn in detail how to update Selenium 3 to Selenium 4 for Java binding. Also, learn how to upgrade while using different build tools such as Maven or Gradle and get comprehensive guidance for upgrading Selenium.
What’s New In Selenium 4 & What’s Being Deprecated? : Get all information about new implementations in Selenium 4, such as W3S protocol adaption, Optimized Selenium Grid, and Enhanced Selenium IDE. Also, learn what is deprecated for Selenium 4, such as DesiredCapabilites and FindsBy methods, etc.
Selenium 4 With Python: Selenium supports all major languages, such as Python, C#, Ruby, and JavaScript. In this chapter, learn how to install Selenium 4 for Python and the features of Python in Selenium 4, such as Relative locators, Browser manipulation, and Chrom DevTool protocol.
Selenium 4 Is Now W3C Compliant: JSON Wireframe protocol is retiring from Selenium 4, and they are adopting W3C protocol to learn in detail about the advantages and impact of these changes.
How To Use Selenium 4 Relative Locator? : Selenium 4 came with new features such as Relative Locators that allow constructing locators with reference and easily located constructors nearby. Get to know its different use cases with examples.
Selenium Grid 4 Tutorial For Distributed Testing: Selenium Grid 4 allows you to perform tests over different browsers, OS, and device combinations. It also enables parallel execution browser testing, reads up on various features of Selenium Grid 4 and how to download it, and runs a test on Selenium Grid 4 with best practices.
Selenium Video Tutorials: Binge on video tutorials on Selenium by industry experts to get step-by-step direction from automating basic to complex test scenarios with Selenium.
LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.
Get 100 minutes of automation test minutes FREE!!