简体   繁体   中英

What is the significance of System.setProperty() while working with Selenium

In selenium, why do we add System.setProperty("webdriver.chrome.driver", "E://chromedriver.exe"); within static{} block ?

public class Demo{
static{
System.setProperty("webdriver.chrome.driver", "E://chromedriver.exe");
}
  public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();              
        driver.get("http://www.google.com");

    }
}

While working with Selenium-Java client, the Java platform itself uses the Properties object to maintain its own configuration. The System class maintains the Properties object that describes the configuration of the current working environment. System properties include information about the current user , the current version of the Java runtime , the character used to separate components of a file path name , etc.

The following table describes some of the most important system properties:

Java_系统属性

Security consideration : Access to system properties can be restricted by the Security Manager . This is most often an issue in applets, which are prevented from reading some system properties, and from writing any system properties. For more on accessing system properties in applets, refer to System Properties in the Doing More With Java Rich Internet Applications lesson.


Reading System Properties

The System class has two methods used to read system properties: getProperty and getProperties .

The System class has two different versions of getProperty . Both retrieve the value of the property named in the argument list. The simpler of the two getProperty methods takes a single argument, a property key For example, to get the value of path.separator , use the following statement:

System.getProperty("path.separator");

The getProperty method returns a string containing the value of the property. If the property does not exist, this version of getProperty returns null.

The other version of getProperty requires two String arguments: the first argument is the key to look up and the second argument is a default value to return if the key cannot be found or if it has no value. For example, the following invocation of getProperty looks up the System property called subliminal.message . This is not a valid system property, so instead of returning null, this method returns the default value provided as a second argument: "Selenium WebDriver!"

System.getProperty("subliminal.message", "Selenium WebDriver!");

The last method provided by the System class to access property values is the getProperties method, which returns a Properties object. This object contains a complete set of system property definitions.

A Java program to extract the System Properties :

  • Code Block:

     package Java_Experiments; public class system_getProperty { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\\\\Utility\\\\BrowserDrivers\\\\chromedriver.exe"); System.out.println(System.getProperty("webdriver.chrome.driver")); System.out.println(System.getProperty("subliminal.message", "Selenium WebDriver!")); System.out.println("Java Runtime Environment version: "+System.getProperty("java.version")); System.out.println("Java Runtime Environment vendor: "+System.getProperty("java.vendor")); System.out.println("Java vendor URL: "+System.getProperty("java.vendor.url")); System.out.println("Java installation directory: "+System.getProperty("java.home")); System.out.println("Java Virtual Machine specification version: "+System.getProperty("java.vm.specification.version")); System.out.println("Java Virtual Machine specification vendor: "+System.getProperty("java.vm.specification.vendor")); System.out.println("Java Virtual Machine specification name: "+System.getProperty("java.vm.specification.name")); System.out.println("Java Virtual Machine implementation version: "+System.getProperty("java.vm.version")); System.out.println("Java Virtual Machine implementation vendor: "+System.getProperty("java.vm.vendor")); System.out.println("Java Virtual Machine implementation name: "+System.getProperty("java.vm.name")); System.out.println("Java Runtime Environment specification version: "+System.getProperty("java.specification.version")); System.out.println("Java Runtime Environment specification vendor: "+System.getProperty("java.specification.vendor")); System.out.println("Java Runtime Environment specification name: "+System.getProperty("java.specification.name")); System.out.println("Java class format version number: "+System.getProperty("java.class.version")); System.out.println("Java class path: "+System.getProperty("java.class.path")); System.out.println("List of paths to search when loading libraries: "+System.getProperty("java.library.path")); System.out.println("Default temp file path: "+System.getProperty("java.io.tmpdir")); System.out.println("Name of JIT compiler to use: "+System.getProperty("java.compiler")); System.out.println("Path of extension directory or directories: "+System.getProperty("java.ext.dirs")); System.out.println("Operating system name: "+System.getProperty("os.name")); System.out.println("Operating system architecture: "+System.getProperty("os.arch")); System.out.println("Operating system version: "+System.getProperty("os.version")); System.out.println("File separator: "+System.getProperty("file.separator")); System.out.println("Path separator: "+System.getProperty("path.separator")); System.out.println("Line separator: "+System.getProperty("line.separator")); System.out.println("User's account name: "+System.getProperty("user.name")); System.out.println("User's home directory: "+System.getProperty("user.home")); System.out.println("User's current working directory: "+System.getProperty("user.dir")); } }
  • Console Output:

     C:\\Utility\\BrowserDrivers\\chromedriver.exe Selenium WebDriver! Java Runtime Environment version: 1.8.0_172 Java Runtime Environment vendor: Oracle Corporation Java vendor URL: http://java.oracle.com/ Java installation directory: C:\\Program Files\\Java\\jre1.8.0_172 Java Virtual Machine specification version: 1.8 Java Virtual Machine specification vendor: Oracle Corporation Java Virtual Machine specification name: Java Virtual Machine Specification Java Virtual Machine implementation version: 25.172-b11 Java Virtual Machine implementation vendor: Oracle Corporation Java Virtual Machine implementation name: Java HotSpot(TM) 64-Bit Server VM Java Runtime Environment specification version: 1.8 Java Runtime Environment specification vendor: Oracle Corporation Java Runtime Environment specification name: Java Platform API Specification Java class format version number: 52.0 Java class path: C:\\Users\\AtechM_03\\LearnAutmation\\learn-automation\\bin;C:\\Utility\\log4j-1.2.15.jar\\log4j-1.2.15.jar;C:\\Utility\\Sikuli\\sikulixapi.jar;C:\\Utility\\poi-bin-3.15-20160924\\poi-3.15\\poi-3.15.jar;C:\\Utility\\poi-bin-3.15-20160924\\poi-3.15\\poi-examples-3.15.jar;C:\\Utility\\poi-bin-3.15-20160924\\poi-3.15\\poi-excelant-3.15.jar;C:\\Utility\\poi-bin-3.15-20160924\\poi-3.15\\poi-ooxml-3.15.jar;C:\\Utility\\poi-bin-3.15-20160924\\poi-3.15\\poi-ooxml-schemas-3.15.jar;C:\\Utility\\poi-bin-3.15-20160924\\poi-3.15\\poi-scratchpad-3.15.jar;C:\\Utility\\poi-bin-3.15-20160924\\poi-3.15\\ooxml-lib\\curvesapi-1.04.jar;C:\\Utility\\poi-bin-3.15-20160924\\poi-3.15\\ooxml-lib\\xmlbeans-2.6.0.jar;C:\\Utility\\poi-bin-3.15-20160924\\poi-3.15\\lib\\commons-codec-1.10.jar;C:\\Utility\\poi-bin-3.15-20160924\\poi-3.15\\lib\\commons-collections4-4.1.jar;C:\\Utility\\poi-bin-3.15-20160924\\poi-3.15\\lib\\commons-logging-1.2.jar;C:\\Utility\\poi-bin-3.15-20160924\\poi-3.15\\lib\\junit-4.12.jar;C:\\Utility\\poi-bin-3.15-20160924\\poi-3.15\\lib\\log4j-1.2.17.jar;C:\\Utility\\Zip4j_1.3.2\\zip4j_1.3.2.jar;C:\\Utility\\PhantomjsDriver1.1.0\\phantomjsdriver-1.4.4.jar;C:\\Utility\\tess4j_jar_files\\commons-beanutils-1.9.2.jar;C:\\Utility\\tess4j_jar_files\\commons-collections-3.2.1.jar;C:\\Utility\\tess4j_jar_files\\commons-io-2.6.jar;C:\\Utility\\tess4j_jar_files\\commons-logging-1.2.jar;C:\\Utility\\tess4j_jar_files\\fontbox-2.0.9.jar;C:\\Utility\\tess4j_jar_files\\ghost4j-1.0.1.jar;C:\\Utility\\tess4j_jar_files\\itext-2.1.7.jar;C:\\Utility\\tess4j_jar_files\\jai-imageio-core-1.4.0.jar;C:\\Utility\\tess4j_jar_files\\jbig2-imageio-3.0.0.jar;C:\\Utility\\tess4j_jar_files\\jboss-logging-3.1.4.GA.jar;C:\\Utility\\tess4j_jar_files\\jboss-vfs-3.2.12.Final.jar;C:\\Utility\\tess4j_jar_files\\jcl-over-slf4j-1.7.25.jar;C:\\Utility\\tess4j_jar_files\\jna-4.1.0.jar;C:\\Utility\\tess4j_jar_files\\jul-to-slf4j-1.7.25.jar;C:\\Utility\\tess4j_jar_files\\lept4j-1.9.4.jar;C:\\Utility\\tess4j_jar_files\\log4j-1.2.17.jar;C:\\Utility\\tess4j_jar_files\\log4j-over-slf4j-1.7.25.jar;C:\\Utility\\tess4j_jar_files\\logback-classic-1.2.3.jar;C:\\Utility\\tess4j_jar_files\\logback-core-1.2.3.jar;C:\\Utility\\tess4j_jar_files\\pdfbox-2.0.9.jar;C:\\Utility\\tess4j_jar_files\\pdfbox-debugger-2.0.9.jar;C:\\Utility\\tess4j_jar_files\\pdfbox-tools-2.0.9.jar;C:\\Utility\\tess4j_jar_files\\slf4j-api-1.7.25.jar;C:\\Utility\\tess4j_jar_files\\tess4j-4.0.2.jar;C:\\Utility\\tess4j_jar_files\\xmlgraphics-commons-1.4.jar;C:\\Utility\\ashot-1.5.x\\ashot-1.4.4.jar;C:\\Utility\\selenium-server-standalone\\selenium-server-standalone-3.14.0.jar;D:\\SeleniumJavaUtilities\\WebDriverReleases\\htmlUnitDriver\\htmlunit-driver-2.33.0-jar-with-dependencies.jar;D:\\SeleniumJavaUtilities\\Shutterbug\\selenium-shutterbug-0.9.jar List of paths to search when loading libraries: C:\\Program Files\\Java\\jre1.8.0_172\\bin;C:\\Windows\\Sun\\Java\\bin;C:\\Windows\\system32;C:\\Windows;C:/Program Files/Java/jre1.8.0_172/bin/server;C:/Program Files/Java/jre1.8.0_172/bin;C:/Program Files/Java/jre1.8.0_172/lib/amd64;C:\\Program Files (x86)\\Common Files\\Oracle\\Java\\javapath;C:\\Utility\\BrowserDrivers;C:\\Python\\Scripts\\;C:\\Python\\;C:\\Windows\\System32;C:\\apache-maven-3.3.3\\bin;C:\\apache-ant-1.10.1\\bin;C:\\Program Files (x86)\\Windows Kits\\8.0\\Windows Performance Toolkit\\;C:\\Program Files\\nodejs\\;C:\\Program Files (x86)\\MySQL\\MySQL Utilities 1.6\\;C:\\Program Files\\Git\\cmd;C:\\Users\\AtechM_03\\node_modules\\mocha;C:\\Utility\\BrowserDrivers;C:\\Program Files\\Java\\jdk1.8.0_172\\bin;C:\\Windows\\System32;C:\\Users\\AtechM_03\\AppData\\Roaming\\npm;C:\\Users\\AtechM_03\\Desktop;;. Default temp file path: C:\\Users\\ATECHM~1\\AppData\\Local\\Temp\\ Name of JIT compiler to use: null Path of extension directory or directories: C:\\Program Files\\Java\\jre1.8.0_172\\lib\\ext;C:\\Windows\\Sun\\Java\\lib\\ext Operating system name: Windows 8 Operating system architecture: amd64 Operating system version: 6.2 File separator: \\ Path separator: ; Line separator: User's account name: AtechM_03 User's home directory: C:\\Users\\AtechM_03 User's current working directory: C:\\Users\\AtechM_03\\LearnAutmation\\learn-automation

Writing System Properties

To modify the existing set of system properties, use System.setProperties . This method takes a Properties object that has been initialized to contain the properties to be set. This method replaces the entire set of system properties with the new set represented by the Properties object.

Note : Changing system properties is potentially dangerous and should be done with discretion. Many system properties are not reread after start-up and are there for informational purposes. Changing some properties may have unexpected side-effects.

The setProperties method changes the set of system properties for the current running application. These changes are not persistent. That is, changing the system properties within an application will not affect future invocations of the Java interpreter for this or any other application. The runtime system re-initializes the system properties each time its starts up. If changes to system properties are to be persistent, then the application must write the values to some file before exiting and read them in again upon startup.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM