在硒中,为什么我们在System.setProperty("webdriver.chrome.driver", "E://chromedriver.exe");
块内添加static{}
?
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");
}
}
答案 0 :(得分:0)
在使用Selenium-Java客户端时,Java平台本身使用Properties对象来维护其自己的配置。 System 类维护用于描述当前工作环境配置的 Properties 对象。系统属性包括有关当前用户, Java运行时的当前版本,用于分隔文件路径名的各个组成部分的字符 的信息,等
以下table描述了一些最重要的系统属性:
安全注意事项:Security Manager可以限制对系统属性的访问。这是applet中最常见的问题,阻止了applet读取某些系统属性以及写入任何系统属性。有关在applet中访问系统属性的更多信息,请参考System Properties课程中的Doing More With Java Rich Internet Applications。
System 类有两种用于读取系统属性的方法:getProperty
和getProperties
。
System 类具有getProperty
的两个不同版本。两者都检索参数列表中命名的属性的值。这两个getProperty
方法中最简单的方法是使用一个参数,即属性键。例如,要获取path.separator
的值,请使用以下语句:
System.getProperty("path.separator");
getProperty
方法返回一个包含属性值的字符串。如果该属性不存在,则此版本的getProperty
返回null。
getProperty
的另一个版本需要两个String参数:第一个参数是要查找的键,第二个参数是在找不到键或没有值的情况下返回的默认值。例如,以下对getProperty
的调用将查找名为System
的{{1}}属性。这不是有效的系统属性,因此此方法返回第二个参数提供的默认值:“ Selenium WebDriver!”。
subliminal.message
System 类提供的最后一个用于访问属性值的方法是System.getProperty("subliminal.message", "Selenium WebDriver!");
方法,该方法返回一个Properties
对象。该对象包含一组完整的系统属性定义。
用于提取系统属性的 Java 程序:
代码块:
getProperties
控制台输出:
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"));
}
}
要修改现有的系统属性集,请使用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
。此方法采用一个System.setProperties
对象,该对象已初始化为包含要设置的属性。此方法将整个系统属性集替换为Properties
对象表示的新集。
注意:更改系统属性有潜在危险,应谨慎行事。启动后不会重新读取许多系统属性,这些属性仅供参考。更改某些属性可能会产生意想不到的副作用。
Properties
方法更改当前正在运行的应用程序的系统属性集。这些变化不是持久的。也就是说,更改应用程序内的系统属性将不会影响对此应用程序或任何其他应用程序的Java解释器的将来调用。每次启动时,运行时系统都会重新初始化系统属性。如果要永久保留对系统属性的更改,则应用程序必须在退出之前将值写入某个文件,并在启动时再次读取它们。