Java自动化测试selenium教程

首先创建工程添加selenium依赖
这里使用的chrome浏览器编写示例
你用什么浏览器测试就下载什么浏览器驱动
下面先说一下自动化环境常见问题

Firefox

1. Firefox路径问题

firefox火狐浏览器去完成自动化测试时,代码报了如下错误:

Cannot find firefox binary in PATH. mark sure firefox is installed

错误原因:firefox安装在其它路径,不是默认的安装路径

代码设置:

//换成你自己火狐浏览器安装位置
System.setProperty("webdriver.firefox.bin", "I:\\WebDriver\\Mozilla Firefox\\firefox.exe");

解决办法:指定firefox可执行文件路径:webdriver.firefox.bin

2. selenium 3.x Firefox驱动问题
使用selenium3.x+firefox火狐浏览器去完成自动化测试时,代码报了如下错误:

The path to the driver executable must be set by the webdriver.gecko.driver system properity

错误原因:

缺少火狐浏览器驱动包。如果selenium版本是3.x的,需要使用驱动包

代码设置:

System.setProperty("webdriver.gecko.driver", "src/resources/geckodriver.exe");

3. selenium与驱动版本匹配问题

使用selenium2.x版本+firefox去完成自动化测试时,代码报了如下错误:

Unable to connect to host 127.0.0.1 on port 7055 after 35000ms

错误总结:firefox浏览器版本和selenium版本不适配。
解决办法:建议降级火狐版本到47以下(比如:selenium 2.53.0+firefox 46)
4.selenium for firefox说明
selenium 2.x
selenium 2.x中自动集成了firefox驱动。
因此只须selenium 版本与firefox浏览器版本对应即可

selenium版本Firefox浏览器
2.25.0v 18
2.30.0v 19
2.31.0v 20
2.42.2v 29
2.44.0v 33 (不支持31)
2.52.0v 45.0
2.53.0v 46.0
2.53.1v 47.0.1

selenium 3.x
selenium,geckodriver,firefox 对应版本说明:

selenium版本geckodriver版本Firefox版本
3.30.15v 48+
3.40.16v 52
3.40.17v 52
3.40.18v 53
3.50.19v 55
3.110.21v 57

从selenium 3.0.0开始就要求firefox为48及以上版本
selenium 3.x使用的java版本为jdk 1.8
selenium 3.x使用geckodriver作为firefox浏览器的驱动的替代

4、Firefox自动化相关工具链接
Change log :https://raw.githubusercontent.com/SeleniumHQ/selenium/master/java/CHANGELOG

Firefox驱动https://github.com/mozilla/geckodriver/releases/

Firefox各版本 http://ftp.mozilla.org/pub/firefox/releases/

Firefox驱动 镜像 https://npm.taobao.org/mirrors/geckodriver/
InternetExplorer

  1. IE浏览器驱动问题使用IE浏览器去完成自动化测试时,代码报了如下错误:
The path to the driver executable must be set by the webdriver.ie.driver system property

错误总结:缺少IE浏览器驱动包
解决办法:往项目中添加IE驱动包,并加载驱动的配置。
下载地址 http://www.seleniumhq.org/download/
建议下载版本:3.7.0:http://selenium-release.storage.googleapis.com/index.html?path=3.7/
IE驱动版本与Selenium版本保持相同即可:http://selenium-release.storage.googleapis.com/index.html

System.setProperty("webdriver.ie.driver", "src/resources/IEDriverServer.exe");

2. IE浏览器保护模式问题:使用IE浏览器去完成自动化测试时,代码报了如下错误:

Protected Mode Settings are not the same for all zones

解决方法1:
浏览器设置(但是换一台电脑就不适用了)
打开IE浏览器->工具->安全->全部勾选启用保护模式
浏览器设置(但是换一台电脑就不适用了)
打开IE浏览器->工具->安全->全部勾选启用保护模式
解决方法2:

忽略浏览器保护模式的设置InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS
代码:

//取消IE安全设置(忽略IE的Protected Mode的设置)
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);

3. IE浏览器缩放设置
使用IE浏览器去完成自动化测试时,代码报了如下错误:

Browser zoom level was set to 125%.It should be set to 100%

错误总结:

浏览器缩放级别设置不对导致的(点工具栏页面->缩放设置)

解决办法:

忽略此设置:InternetExplorerDriver.IGNORE_ZOOM_SETTING
代码:

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);

4. IE浏览器window丢失问题
使用IE浏览器去完成自动化测试时,代码报了如下错误:

unable to find element with id -- kw

错误总结:不是因为没有设置等待时间,而是因为之前的window对象已经丢失
解决办法:最快的解决办法是直接指定一个初始化页InternetExplorerDriver.INITIAL_BROWSER_URL
代码:

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL, "http://www.baidu.com");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL, "http://www.baidu.com");

Chrome

1. chrome浏览器驱动路径
使用Chrome做测试时,报了如下错误:

The path to the driver executable must be set by the webdirver.chrome.driver system properity

解决方案:系统设置Chrome驱动文件的路径

System.setProperty("webdriver.chrome.driver", "xxx");

2. chrome浏览器与chromeDriver匹配问题
使用chrome浏览器去完成自动化测试时,chrome浏览器停止运行chromedriver.exe 已停止工作

错误总结:chrome浏览器版本过高,虽然根据官网上的信息,2.33的chrome驱动支持60-62的谷歌。但是60根本不行

解决办法:降级chrome
3.Chrome与ChromeDriver版本对照表

ChromeDriver 版本支持的 Chrome 版
v2.41v67-69
v2.40v66-68
v2.38v65-67
v2.37v64-66
v2.36v65-67
v2.35v62-64
v2.34v61-63
v2.33v60-62
v2.32v59-61
v2.31v58-60
v2.30v58-60
v2.29v56-58

chrome浏览器各版本
http://www.chromedownloads.net/chrome64win/
禁止谷歌浏览器更新
https://jingyan.baidu.com/article/76a7e409f2137afc3b6e15be.html

ChromeDriver 镜像 http://npm.taobao.org/mirrors/chromedriver

Selenium 镜像 http://npm.taobao.org/mirrors/selenium
JDK版本问题
使用3.x的selenium来完成自动化测试时,代码报了如下错误:

Exception in thread "main" java.lang.UnsupportedClassVersionError:

错误总结:

3.x的selenium需要1.8的jdk,可能jdk版本过低

解决办法:

降级selenium版本,或提高jdk的版本为1.8

下面是chrome浏览器使用的示例代码:

		<dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>
package com.baojiaren;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

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

/**
 * @author ✎ℳ๓₯㎕.倾心❀、
 * @create 2021-05-15-15:20
 */
public class Main {
    public static void main(String[] args) throws InterruptedException {
        // 让系统知道chrome driver 的位置
        // key 必须为webdriver.chrome.driver 否则报错
        //等待分别有3种   需要等待的原因是页面元素还没加载完全程序运行太快获取不了元素 需要等元素加载完毕
        //硬性等待 Thread.sleep(2000);  主线程直接休眠
        // 隐式等待 wd.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        //WebDriverWait wait = new WebDriverWait(wd, 60);
        /**
        显示等待 时间单位为秒
        WebDriverWait wait = new WebDriverWait(wd, 60);
        页面元素存在并且可见
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("s_ipt")));
        */
        System.setProperty("webdriver.chrome.driver", "I:\\WebDriver\\chromedriver.exe");
        WebDriver wd = new ChromeDriver();
        //tencent(wd);
        // nav(wd);
        //waits(wd);
        //showWait(wd);
        alerts(wd);
    }

    /**
     * 显示等待
     *
     * @param wd
     */
    public static void showWait(WebDriver wd) {
        WebDriverWait wait = new WebDriverWait(wd, 60);
        wd.manage().window().maximize(); //页面最大化
        wd.get("http://www.baidu.com/");
        // 页面元素存在并且可见
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("s_ipt")));
        wd.findElement(By.className("s_ipt")).sendKeys("腾讯课堂");
        wd.findElement(By.xpath("//*[@id=\"su\"]")).click();
        // 页面元素是否在页面上可用和可被单击
        wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[text()='官方']")));
        wd.findElement(By.xpath("//a[text()='官方']")).click();
        ExpectedConditions.elementToBeClickable(By.xpath("//a[text()='官方']"));

        Alert alert = wd.switchTo().alert();
        alert.accept();
        alert.dismiss();
        alert.getText();
        /*
        其他的一些方法:
            页面元素存在并且可见
            visibilityOfElementLocated
            页面元素是否在页面上可用和可被单击
            elementToBeClickable()
            页面元素是否被选中的状态
            ExpectedConditions.elementToBeSelected(By.xpath("//a[text()='官方']"));
            // 在页面元素中是否包含特定的文本
            textToBePresentInElement()
            //页面元素在页面中存在
            presenceOfElementLocated()
            找到alert弹框
            wd.switchTo().alert()
            Alert alert = wd.switchTo().alert();
            alert.accept(); 确认
            alert.dismiss();取消
            alert.getText();获取文本
         */
    }

	
    public static void alerts(WebDriver wd) throws InterruptedException {
        wd.manage().window().maximize();
        wd.get("F:\\java_senior\\Selenium\\src\\main\\resources\\index.html");
        Thread.sleep(2000);
        wd.findElement(By.id("btn")).click();
        Thread.sleep(2000);
        Alert alert = wd.switchTo().alert();
        System.out.println(alert.getText());
        alert.accept();
    }
    public static void waits(WebDriver wd) {
        wd.manage().window().maximize(); //页面最大化
        wd.findElement(By.className("s_ipt")).sendKeys("腾讯课堂");
        wd.findElement(By.xpath("//*[@id=\"su\"]")).click();
        wd.findElement(By.xpath("//a[text()='官方']")).click();
    }

    /**
     * 隐式等待
     *
     * @param wd
     * @throws InterruptedException
     */
    public static void tencent(WebDriver wd) throws InterruptedException {
        // 隐式等待
        wd.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        wd.get("http://www.baidu.com/");
        WebDriver.Options op = wd.manage();
        op.window().maximize(); //页面最大化
        Thread.sleep(2000);
        wd.get("http://www.baidu.com/");
        wd.findElement(By.className("s_ipt")).sendKeys("腾讯课堂");
        //点击百度按钮
        Thread.sleep(2000);
        wd.findElement(By.xpath("//*[@id=\"su\"]")).click();
        Thread.sleep(2000);
        System.out.println("==================" + wd.getWindowHandle());
        System.out.println("所有==================" + wd.getWindowHandles());
        wd.findElement(By.xpath("//a[text()='官方']")).click();
        Thread.sleep(5000);
        System.out.println("==================" + wd.getWindowHandle());
        System.out.println("最后所有==================" + wd.getWindowHandles());
        Dimension size = op.window().getSize();
        int height = size.getHeight();
        int width = size.getWidth();
        System.out.println("窗口宽度" + width);
        System.out.println("窗口高度" + height);
        Point point = op.window().getPosition();
        System.out.println(point.getX());
    }

    /**
     * navigate对象操作
     *
     * @param wd
     */
    public static void nav(WebDriver wd) {
        WebDriver.Navigation navigate = wd.navigate();
        //访问
        navigate.to("http://www.jd.com");
        //刷新
        navigate.refresh();
        //后退
        navigate.back();
        //前进
        navigate.forward();
    }

    public static void bjTest(WebDriver wd) throws InterruptedException {
        Thread.sleep(1000);
        wd.manage().window().maximize(); //页面最大化
        wd.get("http://baidu.com/");
        wd.findElement(By.className("s_ipt")).sendKeys("宝家人");
        Thread.sleep(1000);
        //点击百度按钮
        wd.findElement(By.xpath("//*[@id=\"su\"]")).click();
        Thread.sleep(1000);
        wd.findElement(By.xpath("//*[@id=\"1\"]/h3/a")).click();
        Thread.sleep(5000);
        Thread.sleep(5000);
        //wd.findElement(By.cssSelector("a[title='海带']")).click();
        System.out.println("==================" + wd.getWindowHandle());
    }

    public static void bjrTest(WebDriver wd) throws InterruptedException {
        wd.get("http://baidu.com/");
      /*  try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        //wd.findElement(By.name("wd")).sendKeys("百度");
        // wd.findElement(By.linkText("新闻")).click();
        //wd.findElement(By.partialLinkText("hao")).click();
        wd.findElement(By.className("s_ipt")).sendKeys("宝家人");
        //点击百度按钮
        wd.findElement(By.xpath("//*[@id=\"su\"]")).click();
        //wd.findElement(By.xpath("//input[@name=\"wd\"]")).sendKeys("宝家人");
        //wd.findElement(By.xpath("//input[@name=\"wd\"]"));
        wd.findElement(By.id("su")).click();
        Thread.sleep(1000);
        //wd.findElement(By.xpath("//*[@id=\"1\"]/h3/a")).click();
        wd.manage().window().maximize(); //页面最大化
        wd.findElement(By.xpath("/html/body/div[8]/div/div[2]/div[2]/ul/li[1]")).click();
        wd.findElement(By.cssSelector("a[title='海带']")).click();
        System.out.println(wd.getCurrentUrl());
        System.out.println(wd.getTitle());
        System.out.println(wd.getPageSource());
        // wd.quit();
    }
}






  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
课程介绍你是否在寻找机会进入自动化测试领域? 你是否渴望学习selenium webdriver + Java以及最新的框架和技术进行web自动化测试? 你是否感兴趣学习Selenium如何用在你现有的项目里的? 这门课带你从Selenium搭建环境开始讲起,然后学习selenium,TestNG, logback, maven, jenkins。 我们假设学员没有任何自动化经验,来设计的这套课程。每个课题都从最基础的开始讲起。Selenium相关的该覆盖的课题都覆盖了。 例子都是来自于真实的web应用项目,帮助你理解不同的组件怎么用上自动化,这将展示给你一个行业层面的框架,增加自信心。 全网没有其他课程像这门课涵盖到如此之深的细节。 您将会学到什么 学完课程以后,你将拥有完整的Selenium Webdriver知识 你将具备从头开始设计Page Object、Page Factory、DATADRIVEN等搭建自动化框架的能力 用100多个实例对Selenium现实场景应用进行深入理解 全面了解TestNG, Maven, Jenkins, HTML报告,多浏览器并行测试 了解数据库测试和使用Selenium进行性能测试 你将彻底了解testNG框架 你从网上随便选择一个网站,都可以实现自动化,用所有可能的测试用例进行自动化测试 将提高你的编码技能,以编写最优化的自动化测试用例代码 你基本可以搞定任何Selenium面试,并能从设计阶段开始领导整个Selenium自动化项目 你应该能够使用应用程序的GUI来验证数据完整性 你将能够创建漂亮的报告来打动客户或领导 更深入地理解自动化指南和代码质量标准 会附带一个练习网站,可以用上所有可用的WebDriver功能,实现自动化 【适合人群】 软件手动测试人员想转为自动化测试的人员 自动化软件测试人员想加强专业技能的 刚毕业学生想从事软件行业 QA 组长或项目经理 【课程优势】 学完课程以后,你将拥有完整的Selenium Webdriver知识 【讲师介绍】 资质介绍: 12年以上软件测试工作经验,其中7年以上自动化测试开发经验 新书“Python3+Selenium3自动化测试项目实战”作者

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

௸྄ིོུ倾心ღ᭄ᝰꫛꫀꪝ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值