How to use CommandLine class of org.openqa.selenium.os package

Best Selenium code snippet using org.openqa.selenium.os.CommandLine

Source:CommandLineTest.java Github

copy

Full Screen

...23import static org.mockito.Mockito.verify;24import static org.mockito.Mockito.verifyNoInteractions;25import static org.mockito.Mockito.verifyNoMoreInteractions;26import static org.openqa.selenium.Platform.WINDOWS;27import static org.openqa.selenium.os.CommandLine.getLibraryPathPropertyName;28import static org.openqa.selenium.testing.TestUtilities.isOnTravis;29import org.junit.Assume;30import org.junit.Test;31import org.openqa.selenium.Platform;32import java.io.ByteArrayOutputStream;33import java.lang.reflect.Field;34import java.util.HashMap;35import java.util.Map;36public class CommandLineTest {37 // ping can be found on every platform we support.38 private static String testExecutable = "java/client/test/org/openqa/selenium/os/echo";39 private CommandLine commandLine = new CommandLine(testExecutable);40 private OsProcess process = spyProcess(commandLine);41 @Test42 public void testSetEnvironmentVariableDelegatesToProcess() {43 String key = "foo";44 String value = "bar";45 commandLine.setEnvironmentVariable(key, value);46 verify(process).setEnvironmentVariable(key, value);47 verifyNoMoreInteractions(process);48 }49 @Test50 public void testSetEnvironmentVariablesDelegatesToProcess() {51 Map<String, String> env = new HashMap<>();52 env.put("k1", "v1");53 env.put("k2", "v2");54 commandLine.setEnvironmentVariables(env);55 verify(process).setEnvironmentVariable("k1", "v1");56 verify(process).setEnvironmentVariable("k2", "v2");57 verifyNoMoreInteractions(process);58 }59 @Test60 public void testSetDynamicLibraryPathWithNullValueIgnores() {61 commandLine.setDynamicLibraryPath(null);62 verifyNoInteractions(process);63 }64 @Test65 public void testSetDynamicLibraryPathWithNonNullValueSets() {66 String value = "Bar";67 commandLine.setDynamicLibraryPath(value);68 verify(process).setEnvironmentVariable(getLibraryPathPropertyName(), value);69 verifyNoMoreInteractions(process);70 }71 @Test72 public void executeWaitsForProcessFinish() throws InterruptedException {73 commandLine.execute();74 verify(process).executeAsync();75 verify(process).waitFor();76 verifyNoMoreInteractions(process);77 }78 @Test79 public void testDestroy() {80 commandLine.executeAsync();81 verify(process).executeAsync();82 assertThat(commandLine.isRunning()).isTrue();83 verify(process).isRunning();84 commandLine.destroy();85 verify(process).destroy();86 assertThat(commandLine.isRunning()).isFalse();87 verify(process, atLeastOnce()).isRunning();88 }89 @Test90 public void canHandleOutput() {91 CommandLine commandLine = new CommandLine(testExecutable, "ping");92 commandLine.execute();93 assertThat(commandLine.getStdOut()).isNotEmpty().contains("ping");94 }95 @Test96 public void canCopyOutput() {97 CommandLine commandLine = new CommandLine(testExecutable, "I", "love", "cheese");98 ByteArrayOutputStream buffer = new ByteArrayOutputStream();99 commandLine.copyOutputTo(buffer);100 commandLine.execute();101 assertThat(buffer.toByteArray()).isNotEmpty();102 assertThat(commandLine.getStdOut()).isEqualTo(buffer.toString());103 }104 @Test105 public void canDetectSuccess() {106 assumeThat(isOnTravis()).as("Operation not permitted on travis").isFalse();107 CommandLine commandLine = new CommandLine(108 testExecutable, (Platform.getCurrent().is(WINDOWS) ? "-n" : "-c"), "3", "localhost");109 commandLine.execute();110 assertThat(commandLine.getExitCode()).isEqualTo(0);111 assertThat(commandLine.isSuccessful()).isTrue();112 }113 @Test114 public void canDetectFailure() {115 commandLine.execute();116 assertThat(commandLine.getExitCode()).isNotEqualTo(0);117 assertThat(commandLine.isSuccessful()).isFalse();118 }119 @Test120 public void canUpdateLibraryPath() {121 Assume.assumeTrue(Platform.getCurrent().is(WINDOWS));122 commandLine.updateDynamicLibraryPath("C:\\My\\Tools");123 verify(process).setEnvironmentVariable(124 getLibraryPathPropertyName(), String.format("%s;%s", getenv("PATH"), "C:\\My\\Tools"));125 }126 private OsProcess spyProcess(CommandLine commandLine) {127 try {128 Field processField = CommandLine.class.getDeclaredField("process");129 processField.setAccessible(true);130 OsProcess process = (OsProcess) processField.get(commandLine);131 OsProcess spyProcess = spy(process);132 processField.set(commandLine, spyProcess);133 return spyProcess;134 } catch (NoSuchFieldException | IllegalAccessException e) {135 throw new RuntimeException(e);136 }137 }138}...

Full Screen

Full Screen

Source:Firefox2Locator.java Github

copy

Full Screen

1package org.openqa.selenium.browserlaunchers.locators;2import java.io.File;3import org.openqa.selenium.Platform;4import org.openqa.selenium.os.WindowsUtils;5import org.openqa.selenium.os.CommandLine;6/**7 * Discovers a valid Firefox 2.x installation on local system.8 */9public class Firefox2Locator extends FirefoxLocator {10 private static final String[] USUAL_UNIX_LAUNCHER_LOCATIONS = {11 "/Applications/Minefield.app/Contents/MacOS",12 "/Applications/Firefox-2.app/Contents/MacOS",13 "/Applications/Firefox.app/Contents/MacOS",14 "/usr/lib/firefox", /* Ubuntu 7.x default location */15 };16 private static final String[] USUAL_WINDOWS_LAUNCHER_LOCATIONS = {17 WindowsUtils.getProgramFilesPath() + "\\Mozilla Firefox",18 WindowsUtils.getProgramFilesPath() + "\\Firefox",19 WindowsUtils.getProgramFilesPath() + "\\Firefox-2",20 };21 protected String browserName() {22 return "Firefox 2";23 }24 protected String seleniumBrowserName() {25 return "*firefox2";26 }27 protected String[] standardlauncherFilenames() {28 if (WindowsUtils.thisIsWindows()) {29 return new String[]{"firefox.exe"};30 } else {31 return new String[]{"firefox-bin"};32 }33 }34 protected String[] usualLauncherLocations() {35 return WindowsUtils.thisIsWindows() ? USUAL_WINDOWS_LAUNCHER_LOCATIONS : USUAL_UNIX_LAUNCHER_LOCATIONS;36 }37 protected boolean runningOnWindows() {38 return Platform.getCurrent().is(Platform.WINDOWS);39 }40 @Override41 public String computeLibraryPath(File launcherPath) {42 if (runningOnWindows()) {43 return "";44 }45 StringBuilder libraryPath = new StringBuilder();46 String libraryPropertyName = CommandLine.getLibraryPathPropertyName();47 String existingLibraryPath = System.getenv(libraryPropertyName);48 if (Platform.getCurrent().is(Platform.MAC) && Platform.getCurrent().getMinorVersion() > 5) {49 libraryPath.append(existingLibraryPath);50 } else {51 libraryPath.append(launcherPath.getParent()).append(File.pathSeparator).append(libraryPath);52 }53 return libraryPath.toString();54 }55}...

Full Screen

Full Screen

Source:TestNGBase.java Github

copy

Full Screen

1package com.epam.jdi.uitests.win.testing.testRunner;2import com.epam.jdi.uitests.win.settings.WinSettings;3import com.epam.jdi.uitests.win.winnium.driver.WiniumDriverFactory;4import org.openqa.selenium.os.CommandLine;5import org.openqa.selenium.os.WindowsUtils;6import org.testng.annotations.AfterSuite;7import org.testng.annotations.BeforeSuite;8import static com.epam.jdi.tools.LinqUtils.first;9import static com.epam.jdi.tools.LinqUtils.where;10import static com.epam.jdi.tools.TryCatchUtil.tryGetResult;11import static com.epam.jdi.uitests.core.settings.JDISettings.logger;12public class TestNGBase {13 @BeforeSuite(alwaysRun = true)14 public static void jdiSetUp() throws Exception {15 WinSettings.initFromProperties();16 logger.info("Init test run");17 }18 @AfterSuite19 public static void jdiTearDown() {20 if (WinSettings.driverFactory instanceof WiniumDriverFactory) {21 WiniumDriverFactory winiumDriverFactory = (WiniumDriverFactory) WinSettings.driverFactory;22 winiumDriverFactory.getDriver("winniumdesctop").close(); //Can be useful only for *.exe applications23 Process process = winiumDriverFactory.getStartedProcess();24 if (process != null)25 process.destroy();26// killAllRunDrivers();27 }28 }29 public static void killAllRunDrivers() {30 try {31 String pid = getPid();32 while (pid != null) {33 killPID(pid);34 pid = getPid();35 }36 } catch (Exception ignore) {37 // Ignore in case of not windows Operation System or any other errors38 }39 }40 private static String getPid() {41 return first(where(tryGetResult(WindowsUtils::procMap), el -> el.getKey() != null42 && (el.getKey().contains("Winium.Desktop.Driver")43 || el.getKey().contains("WindowsPhoneDriver.OuterDriver")44 || el.getKey().contains("Winium.StoreApps.Driver"))));45 }46 private static void killPID(String processID) {47 new CommandLine("taskkill", "/f", "/t", "/pid", processID).execute();48 }49}...

Full Screen

Full Screen

Source:WebDriverUtils.java Github

copy

Full Screen

1package com.ggasoftware.uitest.control.base.usefulUtils;2import org.openqa.selenium.os.CommandLine;3import org.openqa.selenium.os.WindowsUtils;4import static com.ggasoftware.uitest.control.base.usefulUtils.TryCatchUtil.tryGetResult;5import static com.ggasoftware.uitest.utils.LinqUtils.first;6import static com.ggasoftware.uitest.utils.LinqUtils.where;7/**8 * Created by 12345 on 26.01.2015.9 */10public class WebDriverUtils {11 public static void killAllRunWebDrivers() {12 try {13 String pid = getPid();14 while (pid != null) {15 killPID(pid);16 pid = getPid();17 }18 } catch (Exception | AssertionError ignore) {19 }20 }21 private static String getPid() {22 return first(where(tryGetResult(WindowsUtils::procMap), el -> el.getKey() != null23 && (el.getKey().contains("firefox") && el.getKey().contains("-foreground"))24 | el.getKey().contains("chromedriver")25 | el.getKey().contains("IEDriverServer")));26 }27 private static void killPID(String processID) {28 executeCommand("taskkill", "/f", "/t", "/pid", processID);29 }30 private static void executeCommand(String commandName, String... args) {31 CommandLine cmd = new CommandLine(commandName, args);32 cmd.execute();33 }34}...

Full Screen

Full Screen

Source:Utils.java Github

copy

Full Screen

1package org.mytests.uiobjects.example.enums;2import org.openqa.selenium.os.CommandLine;3import org.openqa.selenium.os.WindowsUtils;4import static com.epam.commons.LinqUtils.first;5import static com.epam.commons.LinqUtils.where;6import static com.epam.commons.TryCatchUtil.tryGetResult;7/**8 * Created by Roman_Iovlev on 1/10/2018.9 */10public class Utils {11 public static void killAllRunDrivers() {12 try {13 String pid = getPid();14 while (pid != null) {15 killPID(pid);16 pid = getPid();17 }18 } catch (Exception ignore) {19 // Ignore in case of not windows Operation System or any other errors20 }21 }22 private static String getPid() {23 return first(where(tryGetResult(WindowsUtils::procMap), el -> el.getKey() != null24 && (el.getKey().contains("Winium.Desktop.Driver")25 || el.getKey().contains("WindowsPhoneDriver.OuterDriver")26 || el.getKey().contains("Winium.StoreApps.Driver"))));27 }28 private static void killPID(String processID) {29 new CommandLine("taskkill", "/f", "/t", "/pid", processID).execute();30 }31}...

Full Screen

Full Screen

CommandLine

Using AI Code Generation

copy

Full Screen

1public class ChromeDriverPath {2 public static void main(String[] args) {3 System.out.println(new CommandLine().findExecutable("chromedriver.exe"));4 }5}6C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe7public class ChromeDriverPath {8 public static void main(String[] args) {9 System.setProperty("webdriver.chrome.driver", new CommandLine().findExecutable("chromedriver.exe"));10 WebDriver driver = new ChromeDriver();11 }12}

Full Screen

Full Screen

CommandLine

Using AI Code Generation

copy

Full Screen

1public static void main(String[] args) throws IOException {2 String cmd = "ls -l";3 CommandLine commandLine = new CommandLine(cmd);4 commandLine.execute();5 String output = commandLine.getStdOut();6 System.out.println(output);7}8import org.openqa.selenium.os.CommandLine;9import java.io.IOException;10public static void main(String[] args) throws IOException {11 String cmd = "ls -l";12 CommandLine commandLine = new CommandLine(cmd);13 commandLine.execute();14 String output = commandLine.getStdOut();15 System.out.println(output);16}

Full Screen

Full Screen

CommandLine

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.By;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.WebElement;4import org.openqa.selenium.firefox.FirefoxDriver;5import org.openqa.selenium.os.CommandLine;6public class SeleniumWebDriver {7public static void main(String[] args) throws Exception {8CommandLine command = new CommandLine("C:\\Program Files\\Mozilla Firefox\\firefox.exe");9command.executeAsync();10WebDriver driver = new FirefoxDriver();11driver.manage().window().maximize();12WebElement searchTextBox = driver.findElement(By.name("q"));13searchTextBox.sendKeys("Selenium WebDriver");14WebElement searchButton = driver.findElement(By.name("btnG"));15searchButton.click();16driver.close();17command.destroy();18}19}

Full Screen

Full Screen
copy
1a.f(b); <-> b.f(a);2
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.

...Most popular Stackoverflow questions on CommandLine

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful