Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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");

    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
109 views
Welcome To Ask or Share your Answers For Others

1 Answer

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_SystemProperties

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:UtilityBrowserDriverschromedriver.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 FilesJavajre1.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:UsersAtechM_03LearnAutmationlearn-automationin;C:Utilitylog4j-1.2.15.jarlog4j-1.2.15.jar;C:UtilitySikulisikulixapi.jar;C:Utilitypoi-bin-3.15-20160924poi-3.15poi-3.15.jar;C:Utilitypoi-bin-3.15-20160924poi-3.15poi-examples-3.15.jar;C:Utilitypoi-bin-3.15-20160924poi-3.15poi-excelant-3.15.jar;C:Utilitypoi-bin-3.15-20160924poi-3.15poi-ooxml-3.15.jar;C:Utilitypoi-bin-3.15-20160924poi-3.15poi-ooxml-schemas-3.15.jar;C:Utilitypoi-bin-3.15-20160924poi-3.15poi-scratchpad-3.15.jar;C:Utilitypoi-bin-3.15-20160924poi-3.15ooxml-libcurvesapi-1.04.jar;C:Utilitypoi-bin-3.15-20160924poi-3.15ooxml-libxmlbeans-2.6.0.jar;C:Utilitypoi-bin-3.15-20160924poi-3.15libcommons-codec-1.10.jar;C:Utilitypoi-bin-3.15-20160924poi-3.15libcommons-collections4-4.1.jar;C:Utilitypoi-bin-3.15-20160924poi-3.15libcommons-logging-1.2.jar;C:Utilitypoi-bin-3.15-20160924poi-3.15libjunit-4.12.jar;C:Utilitypoi-bin-3.15-20160924poi-3.15liblog4j-1.2.17.jar;C:Utilityip4j_1.3.2zip4j_1.3.2.jar;C:UtilityPhantomjsDriver1.1.0phantomjsdriver-1.4.4.jar;C:Utility	ess4j_jar_filescommons-beanutils-1.9.2.jar;C:Utility	ess4j_jar_filescommons-collections-3.2.1.jar;C:Utility	ess4j_jar_filescommons-io-2.6.jar;C:Utility	ess4j_jar_filescommons-logging-1.2.jar;C:Utility	ess4j_jar_filesfontbox-2.0.9.jar;C:Utility	ess4j_jar_filesghost4j-1.0.1.jar;C:Utility	ess4j_jar_filesitext-2.1.7.jar;C:Utility	ess4j_jar_filesjai-imageio-core-1.4.0.jar;C:Utility	ess4j_jar_filesjbig2-imageio-3.0.0.jar;C:Utility	ess4j_jar_filesjboss-logging-3.1.4.GA.jar;C:Utility	ess4j_jar_filesjboss-vfs-3.2.12.Final.jar;C:Utility	ess4j_jar_filesjcl-over-slf4j-1.7.25.jar;C:Utility	ess4j_jar_filesjna-4.1.0.jar;C:Utility	ess4j_jar_filesjul-to-slf4j-1.7.25.jar;C:Utility	ess4j_jar_fileslept4j-1.9.4.jar;C:Utility	ess4j_jar_fileslog4j-1.2.17.jar;C:Utility	ess4j_jar_fileslog4j-over-slf4j-1.7.25.jar;C:Utility	ess4j_jar_fileslogback-classic-1.2.3.jar;C:Utility	ess4j_jar_fileslogback-core-1.2.3.jar;C:Utility	ess4j_jar_filespdfbox-2.0.9.jar;C:Utility	ess4j_jar_filespdfbox-debugger-2.0.9.jar;C:Utility	ess4j_jar_filespdfbox-tools-2.0.9.jar;C:Utility	ess4j_jar_filesslf4j-api-1.7.25.jar;C:Utility	ess4j_jar_files	ess4j-4.0.2.jar;C:Utility	ess4j_jar_filesxmlgraphics-commons-1.4.jar;C:Utilityashot-1.5.xashot-1.4.4.jar;C:Utilityselenium-server-standaloneselenium-server-standalone-3.14.0.jar;D:SeleniumJavaUtilitiesWebDriverReleaseshtmlUnitDriverhtmlunit-driver-2.33.0-jar-with-dependencies.jar;D:SeleniumJavaUtilitiesShutterbugselenium-shutterbug-0.9.jar
    List of paths to search when loading libraries: C:Program FilesJavajre1.8.0_172in;C:WindowsSunJavain;C:Windowssystem32;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 FilesOracleJavajavapath;C:UtilityBrowserDrivers;C:PythonScripts;C:Python;C:WindowsSystem32;C:apache-maven-3.3.3in;C:apache-ant-1.10.1in;C:Program Files (x86)Windows Kits8.0Windows Performance Toolkit;C:Program Files
    odejs;C:Program Files (x86)MySQLMySQL Utilities 1.6;C:Program FilesGitcmd;C:UsersAtechM_03
    ode_modulesmocha;C:UtilityBrowserDrivers;C:Program FilesJavajdk1.8.0_172in;C:WindowsSystem32;C:UsersAtechM_03AppDataRoaming
    pm;C:UsersAtechM_03Desktop;;.
    Default temp file path: C:UsersATECHM~1AppDataLocalTemp
    Name of JIT compiler to use: null
    Path of extension directory or directories: C:Program FilesJavajre1.8.0_172libext;C:WindowsSunJavalibext
    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:UsersAtechM_03
    User's current working directory: C:UsersAtechM_03LearnAutmationlearn-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 propert


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...