selenium软件使用教程,基于java-idea、maven

selenium安装

  1. java JDK 1.8
  2. idea 社区版
  3. maven 配置阿里云镜像资源库

第一段java-selenium测试代码

创建maven项目配置拍pom文件依赖

如果找不到对应版本,可以去中央镜像网站搜索:https://mvnrepository.com/ (网址我放这儿了)

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

        <!-- https://mvnrepository.com/artifact/org.testng/testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.4.0</version>
        </dependency>

    </dependencies>

通过chromedriver(谷歌浏览器驱动文件打开百度)

package com.abtest;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

import java.nio.file.Path;
import java.nio.file.Paths;

public class TestCase {
    public WebDriver webDriver;

    @Test
    public void openBaidu(){
        // 找到浏览器驱动
        Path p1 = Paths.get("src","drivers","chromedriver.exe");
        // 将其设置到系统中
        System.setProperty("webdriver.chrome.driver",p1.toAbsolutePath().toString());
        // 创建一个浏览器实例
        webDriver = new ChromeDriver();
        // 打开百度
        webDriver.get("https://www.baidu.com");
    }
}

规范化第一段java-selenium代码

这里的规范化主要是将测试的三个时期进行了规范处理。同时在测试过程中使用了通过id定位web组件(元素)

package com.abtest;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.nio.file.Path;
import java.nio.file.Paths;

public class TestCase {
    public WebDriver webDriver;

    // 对整个测试类运行之前所做的操作
    @BeforeClass
    public void setUpEnv(){
        // 找到浏览器驱动
        Path p1 = Paths.get("src","drivers","chromedriver.exe");
        // 将其设置到系统中
        System.setProperty("webdriver.chrome.driver",p1.toAbsolutePath().toString());
        // 创建一个浏览器实例
        webDriver = new ChromeDriver();
    }


    @Test
    public void openBaidu() throws InterruptedException {
        // 打开百度
        webDriver.get("https://www.baidu.com");

        // 使用By.id获取元素
        webDriver.findElement(By.id("kw")).sendKeys("Helloword");
        webDriver.findElement(By.id("su")).click();

        // 线程休眠3000毫秒,这里需要使用异常处理,否则会报错(重要)
        Thread.sleep(3000);
    }

    @AfterClass
    public void tearDownEnv(){
        // 关闭浏览器
        webDriver.quit();
    }
}

元素定位方式

通过name或者class对web元素进行定位

几乎和通过id定位元素的方式同理

@Test
    public void testName() throws InterruptedException {
        webDriver.findElement(By.name("wd")).sendKeys("HelloWorld");
        webDriver.findElement(By.className("bg")).click();
        Thread.sleep(3000);
    }
通过xpath对元素进行定位

另外xpath表达式可以在网页上右键-检查-找到相应组件-右键-copy-copyXpath

@Test
    public void testXpath() throws InterruptedException {
        // 这里增加3秒延迟时间保证元素能被刷出来
        // 否则会出现错误信息:"org.openqa.selenium.ElementNotInteractableException: element not interactable"
        Thread.sleep(3000);
        // 这里我重新找了一个网页:https://cn.bing.com/
        webDriver.findElement(By.xpath("//*[@id=\"images\"]/a")).click();
        Thread.sleep(3000);
    }

模拟鼠标滑动事件

// 模拟鼠标滑动事件
    @Test
    public void testAction(){
        Actions actions = new Actions(webDriver);
        // 第一步找到 链接按钮
        WebElement moreBtn = webDriver.findElement(By.xpath("//*[@id=\"s-top-left\"]/div/a"));
        // 将鼠标移动到链接按钮
        actions.moveToElement(moreBtn);
        // 找到展示出来的新元素
        WebElement mp3Link = webDriver.findElement(By.xpath("//*[@id=\"s-top-more\"]/div[8]/a/img"));
        // 将鼠标移动到链接按钮
        actions.moveToElement(mp3Link).click().perform();
    }

Action拖拽百度地图规划路线

@Test
    public void testDragandDrop() throws InterruptedException {
        // 跳转到百度地图页面
        webDriver.get("https://map.baidu.com/");
        Thread.sleep(3000);
        // 找到地图的mask组件
        WebElement mask = webDriver.findElement(By.id("mask"));
        // 添加事件处理
        Actions actions = new Actions(webDriver);
        // 右键之后等待1秒延迟
        actions.moveToElement(mask).contextClick().pause(1000);
        // 设置起点
        WebElement cmitem_start = webDriver.findElement(By.id("cmitem_start"));

        actions.moveToElement(cmitem_start).click();
        // 拖拽地图
            // 按下鼠标左键不松开
        actions.clickAndHold();
            // 按偏移量进行拖拽,参数分别为x偏移量;y偏移量
        actions.moveByOffset(200,100);
            // 松开鼠标
        actions.release();
        // 右键之后等待1秒延迟
        actions.contextClick().pause(1000);
        // 设置终点
        WebElement cmitem_end = webDriver.findElement(By.id("cmitem_end"));
        actions.moveToElement(cmitem_end).click();

        // 执行以上action中的所有动作
        actions.perform();
    }

谷歌浏览器做窗口切换

@Test
    public void testSwitchWindows() throws InterruptedException {
        webDriver.findElement(By.xpath("//*[@id=\"s-top-left\"]/a[6]")).click();
        // 获取当前窗口
        String currentWindow = webDriver.getWindowHandle();
        // 获取所有的窗口
        Set<String> allWindows = webDriver.getWindowHandles();

        // 用加强for循环遍历所有的window
        for(String window:allWindows){
            // 如果新的窗口不是当前的窗口
            if(window != currentWindow){
                // 将窗口切换进去
                webDriver.switchTo().window(window);
            }
        }

        //Thread.sleep(10);
        webDriver.findElement(By.id("kw")).sendKeys("美女");

        webDriver.findElement(By.xpath("//*[@id=\"homeSearchForm\"]/span[2]/input")).click();

        webDriver.switchTo().window(currentWindow);

        webDriver.findElement(By.id("kw")).sendKeys("邯郸埋尸案");
    }

frame组件定位

@Test
    public void testSwitchFrame() throws InterruptedException {
        // 跳转到我们有frame组件的网页
        webDriver.get("https://login.anjuke.com/login/form");
        // 找到我们需要的frame组件
        WebElement iframe = webDriver.findElement(By.id("iframeLoginIfm"));
        // 切换到我们的frame组件位置
        webDriver.switchTo().frame(iframe);
        // 找到frame组件中的输入框然后输入对应字符信息
        webDriver.findElement(By.id("phoneIpt")).sendKeys("13212341234");
    }

设置隐式的最长等待时间(不推荐)

隐式等待时间表示的是全局的等待时间:无论是找到还是找不到都会等待那么多时间

缺点:让测试项目整体的等待时间变长

@Test
public void testImplicity(){
    // 设置最长隐式等待时间为10s
    webDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(20));
    
    WebElement frame = webDriver.findElement(By.id("layui-layer-iframe1"));
    webDriver.switchTo().frame(frame);
    webDriver.findElement(By.id("mobilephone")).sendKeys("1234567890");
}

设置显示的等待时间(推荐)

@Test
public void testTimeOut(){
    // 设置最长显示等待时间为20秒
    WebDriverWait wait = new WebDriverWait(webDriver,20);
    // 设置提前结束等待条件
    WebElement frame = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("layui-layer-iframe1")));

    webDriver.switchTo().frame(frame);
    webDriver.findElement(By.id("mobilephone")).sendKeys("1234567890");

}
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值