• 全自动测试-2-测试页面添加搜索


    需求

    使用Selenium写一个脚本页面

    代码

    正常写页面都使用 name id xpath 这些元素定位

    1.xpath获取方式
    在这里插入图片描述
    在这里插入图片描述

    2.使用方法 详情百度Selenium使用就有更多方法了
    //输入
    driver.findElement(new By.ByXPath(“xpath”)).sendKeys(“你好”);
    //点击
    driver.findElement(new By.ByXPath(“xpath”)).click();

    3.发现的问题

    但是我发现页面加载需要时间,页面没加载完成方法就走完了,点击事件找不到就报错了

    问题解决方法

    //点击,输入事件方法前加一个睡眠等待页面加载完即可
    Thread.sleep(1000);
    
    • 1
    • 2

    4.问题优化

    虽然使用了强制等待但是每个页面每个接口等待时间都不一样,而且这样等待的时间太长,这时候我去找了找方法发,发现一个隐式等待和显式等待

    问题优化解决

    创建一个SeleniumUtil 类 我用的是显示等待,WebDriverWait(驱动,等待时间)
    这样5秒内找到了就返回true未找到就返回false,这个方法在5秒内每0.5秒去找一次,直到发现为止

    //点击事件
    public static boolean click(WebDriver driver, String Xpath) {
            try {
                new WebDriverWait(driver, 5).until(ExpectedConditions.presenceOfElementLocated(new By.ByXPath(Xpath))).click();
            }catch (Exception e){
                return false;
            }
            return true;
        }
    
    //输入事件
    public static boolean sendKeys(WebDriver driver, String Xpath, String value){
            try {
                new WebDriverWait(driver, 5).until(ExpectedConditions.presenceOfElementLocated(new By.ByXPath(Xpath))).sendKeys(value);
            }catch (Exception e){
                return false;
            }
            return true;
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    完整代码

    package com.selenium.demo;
    
    import com.selenium.demo.Util.SeleniumUtil;
    import org.openqa.selenium.*;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    
    
    public class SeleniumDemo {
    
        //访问地址
        private static final String url = "http://localhost/auto";
    
        public static void main(String[] args) throws InterruptedException {
    
            //设置驱动,后面的路径自己要选择正确,也可以放在本地
            System.setProperty("webdriver.chrome.driver", "D:\\WORK\\selenium\\chromedriver.exe");
            //创建参数配置
            ChromeOptions options = new ChromeOptions();
            //配置远程地址
            options.setExperimentalOption("debuggerAddress", "127.0.0.1:9222");
            //加载驱动
            WebDriver driver = new ChromeDriver(options);
            //打开页面
            driver.get(url);
    
            //点击新增
            SeleniumUtil.click(driver,"//*[@id=\"app\"]/div/div[2]/section/div/div[1]/div[1]/button");
            //输入名称
            SeleniumUtil.sendKeys(driver,"/html/body/div[2]/div/div[2]/form/div[1]/div/div/input","李四");
            //点击性别下拉
            SeleniumUtil.click(driver,"/html/body/div[2]/div/div[2]/form/div[2]/div/div/div[1]/input");
            //选择男
            SeleniumUtil.click(driver,"/html/body/div[4]/div[1]/div[1]/ul/li[1]");
            //输入年龄
            SeleniumUtil.sendKeys(driver,"/html/body/div[2]/div/div[2]/form/div[3]/div/div/input","18");
            //点击确定
            SeleniumUtil.click(driver,"/html/body/div[2]/div/div[3]/div/button[1]");
            //表单输入李四名称
            SeleniumUtil.sendKeys(driver,"//*[@id=\"app\"]/div/div[2]/section/div/form/div[1]/div/div/input","李四");
            //点击搜索
            SeleniumUtil.click(driver,"//*[@id=\"app\"]/div/div[2]/section/div/form/div[4]/div/button[1]");
            //点击删除
            SeleniumUtil.click(driver,"//*[@id=\"app\"]/div/div[2]/section/div/div[2]/div[3]/table/tbody/tr/td[5]/div/button[2]");
            //确定删除
            SeleniumUtil.click(driver,"/html/body/div[4]/div/div[3]/button[2]");
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    视频演示

  • 相关阅读:
    反向迭代器---迭代器适配器
    SQLSERVER 临时表和表变量到底有什么区别?
    外贸干货/与非洲客户打交道要知道的几点
    D. AND, OR and square sum(二进制位+贪心)
    五一“捡钱”,就在这几天!国债逆回购最佳时点来了,如何躺赚6天利息?来看操作攻略
    Android原生数据库的基本使用和升级
    深入理解计算机系统——第九章 Virtual Memory
    java计算机毕业设计高校车辆管理系统源码+mysql数据库+系统+lw文档+部署
    Ansible数组同步至Shell脚本数组中
    java零散知识点复习--基础知识
  • 原文地址:https://blog.csdn.net/zuichu_2001/article/details/126586368