Java Selenium 自动测试

导语: 

Selenium  是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括IE(7, 8, 9, 10, 11),Safari,Google Chrome,Opera等。这个工具的主要功能包括:测试与浏览器的兼容性——测试你的应用程序看是否能够很好得工作在不同浏览器和操作系统之上。测试系统功能——创建回归测试检验软件功能和用户需求。支持自动录制动作和自动生成 .Net、Java、Python 等不同语言的测试脚本。

 

本文是用Java语言做开发测试, 代码如下

1, 引入maven配置

      https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java

 <dependencies>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>
    </dependencies>

2, 下载谷歌浏览器驱动 (根据本机的浏览器版本下载对应驱动版本)

  地址: http://chromedriver.storage.googleapis.com/index.html

 

3, 编写代码

package com.selenium.test;

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

/**
 * 描述: selenium 自动测试 demo<br>
 *     测试功能: 打开百度,并输入"热点新闻",然后搜索,等待几秒,关闭浏览器.
 * created by hwj on 2019/4/26 21:16
 */
public class BaiduTest {

    //1, jar下载 https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java
    //2, 谷歌浏览器驱动 http://chromedriver.storage.googleapis.com/index.html

    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver",
                "D:\\person\\soft\\chromedriver_win32\\chromedriver.exe"); //谷歌浏览器驱动路径
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();

        //1,打开百度
        driver.get("https://www.baidu.com/");
        //2,给输入框填值
        driver.findElement(By.id("kw")).sendKeys(new String[]{"热点新闻"});
        //3,点击"百度一下"按钮
        driver.findElement(By.id("su")).click();

        try {
            Thread.sleep(1000 * 5);
        } catch (Exception e) {

        }
        //4,退出浏览器
        driver.quit();  //driver.close(); //关闭某个标签页

        //有一个比较全面的api链接(转):
        // https://www.cnblogs.com/test-my/p/5942387.html
        // https://www.cnblogs.com/unknows/p/7688613.html
    }
}

4, 运行上面的代码即可体验一下selenium功能.

 

以上只是一个简单的百度搜索功能测试. 

 

欢迎加我VX聊技术:             

          

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值