Java+Selenium环境搭建

一、下载文件 

先要去官网(http://seleniumhq.org/download/)下载必需的文件: 
 

  • Selenium IDE (专门用于 FireFox 测试的独立界面,可以录制测试步骤,但我更倾向于写代码做标准的功能测试)
  • Selenium Server (可以输入指令控制、可以解决跨域的 js 问题,等到后面学到了再讲吧)
  • The Internet Explorer Driver Server (专门用于IE测试的)
  • Selenium Client Drivers (可以找到你熟悉的语言,例如我选择的 Java)
  • Third Party Browser Drivers NOT SUPPORTED/DEVELOPED by seleniumhq(第三方开发的 Selenium 插件,第一个就是 Chrome 的,否则你就没办法测试 Chrome 了)
  • 其他的,就根据你自己的需要寻找吧,目前这些足够我用

官网的文档(http://seleniumhq.org/documentation/

【1. 建立 Maven 工程】 
Selenium 支持 maven 工程,这会让你的工作更加简便。

用IDEA建个maven工程,建成后,直接修改 pom.xml,

(参考:http://seleniumhq.org/docs/03_webdriver.html#setting-up-a-selenium-webdriver-project

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>relation</groupId>
    <artifactId>WebAuto</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.25.0</version>
        </dependency>

        <dependency>
            <groupId>com.opera</groupId>
            <artifactId>operadriver</artifactId>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-api</artifactId>
            <version>2.25.0</version>
        </dependency>


    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.opera</groupId>
                <artifactId>operadriver</artifactId>
                <version>0.16</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.seleniumhq.selenium</groupId>
                        <artifactId>selenium-remote-driver</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

2.创建测试方法验证

package com.ralation.week1;


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.concurrent.TimeUnit;

//基础篇,环境搭建及验证
public class basis {


    public static void main(String[] args){
        //因为浏览器不是在默认安装路径所以,先设置浏览器路径
        System.setProperty("webdriver.chrome.driver","F:\\Selenium\\Tools\\chromedriver.exe");
        //先获取一个谷歌浏览器实例
        WebDriver driver = new ChromeDriver();

        driver.manage().window().maximize();

        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

        driver.get("https://www.baidu.com");

        String title=driver.getTitle().toString();

        System.out.println("当前页面标题是:"+title);

        driver.quit();


    }
}

输出结果

0.5.2.jar;C:\Users\Administrator\.m2\repository\org\seleniumhq\selenium\selenium-api\2.25.0\selenium-api-2.25.0.jar;C:\Users\Administrator\.m2\repository\org\json\json\20080701\json-20080701.jar" com.ralation.week1.basis
Starting ChromeDriver 2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73) on port 8786
Only local connections are allowed.
当前页面标题是:百度一下,你就知道

Process finished with exit code 0

错误提醒: 
1)Exception in thread "main" org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed. 
出现这个错误,是说明你的 FireFox 文件并没有安装在默认目录下,这时候需要在最开始执行:System.setProperty 设置环境变量  "webdriver.firefox.bin" 将自己机器上 FireFox 的正确路径设置完毕后即可。 

2)Exception in thread "main" org.openqa.selenium.UnsupportedCommandException: Bad request 

出现这个错误,很有意思。 查了一下 有人说应该是 hosts 出现了问题,加上一个 127.0.0.1  localhost 就行了,但我的 hosts 上肯定有这个玩意,为啥也会出现这个问题呢? 

经过调试,发现 127.0.0.1 localhost 的设置必须要在 hosts 文件的最开始,而且如果后面有其他设置后,也不要再出现同样的 127.0.0.1 localhost ,只要有就会出错。(因为我为了方便访问 google 的网站,专门加入了 smarthosts 的内容,导致了 localhost 的重复)

【3. 测试 Chrome】 
Chrome 虽然不是 Selenium 的原配,但是没办法,她太火辣了,绝对不能抛下她不管的。 
把 ExampleForFireFox.java 稍微修改就可以制作出一个 ExampleForChrome.java ,直接把 new FireFoxDriver() 修改为 new ChromeDriver() 你会发现还是行不通。 

错误如下: 
1)Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see http://code.google.com/p/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://code.google.com/p/chromedriver/downloads/list 
这应该是找不到 chrome 的文件,好吧,利用 System.setProperty 方法添加路径,这里要注意,是 “webdriver.chrome.driver” 可不是“webdriver.chrome.bin” 

设置路径后还是会报错: 
2)[6416:4580:1204/173852:ERROR:gpu_info_collector_win.cc(91)] Can't retrieve a valid WinSAT assessment. 
这个貌似是因为 Selenium 无法直接启动 Chrome 导致的,必须要通过前面咱们下载 Chrome 的第三方插件 ChromeDriver,去看第一个错误中提示给你的 网址:http://code.google.com/p/selenium/wiki/ChromeDriver 

发布了17 篇原创文章 · 获赞 0 · 访问量 179

猜你喜欢

转载自blog.csdn.net/qq_37637691/article/details/100069018