使用 Selenium 时 System.setProperty() 的意义是什么 [英] What is the significance of System.setProperty() while working with Selenium
问题描述
在selenium中,为什么要在static{}
System.setProperty("webdriver.chrome.driver", "E://chromedriver.exe");> 阻止?
公共类演示{静止的{System.setProperty("webdriver.chrome.driver", "E://chromedriver.exe");}公共静态无效主(字符串 [] args){WebDriver 驱动程序 = new ChromeDriver();driver.get("http://www.google.com");}}
在使用 Selenium-Java 客户端时,Java 平台本身使用
<块引用>安全注意事项:可以通过 安全管理器.这通常是小程序中的一个问题,它被阻止读取某些系统属性和写入任何系统属性.有关在小程序中访问系统属性的更多信息,请参阅系统属性 在 使用 Java 富 Internet 应用程序做更多事情 课程中.
<小时>
读取系统属性
System 类有两个用于读取系统属性的方法:getProperty
和 getProperties
.
System 类有两个不同版本的 getProperty
.两者都检索参数列表中命名的属性的值.两个 getProperty
方法中较简单的一个接受一个参数,一个属性键.例如,要获取 path.separator
的值,请使用以下语句:
System.getProperty("path.separator");
getProperty
方法返回一个包含属性值的字符串.如果该属性不存在,则此版本的 getProperty
返回 null.
另一个版本的 getProperty
需要两个 String 参数:第一个参数是要查找的键,第二个参数是默认值,如果找不到该键或它没有价值.例如,以下对 getProperty
的调用查找名为 subliminal.message
的 System
属性.这不是有效的系统属性,因此该方法不会返回 null,而是返回作为第二个参数提供的默认值:Selenium WebDriver!"
System.getProperty("subliminal.message", "Selenium WebDriver!");
System 类提供的最后一个访问属性值的方法是 getProperties
方法,它返回一个 Properties
对象.该对象包含一套完整的系统属性定义.
用于提取系统属性的Java程序:
代码块:
包Java_Experiments;公共类 system_getProperty {公共静态无效主(字符串 [] 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 运行时环境版本:"+System.getProperty("java.version"));System.out.println("Java 运行时环境供应商:"+System.getProperty("java.vendor"));System.out.println("Java 供应商网址:"+System.getProperty("java.vendor.url"));System.out.println("Java安装目录:"+System.getProperty("java.home"));System.out.println("Java 虚拟机规范版本:"+System.getProperty("java.vm.specification.version"));System.out.println("Java 虚拟机规范供应商:"+System.getProperty("java.vm.specification.vendor"));System.out.println("Java 虚拟机规范名称:"+System.getProperty("java.vm.specification.name"));System.out.println("Java 虚拟机实现版本:"+System.getProperty("java.vm.version"));System.out.println("Java 虚拟机实现厂商:"+System.getProperty("java.vm.vendor"));System.out.println("Java 虚拟机实现名称:"+System.getProperty("java.vm.name"));System.out.println("Java 运行时环境规范版本:"+System.getProperty("java.specification.version"));System.out.println("Java 运行时环境规范供应商:"+System.getProperty("java.specification.vendor"));System.out.println("Java 运行时环境规范名称:"+System.getProperty("java.specification.name"));System.out.println("Java 类格式版本号:"+System.getProperty("java.class.version"));System.out.println("Java 类路径:"+System.getProperty("java.class.path"));System.out.println("加载库时要搜索的路径列表:"+System.getProperty("java.library.path"));System.out.println("默认临时文件路径:"+System.getProperty("java.io.tmpdir"));System.out.println("使用的JIT编译器名称:"+System.getProperty("java.compiler"));System.out.println("扩展目录路径:"+System.getProperty("java.ext.dirs"));System.out.println("操作系统名称:"+System.getProperty("os.name"));System.out.println("操作系统架构:"+System.getProperty("os.arch"));System.out.println("操作系统版本:"+System.getProperty("os.version"));System.out.println("文件分隔符:"+System.getProperty("file.separator"));System.out.println("路径分隔符:"+System.getProperty("path.separator"));System.out.println("行分隔符:"+System.getProperty("line.separator"));System.out.println("用户名:"+System.getProperty("user.name"));System.out.println("用户家目录:"+System.getProperty("user.home"));System.out.println("用户当前工作目录:"+System.getProperty("user.dir"));}}
控制台输出:
C:\Utility\BrowserDrivers\chromedriver.exe硒网络驱动程序!Java 运行时环境版本:1.8.0_172Java 运行时环境供应商:Oracle CorporationJava 供应商 URL:http://java.oracle.com/Java安装目录:C:\Program Files\Java\jre1.8.0_172Java 虚拟机规范版本:1.8Java 虚拟机规范供应商:Oracle CorporationJava 虚拟机规范名称:Java 虚拟机规范Java虚拟机实现版本:25.172-b11Java 虚拟机实现供应商:Oracle CorporationJava 虚拟机实现名称:Java HotSpot(TM) 64-Bit Server VMJava 运行时环境规范版本:1.8Java 运行时环境规范供应商:Oracle CorporationJava 运行时环境规范名称:Java Platform API SpecificationJava 类格式版本号:52.0Java 类路径: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加载库时搜索路径列表: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:\ProgramFiles (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:\ProgramFiles (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;;.默认临时文件路径:C:\Users\ATECHM~1\AppData\Local\Temp\要使用的 JIT 编译器的名称:null扩展目录路径:C:\Program Files\Java\jre1.8.0_172\lib\ext;C:\Windows\Sun\Java\lib\ext操作系统名称:Windows 8操作系统架构:amd64操作系统版本:6.2文件分隔符:\路径分隔符:;行分隔符:用户账户名:AtechM_03用户的主目录:C:\Users\AtechM_03用户当前工作目录:C:\Users\AtechM_03\LearnAutmation\learn-automation
写入系统属性
要修改现有的系统属性集,请使用 System.setProperties
.此方法采用一个 Properties
对象,该对象已初始化为包含要设置的属性.此方法用 Properties
对象表示的新集替换整个系统属性集.
注意:更改系统属性具有潜在危险,应谨慎进行.许多系统属性在启动后不会重新读取,仅供参考.更改某些属性可能会产生意想不到的副作用.
setProperties
方法更改当前运行的应用程序的系统属性集.这些变化不是持久的.也就是说,更改应用程序内的系统属性不会影响将来为此应用程序或任何其他应用程序调用 Java 解释器.运行时系统每次启动时都会重新初始化系统属性.如果对系统属性的更改要持久化,则应用程序必须在退出前将值写入某个文件,并在启动时再次读入.
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:
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.
这篇关于使用 Selenium 时 System.setProperty() 的意义是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!