1. 安装JDK
2. 配置环境变量
JAVA_HOME = E:\Java\Java\jdk1.7.0_15
PATH = %JAVA_HOME%\bin;%JAVA_HOME%\jre\bin
CLASSPATH = .;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar
注:上面这行的“.”不能忽略掉了。
然后在windows命令窗口任意目录下,运行命令java -version,或执行成功则安装和配置成功。
3. 安装eclipse
URL: http://www.eclipse.org/downloads/
4. 安装TestNG
可直接安装 Help->Install New Software
验证方法:file-->new-->other-->TestNg
5.下载Selenium IDE、SeleniumRC、IEDriverServer,WebDriver
1) Selenium IDE:selenium_ide-2.9.1-fx.xpi 用来在Firefox上录制脚本。
2) Selenium RC:selenium-server-standalone-3.4.0.jar 模拟服务器端,selenium 1.0执行脚本时需要单独启动该jar包, selenium webdriver无需单独启动。
3) IEDriverServer:IEDriverServer_Win32_2.40.0.zip IE驱动
4) WebDriver: selenium-java-3.4.0.zip
6. 下载Firefox、安装Selenium IDE、firebug、Xpath checker、Xpath finder插件
1) 安装Selenium IDE
把前面下载的selenium-ide拖放到Firefox,弹出图后,安装即可。
验证: firefox 工具〉开发者〉selenium IDE
2) 安装firebug:进附加组件,搜索firebug、Xpath,安装,重启浏览器。
7. 启动SeleniumRC
selenium 1.0需要启动单独rc,webdriver则不需要启动。
启动seleniumRC的方法: cmd命令行进入selenium-server-standalone-2.40.0.jar存放目录(或者进入selenium-server-standalone-2.40.0.jar存放目录然后在文档路径里输入cmd),然后输入如下命令 java -jar selenium-server-standalone-2.40.0.jar
8. Eclipse执行Selenium的Java实例
1)新建java工程:File-->new-->other-->Java Project
2)引入Selenium相关的包:Properties-->Java Build Path-->Libraries-->Add External Jars
9. Below error happen when execute "WebDriver driver = new FirefoxDriver();" on Selenium 3.0, need download file geckodriv.
Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases
URL: https://github.com/mozilla/geckodriver/releases
可以单独创建一个目录,如:D:/drivers/ ,把不同浏览器的驱动都放到该目录。geckodriver.exe(Firefox)、chromedriver.exe(Chrome)、MicrosoftWebDriver.exe(Edge)、IEDriverServer.exe(IE)、operadriver.exe(Opera)等。
然后,将D:/drivers/添加到系统环境变最path下面即可。
1)Firefox
System.setProperty ("webdriver.firefox.bin","C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
System.setProperty("webdriver.gecko.driver","C:\\Tools\\Selenium\\driver\\geckodriver");
2)IE
download IEDriverServer.exe and then put into the same folder as iexplore.exe.
File file_ie=newFile("C:\\Program Files\\Internet Explorer\\IEDriverServer.exe");
System.setProperty( "webdriver.ie.driver",file_ie.getAbsolutePath());
DesiredCapabilities caps= DesiredCapabilities.internetExplorer();
caps.setCapability("ignoreZoomSetting",true);
WebDriver ieDriver=newInternetExplorerDriver(caps);
//WebDriver driver = new InternetExplorerDriver();
ieDriver.get("https://www.baidu.com/");