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()括號中即可

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM