How to use getCurrent method of org.openqa.selenium.Enum Platform class

Best Selenium code snippet using org.openqa.selenium.Enum Platform.getCurrent

Source:FirefoxBinary.java Github

copy

Full Screen

...96 executable = platformBinary;97 return;98 }99 throw new WebDriverException("Cannot find firefox binary in PATH. " +100 "Make sure firefox is installed. OS appears to be: " + Platform.getCurrent());101 }102 public FirefoxBinary(Channel channel) {103 Executable systemBinary = locateFirefoxBinaryFromSystemProperty();104 if (systemBinary != null) {105 if (systemBinary.getChannel() == channel) {106 executable = systemBinary;107 return;108 } else {109 throw new WebDriverException(110 "Firefox executable specified by system property " + FirefoxDriver.SystemProperty.BROWSER_BINARY +111 " does not belong to channel '" + channel + "', it appears to be '" + systemBinary.getChannel() + "'");112 }113 }114 executable = locateFirefoxBinariesFromPlatform()115 .filter(e -> e.getChannel() == channel)116 .findFirst().orElseThrow(() -> new WebDriverException(117 String.format("Cannot find firefox binary for channel '%s' in PATH", channel)));118 }119 public FirefoxBinary(File pathToFirefoxBinary) {120 executable = new Executable(pathToFirefoxBinary);121 }122 /**123 * deprecated Use {@link DriverService.Builder#withEnvironment(Map)} instead:124 * <p>125 * new FirefoxDriver(126 * new GeckoDriverService.Builder()127 * .usingDriverExecutable(new File("path/to/geckodriver.exe"))128 * .usingFirefoxBinary(new FirefoxBinary(new File("path/to/firefox.exe")))129 * .withEnvironment(ImmutableMap.of("DISPLAY", "0:0"))130 * .build());131 */132 @Deprecated133 public void setEnvironmentProperty(String propertyName, String value) {134 if (propertyName == null || value == null) {135 throw new WebDriverException(136 String.format("You must set both the property name and value: %s, %s", propertyName,137 value));138 }139 extraEnv.put(propertyName, value);140 }141 public void addCommandLineOptions(String... options) {142 Collections.addAll(extraOptions, options);143 }144 void amendOptions(FirefoxOptions options) {145 options.addArguments(extraOptions);146 }147 protected boolean isOnLinux() {148 return Platform.getCurrent().is(Platform.LINUX);149 }150 public void startProfile(FirefoxProfile profile, File profileDir, String... commandLineFlags)151 throws IOException {152 String profileAbsPath = profileDir.getAbsolutePath();153 setEnvironmentProperty("XRE_PROFILE_PATH", profileAbsPath);154 setEnvironmentProperty("MOZ_NO_REMOTE", "1");155 setEnvironmentProperty("MOZ_CRASHREPORTER_DISABLE", "1"); // Disable Breakpad156 setEnvironmentProperty("NO_EM_RESTART", "1"); // Prevent the binary from detaching from the157 // console158 if (isOnLinux() && profile.shouldLoadNoFocusLib()) {159 modifyLinkLibraryPath(profileDir);160 }161 List<String> cmdArray = new ArrayList<>();162 cmdArray.addAll(extraOptions);163 Collections.addAll(cmdArray, commandLineFlags);164 CommandLine command = new CommandLine(getPath(), Iterables.toArray(cmdArray, String.class));165 command.setEnvironmentVariables(getExtraEnv());166 command.updateDynamicLibraryPath(getExtraEnv().get(CommandLine.getLibraryPathPropertyName()));167 // On Snow Leopard, beware of problems the sqlite library168 if (! (Platform.getCurrent().is(Platform.MAC) && Platform.getCurrent().getMinorVersion() > 5)) {169 String firefoxLibraryPath = System.getProperty(170 FirefoxDriver.SystemProperty.BROWSER_LIBRARY_PATH,171 getFile().getAbsoluteFile().getParentFile().getAbsolutePath());172 command.updateDynamicLibraryPath(firefoxLibraryPath);173 }174 if (stream == null) {175 stream = getDefaultOutputStream();176 }177 command.copyOutputTo(stream);178 startFirefoxProcess(command);179 }180 protected void startFirefoxProcess(CommandLine command) {181 process = command;182 command.executeAsync();183 }184 protected File getFile() {185 return executable.getFile();186 }187 protected String getPath() {188 return executable.getPath();189 }190 /**191 * @deprecated No replacement. Environment should be configured in {@link DriverService} instance.192 */193 @Deprecated194 public Map<String, String> getExtraEnv() {195 return Collections.unmodifiableMap(extraEnv);196 }197 protected void modifyLinkLibraryPath(File profileDir) {198 // Extract x_ignore_nofocus.so from x86, amd64 directories inside199 // the jar into a real place in the filesystem and change LD_LIBRARY_PATH200 // to reflect that.201 String existingLdLibPath = System.getenv("LD_LIBRARY_PATH");202 // The returned new ld lib path is terminated with ':'203 String newLdLibPath =204 extractAndCheck(profileDir, NO_FOCUS_LIBRARY_NAME, PATH_PREFIX + "x86", PATH_PREFIX +205 "amd64");206 if (existingLdLibPath != null && !existingLdLibPath.equals("")) {207 newLdLibPath += existingLdLibPath;208 }209 setEnvironmentProperty("LD_LIBRARY_PATH", newLdLibPath);210 // Set LD_PRELOAD to x_ignore_nofocus.so - this will be taken automagically211 // from the LD_LIBRARY_PATH212 setEnvironmentProperty("LD_PRELOAD", NO_FOCUS_LIBRARY_NAME);213 }214 protected String extractAndCheck(File profileDir, String noFocusSoName,215 String jarPath32Bit, String jarPath64Bit) {216 // 1. Extract x86/x_ignore_nofocus.so to profile.getLibsDir32bit217 // 2. Extract amd64/x_ignore_nofocus.so to profile.getLibsDir64bit218 // 3. Create a new LD_LIB_PATH string to contain:219 // profile.getLibsDir32bit + ":" + profile.getLibsDir64bit220 Set<String> pathsSet = new HashSet<>();221 pathsSet.add(jarPath32Bit);222 pathsSet.add(jarPath64Bit);223 StringBuilder builtPath = new StringBuilder();224 for (String path : pathsSet) {225 try {226 FileHandler.copyResource(profileDir, getClass(), path + File.separator + noFocusSoName);227 } catch (IOException e) {228 if (Boolean.getBoolean("webdriver.development")) {229 System.err.println(230 "Exception unpacking required library, but in development mode. Continuing");231 } else {232 throw new WebDriverException(e);233 }234 } // End catch.235 String outSoPath = profileDir.getAbsolutePath() + File.separator + path;236 File file = new File(outSoPath, noFocusSoName);237 if (!file.exists()) {238 throw new WebDriverException("Could not locate " + path + ": "239 + "native events will not work.");240 }241 builtPath.append(outSoPath).append(":");242 }243 return builtPath.toString();244 }245 /**246 * Waits for the process to execute, returning the command output taken from the profile's247 * execution.248 */249 public void waitFor() {250 process.waitFor();251 }252 /**253 * Waits for the process to execute, returning the command output taken from the profile's254 * execution.255 *256 * @param timeout the maximum time to wait in milliseconds257 */258 public void waitFor(long timeout) {259 process.waitFor(timeout);260 }261 /**262 * Gets all console output of the binary. Output retrieval is non-destructive and non-blocking.263 *264 * @return the console output of the executed binary.265 */266 public String getConsoleOutput() {267 if (process == null) {268 return null;269 }270 return process.getStdOut();271 }272 public long getTimeout() {273 return timeout;274 }275 public void setTimeout(long timeout) {276 this.timeout = timeout;277 }278 @Override279 public String toString() {280 return "FirefoxBinary(" + executable.getPath() + ")";281 }282 public String toJson() {283 return executable.getPath();284 }285 public void setOutputWatcher(OutputStream stream) {286 this.stream = stream;287 }288 public void quit() {289 if (process != null) {290 process.destroy();291 }292 }293 private OutputStream getDefaultOutputStream() throws FileNotFoundException {294 String firefoxLogFile = System.getProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE);295 if (firefoxLogFile != null) {296 if ("/dev/stdout".equals(firefoxLogFile)) {297 return System.out;298 }299 return new FileOutputStream(firefoxLogFile);300 }301 return null;302 }303 /**304 * Locates the firefox binary from a system property. Will throw an exception if the binary cannot305 * be found.306 */307 private static Executable locateFirefoxBinaryFromSystemProperty() {308 String binaryName = System.getProperty(FirefoxDriver.SystemProperty.BROWSER_BINARY);309 if (binaryName == null)310 return null;311 File binary = new File(binaryName);312 if (binary.exists() && !binary.isDirectory())313 return new Executable(binary);314 Platform current = Platform.getCurrent();315 if (current.is(WINDOWS)) {316 if (!binaryName.endsWith(".exe")) {317 binaryName += ".exe";318 }319 } else if (current.is(MAC)) {320 if (!binaryName.endsWith(".app")) {321 binaryName += ".app";322 }323 binaryName += "/Contents/MacOS/firefox-bin";324 }325 binary = new File(binaryName);326 if (binary.exists())327 return new Executable(binary);328 throw new WebDriverException(329 String.format("'%s' property set, but unable to locate the requested binary: %s",330 FirefoxDriver.SystemProperty.BROWSER_BINARY, binaryName));331 }332 /**333 * Locates the firefox binary by platform.334 */335 private static Stream<Executable> locateFirefoxBinariesFromPlatform() {336 ImmutableList.Builder<Executable> executables = new ImmutableList.Builder<>();337 Platform current = Platform.getCurrent();338 if (current.is(WINDOWS)) {339 executables.addAll(Stream.of(getPathsInProgramFiles("Mozilla Firefox\\firefox.exe"),340 getPathsInProgramFiles("Firefox Developer Edition\\firefox.exe"),341 getPathsInProgramFiles("Nightly\\firefox.exe"))342 .flatMap(List::stream)343 .map(File::new).filter(File::exists)344 .map(Executable::new).collect(toList()));345 } else if (current.is(MAC)) {346 // system347 File binary = new File("/Applications/Firefox.app/Contents/MacOS/firefox-bin");348 if (binary.exists()) {349 executables.add(new Executable(binary));350 }351 // user home...

Full Screen

Full Screen

Source:BaseTest.java Github

copy

Full Screen

...76 // TODO factor this up a step to a runner so that it can be run across many browsers77 private WebDriver createRemoteWebDriver() throws MalformedURLException {78 String server = System.getProperty("webdriver.remote.server");79 String browserRaw = System.getProperty("browser", "chrome");80 String platformRaw = System.getProperty("platform", Platform.getCurrent().name());81 DesiredCapabilities capabilities = new DesiredCapabilities(browserRaw, "",82 Enum.valueOf(Platform.class, platformRaw));83 return new RemoteWebDriver(new URL(server), capabilities);84 }85}...

Full Screen

Full Screen

Source:BrowserSelection.java Github

copy

Full Screen

...79 return driver;80 }81 82 public static boolean MacPlatform() {83 Platform current = Platform.getCurrent();84 return Platform.MAC.is(current);85 }86} ...

Full Screen

Full Screen

getCurrent

Using AI Code Generation

copy

Full Screen

1package com.automationrhapsody.selenium;2import org.openqa.selenium.Platform;3import org.openqa.selenium.remote.DesiredCapabilities;4public class EnumExample {5 public static void main(String[] args) {6 DesiredCapabilities capabilities = new DesiredCapabilities();7 capabilities.setPlatform(Platform.getCurrent());8 System.out.println(capabilities.getPlatform());9 }10}

Full Screen

Full Screen

getCurrent

Using AI Code Generation

copy

Full Screen

1package org.openqa.selenium;2public class PlatformExample {3 public static void main(String[] args) {4 Platform currentPlatform = Platform.getCurrent();5 System.out.println("Current Platform: " + currentPlatform);6 }7}8package org.openqa.selenium;9public class PlatformExample {10 public static void main(String[] args) {11 Platform currentPlatform = Platform.getCurrent();12 Platform platform = Platform.get(currentPlatform.toString());13 System.out.println("Current Platform: " + platform);14 }15}16package org.openqa.selenium;17public class PlatformExample {18 public static void main(String[] args) {19 Platform currentPlatform = Platform.getCurrent();20 Platform platform = Platform.get(currentPlatform.toString());21 System.out.println("Current Platform: " + platform);22 }23}24package org.openqa.selenium;25public class PlatformExample {26 public static void main(String[] args) {27 Platform currentPlatform = Platform.getCurrent();28 Platform platform = Platform.get(currentPlatform.toString());29 System.out.println("Current Platform: " + platform);30 }31}32package org.openqa.selenium;33public class PlatformExample {34 public static void main(String[] args) {35 Platform currentPlatform = Platform.getCurrent();36 Platform platform = Platform.get(currentPlatform.toString());37 System.out.println("Current Platform: " + platform);38 }39}

Full Screen

Full Screen

getCurrent

Using AI Code Generation

copy

Full Screen

1package com.automationrhapsody.selenium;2import org.openqa.selenium.Platform;3import org.openqa.selenium.remote.DesiredCapabilities;4public class PlatformGetCurrent {5 public static void main(String[] args) {6 Platform currentPlatform = Platform.getCurrent();7 System.out.println("Current platform: " + currentPlatform);8 DesiredCapabilities caps = DesiredCapabilities.chrome();9 Platform platform = caps.getPlatform();10 System.out.println("Current platform: " + platform);11 }12}13package com.automationrhapsody.selenium;14import org.openqa.selenium.Platform;15public class PlatformIs {16 public static void main(String[] args) {17 Platform currentPlatform = Platform.getCurrent();18 System.out.println("Is current platform Linux? " + currentPlatform.is(Platform.LINUX));19 System.out.println("Is current platform Mac? " + currentPlatform.is(Platform.MAC));20 System.out.println("Is current platform Vista? " + currentPlatform.is(Platform.VISTA));21 System.out.println("Is current platform XP? " + currentPlatform.is(Platform.XP));22 System.out.println("Is current platform Windows? " + currentPlatform.is(Platform.WINDOWS));23 }24}25In the above example, we first get the current platform using the Platform.getCurrent()

Full Screen

Full Screen

getCurrent

Using AI Code Generation

copy

Full Screen

1package org.kodejava.example.selenium;2import org.openqa.selenium.Platform;3public class GetCurrentPlatform {4 public static void main(String[] args) {5 Platform platform = Platform.getCurrent();6 System.out.println("Current Platform = " + platform);7 }8}9Share on Skype (Opens in new window)

Full Screen

Full Screen

Selenium 4 Tutorial:

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.

Chapters:

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. 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.

Selenium 101 certifications:

LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.

Run Selenium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful