自动化测试--WebDriver API

1. 元素定位方法

  1. 通过 ID 定位:如果元素具有唯一的 ID 属性,可以使用 findElement(By.id("elementId")) 方法来定位元素。
  2. 通过 Name 定位:使用 findElement(By.name("elementName")) 来查找具有指定名称的元素。
  3. 通过 Class Name 定位:通过 findElement(By.className("className")) 定位具有特定类名的元素。
  4. 通过 Tag Name 定位:例如 findElement(By.tagName("input")) ,可根据元素的标签名进行定位。
  5. 通过 Link Text 定位:对于链接元素,可使用 findElement(By.linkText("链接文本")) 。
  6. 通过 Partial Link Text 定位:如果只知道链接文本的一部分,使用 findElement(By.partialLinkText("部分链接文本")) 。
  7. 通过 CSS 选择器定位:findElement(By.cssSelector("#idValue")) 或者 .className 等 CSS 选择器规则。
  8. 通过 XPath 定位:这是一种非常强大和灵活的定位方式,例如 findElement(By.xpath("//input[@id='elementId']")) 。

2. 操作测试对象

  1. 点击元素:找到元素后,使用 click() 方法进行点击操作,例如 element.click() 
  2. 输入文本:使用 sendKeys() 方法向输入框输入文本,如 element.sendKeys("输入的文本") 
  3. 提交表单:对于表单元素,可以使用 submit() 方法提交表单,例如 formElement.submit() 
  4. 获取元素文本:通过 getText() 方法获取元素的显示文本,如 element.getText() 
  5. 获取html属性:通过getAttribute()方法获取对应的属性值
  6. 获取页面标题:通过getTitle()方法
  7. 获取页面url:通过getCurrentUrl()方法

3. 添加等待 

如果在进行搜索后马上对结果进行检查,页面可能还没有加载完成,此时检查可能会误判,可以添加等待来等待页面加载完成后再检查。

sleep休眠:只需调用Thread类中的sleep方法即可:

package org.example;

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

import java.util.List;

import static java.lang.Thread.sleep;

public class Main {
    public static void main(String[] args) throws InterruptedException {
        test1();
    }
    public static void test1() throws InterruptedException {
        //启动chrome浏览器
        WebDriver webDriver = new ChromeDriver();

        //打开百度
        webDriver.get("https://www.baidu.com");

        //定位到输入框
        WebElement input = webDriver.findElement(By.name("wd"));
        input.sendKeys("自动化测试");

        //定位搜索按钮
        WebElement button = webDriver.findElement(By.id("su"));
        button.click();

        sleep(2000);

        //检查搜索结果
        List<WebElement> elements = webDriver.findElements(By.tagName("em"));
        for(WebElement element : elements) {
            if(element.getText().equals("自动化测试")) {
                System.out.println("测试通过");
            }else{
                System.out.println("测试失败");
            }
        }
    }
}

 隐式等待:

package org.example;

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

import java.util.List;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

public class Main {
    public static void main(String[] args) throws InterruptedException {
        test1();
    }
    public static void test1() throws InterruptedException {
        //启动chrome浏览器
        WebDriver webDriver = new ChromeDriver();

        //打开百度
        webDriver.get("https://www.baidu.com");

        //定位到输入框
        WebElement input = webDriver.findElement(By.name("wd"));
        input.sendKeys("自动化测试");

        //定位搜索按钮
        WebElement button = webDriver.findElement(By.id("su"));
        button.click();

        //sleep(2000);

        //隐式等待
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

        //检查搜索结果
        List<WebElement> elements = webDriver.findElements(By.tagName("em"));
        for(WebElement element : elements) {
            if(element.getText().equals("自动化测试")) {
                System.out.println("测试通过");
            }else{
                System.out.println("测试失败");
            }
        }
    }
}

通过隐式等待,会等待页面所有元素加载完成后停止等待,如果设置时间内页面没有加载完成,则会抛出异常。

显式等待:

package org.example;

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.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.List;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

public class Main {
    public static void main(String[] args) throws InterruptedException {
        test1();
    }
    public static void test1() throws InterruptedException {
        //启动chrome浏览器
        WebDriver webDriver = new ChromeDriver();

        //打开百度
        webDriver.get("https://www.baidu.com");

        //定位到输入框
        WebElement input = webDriver.findElement(By.name("wd"));
        input.sendKeys("自动化测试");

        //定位搜索按钮
        WebElement button = webDriver.findElement(By.id("su"));
        button.click();

        //sleep(2000);

        //隐式等待
        //webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

        WebDriverWait wait = new WebDriverWait(webDriver, 30);
        //判断元素是否可以点击
        //wait.until(ExpectedConditions.elementToBeClickable(By.tagName("em")));

        //判断元素值是否等于"自动化测试"
        wait.until(ExpectedConditions.textToBe(By.tagName("em"), "自动化测试"));

        //检查搜索结果
        List<WebElement> elements = webDriver.findElements(By.tagName("em"));
        for(WebElement element : elements) {
            if(element.getText().equals("自动化测试")) {
                System.out.println("测试通过");
            }else{
                System.out.println("测试失败");
            }
        }
    }
}

4. 浏览器操作

设置浏览器窗口大小:

    public static void test4() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        //浏览器最大化
        webDriver.manage().window().maximize();
        sleep(2000);
        //设置浏览器窗口大小
        webDriver.manage().window().setSize(new Dimension(500, 500));
    }

 控制浏览器前进后退页面:

    public static void test5() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.id("kw")).sendKeys("软件测试");
        webDriver.findElement(By.id("su")).click();
        sleep(2000);
        //返回
        webDriver.navigate().back();
        sleep(2000);
        //前进
        webDriver.navigate().forward();
        sleep(2000);
        //去指定页面
        webDriver.navigate().to("https://www.baidu.com");
    }

控制浏览器滚动条:

    public static void test6() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.id("kw")).sendKeys("软件测试");
        webDriver.findElement(By.id("su")).click();
        sleep(2000);
        //设置滚动条到最底部
        ((JavascriptExecutor)webDriver).executeScript("document.documentElement.scrollTop=10000");
        sleep(2000);
        //设置滚动条到最顶端
        ((JavascriptExecutor)webDriver).executeScript("document.documentElement.scrollTop=0");
    }

5. 控制键盘鼠标

 控制键盘:

    public static void test7() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        //点击ABC
        webDriver.findElement(By.id("kw")).sendKeys("ABC");
        //点击回车
        webDriver.findElement(By.id("kw")).sendKeys(Keys.ENTER);
        //点击 ctrl + A
        webDriver.findElement(By.id("kw")).sendKeys(Keys.CONTROL, "A");
        //点击 ctrl + X
        webDriver.findElement(By.id("kw")).sendKeys(Keys.CONTROL, "X");
    }

控制鼠标移动和点击:

    public static void test8() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        WebElement element = webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)"));
        //把鼠标移动到这个位置上右击,使用perform方法使点击效果能被看到
        Actions action = new Actions(webDriver);
        action.moveToElement(element).contextClick().perform();
    }

6. 切换窗口

当我们进行某个操作时,可能会打开一个新的页面,我们想要查找或操作这个新页面中的元素,需要先切换到这个页面,例如我们打开百度点击左上角的新闻:

就会出现一个新的页面:

我们想在这个页面进行测试就需要切换窗口:

    public static void test9() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        //点击新闻
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();

        //获取当前浏览器中的所有页面的窗口句柄
        Set<String> handles = webDriver.getWindowHandles();
        String target = "";
        for(String s : handles) {
            target = s;
        }
        //把窗口句柄切换为最后一个窗口
        webDriver.switchTo().window(target);
        webDriver.findElement(By.cssSelector("#ww")).sendKeys("特朗普");
        webDriver.findElement(By.cssSelector("#s_btn_wr")).click();

    }

7. 截图

截图需要引入对应的依赖:

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.11.0</version>
</dependency>

 8. 定位嵌套页面中的元素

有的页面中可能存在另一个页面,想要定位这个页面中的元素,也需要先切换窗口:

 例如我想点击这个click

这里的click在iframe标签下,也就是内部页面中,我就需要先切换窗口再定位:

    public static void test11() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("C:\\Users\\22759\\Desktop\\spring\\AutoTest20240719\\src\\main\\resources\\page\\inner.html");

        //切换到frame
        webDriver.switchTo().frame("f1");
        sleep(2000);
        //点击click
        webDriver.findElement(By.cssSelector("body > div > div > a")).click();
    }

9. 定位下拉框 

有些页面中可能会存在下拉框,定位下拉框中的元素需要使用Select对象:

 

    public static void test12() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("C:\\Users\\22759\\Desktop\\spring\\AutoTest20240719\\src\\main\\resources\\page\\test04.html");

        //定位到select标签元素
        WebElement element = webDriver.findElement(By.cssSelector("#ShippingMethod"));
        //创建Select对象
        Select select = new Select(element);
        //通过序号选择,序号从0开始
        select.selectByIndex(2);
        sleep(3000);
        //通过选项value值选择
        select.selectByValue("9.25");
    }

10. 定位弹窗

有的页面会出现弹窗,需要进行点击:

 

    public static void test13() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("C:\\Users\\22759\\Desktop\\spring\\AutoTest20240719\\src\\main\\resources\\page\\test0502.html");
        webDriver.findElement(By.cssSelector("input")).click();

        //切换到弹窗,输入内容
        webDriver.switchTo().alert().sendKeys("Ting");
        sleep(2000);
        //点击确定
        webDriver.switchTo().alert().accept();

        //点击取消
        //webDriver.switchTo().alert().dismiss();
    }

 11. 上传文件

    public static void test14() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("C:\\Users\\22759\\Desktop\\spring\\AutoTest20240719\\src\\main\\resources\\page\\test07.html");
        webDriver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

        //上传文件
        webDriver.findElement(By.cssSelector("body > div > div > input[type=file]")).sendKeys("C:\\Users\\22759\\Desktop\\spring\\AutoTest20240719\\src\\main\\resources\\page\\test07.html");
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Ting-yu

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

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

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

打赏作者

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

抵扣说明:

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

余额充值