selenium java 自动化测试 基于火狐浏览器/谷歌浏览器

:环境 java1.8+ieda

直接上代码

pom.xml

<?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>com.dqcer</groupId>
    <artifactId>seleniumAotuTest</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <!-- 启动谷歌浏览器需要的特点jar包-->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>22.0</version>
        </dependency>
        <!-- 集成selenium-->
         <dependency>
              <groupId>org.seleniumhq.selenium</groupId>
              <artifactId>selenium-java</artifactId>
              <version>3.7.1</version>
            </dependency>
    </dependencies>
</project>
SeleniumAotuTestDemo.java类

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.io.File;
import java.util.concurrent.TimeUnit;

/**
 * @Author: dongQin
 * @Date: 2018/6/14 8:51
 * @Description: selenium基于火狐浏览器/谷歌浏览器的自动化测试
 */

public class SeleniumAotuTestDemo {

    private static WebDriver webDriver;

    public static void main(String[] args) throws InterruptedException {

        //  初始化谷歌浏览加载所需的配置程序
        initChromeDriver();

        //  初始化火狐浏览器加载所需的配置程序
        //initFirefox();

        //  在打开地址前,清除cookies
        webDriver.manage().deleteAllCookies();

        //  同步浏览器
        webDriver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        //  打开目标地址,这个使用百度为例。只要是web系统都可以
        webDriver.get("https://www.baidu.com");

        //  搜索spring boot

        //  定位当前的输入框
        WebElement element = webDriver.findElement(By.xpath("//*[@id=\"kw\"]"));

        //  在输入框输入"spring boot"
        element.sendKeys("spring boot");

        //  定位当前的"百度一下"按钮所在的位置
        WebElement submint = webDriver.findElement(By.xpath("//*[@id=\"su\"]"));

        //  点击提交
        submint.click();

        //  休息3秒,等待搜索结果并查看
        Thread.sleep(3000);

        //  最后退出,关闭浏览器
        webDriver.quit();

        System.out.println("good job!");
    }

    /**
     * @Author: dongQin
     * @Date: 2018/6/14 9:09
     * @Description: 初始化加载所需的配置程序
     */

    public static void initChromeDriver(){
        //  chromedriver.exe要与当前使用的谷歌浏览器版本一一对应,下载的地址可在淘宝或者GitHub,并将其解压放在与谷歌
        //  .exe 文件同级下
        File file = new File("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
        System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());

        webDriver  = new ChromeDriver();
    }

    /**
     * @Author: dongQin
     * @Date: 2018/6/14 9:51
     * @Description: 初始化火狐浏览器加载所需的配置程序
     */

    public static void initFirefox(){

        //  firefox.exe同样要与当前使用的火狐浏览器版本一一对应,下载的地址可在淘宝或者GitHub

        //  指定火狐浏览器程序的位置
        System.setProperty("webdriver.firefox.bin", "D:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");

        //  指定firefox.exe插件的位置
        System.setProperty("webdriver.gecko.driver", "C://geckodriver.exe");

        webDriver = new FirefoxDriver();
    }
}

 

如何定位当前位置呢?By.xpath()获取括号的值,有个小技巧,打开谷歌浏览器,打开控制台,经过图下操作就可自动获取到xpath值,复制到By.xpath()括号中即可

 

posted @ 2018-06-13 18:50  dqcer  阅读(1623)  评论(0编辑  收藏  举报