selenium-java web自动化测试工具

本篇文章由来,这两天整理了下自己经常使用而且很熟练的项目,今天突然想起漏了一个,补上了,但想到还没对应的博客,那就写一个简单的

我经常使用且相对熟练的部分技术如下(不知道算不算各位大神眼中的辣鸡):

传统的web项目测试,大多数都依靠测试部门小伙伴人工操作,费时费力还容易侧漏`(*∩_∩*)′  错了 是漏测,

而selenium则为项目的测试提供了很大的便利,但并不是所有项目都适合,

哪些适合呢:公司自己的产品,且需要经常回归测试,比如类似OA这类业务系统产品

不适合的呢:比如外包业务,快速交付后就不管的,只需要测试一两次

案例一:一个简单示例(无弹出框这类单一页面),先看看执行效果(点击图片查看高清版本):

效果还满意么?满意请继续往下看,不满意请关闭窗口 谢谢

扫描二维码关注公众号,回复: 6269287 查看本文章

1.新建maven项目,引入selenium-java

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

2.写代码(因为自动化测试速度极快,每个步骤后我都稍微停顿了下,注释也很清晰明了)

package com.xiao.selenium;

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

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class AutoTest {
    public static void main(String[] args) throws InterruptedException {
        Thread.sleep(3000);
        //System.setProperty("webdriver.chrome.driver", "c://chromedriver.exe");
        //WebDriver webDriver = new ChromeDriver();
        System.setProperty("webdriver.gecko.driver", "c://geckodriver.exe");
        WebDriver webDriver = new FirefoxDriver();
        webDriver.manage().window().maximize();
        webDriver.manage().deleteAllCookies();
        // 与浏览器同步非常重要,必须等待浏览器加载完毕
        webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        
        //打开目标地址
        webDriver.get("http://192.168.0.32:88");
        //输入账号 密码并登陆系统
        Thread.sleep(1000);
        webDriver.findElement(By.xpath("/html/body/div/div/form/div[1]/div/div/input")).sendKeys("admin");
        webDriver.findElement(By.xpath("/html/body/div/div/form/div[2]/div/div/input")).sendKeys("123456");
        webDriver.findElement(By.cssSelector("html body div#app div.loginPage form.el-form.fromBox button.el-button.loginBtn.el-button--primary")).click();
        
        //选择系统
        Thread.sleep(1000);
        webDriver.findElement(By.xpath("/html/body/div/div/div/div[1]/p")).click();
        
        //展开基础信息管理菜单
        Thread.sleep(1000);
        webDriver.findElement(By.xpath("/html/body/div/div/div/div[1]/div[3]/ul/div[1]/li/div/span")).click();
        //点击科室管理菜单
        Thread.sleep(1000);
        webDriver.findElement(By.cssSelector(".is-opened > ul:nth-child(2) > li:nth-child(1)")).click();
        
        //跳转到第2页
        Thread.sleep(1000);
        webDriver.findElement(By.xpath("/html/body/div/div/div/div[2]/div[2]/div/div[2]/div[2]/div/div[2]/ul/li[2]")).click();
        
        //点击新增按钮
        webDriver.findElement(By.xpath("/html/body/div/div/div/div[2]/div[2]/div/div[2]/div[1]/div/div[1]/div[1]/button")).click();
        
        //根据规则随机生成文本框内容
        int random = new Random().nextInt(200000000);
        Thread.sleep(1000);
        webDriver.findElement(By.xpath("/html/body/div[1]/div/div/div[2]/div[2]/div/div[3]/div/div[2]/form/div[1]/div[1]/div/div/input")).sendKeys(String.valueOf(random));
        Thread.sleep(1000);
        webDriver.findElement(By.xpath("/html/body/div[1]/div/div/div[2]/div[2]/div/div[3]/div/div[2]/form/div[1]/div[2]/div/div[1]/input")).sendKeys("自动化测试-"+random);
        
        //展开下拉框
        webDriver.findElement(By.xpath("/html/body/div[1]/div/div/div[2]/div[2]/div/div[3]/div/div[2]/form/div[2]/div[1]/div/div/div[1]/span/span/i")).click();
        Thread.sleep(1000);
        //获取下拉框size
        List<WebElement> select1 = webDriver.findElements(By.cssSelector("div.el-select-dropdown:nth-child(4) > div:nth-child(1) > div:nth-child(1) > ul li"));
        //随机选择一个项目
        int selectItem1 = new Random().nextInt(select1.size())+1;
        webDriver.findElement(By.xpath("/html/body/div[3]/div[1]/div[1]/ul/li["+selectItem1+"]")).click();
        
        //稍作停顿,然后保存
        Thread.sleep(1000);
        webDriver.findElement(By.xpath("/html/body/div[1]/div/div/div[2]/div[2]/div/div[3]/div/div[3]/div/button[1]")).click();
        
        //跳转到我的博客
        Thread.sleep(3000);
        webDriver.get("https://www.cnblogs.com/xiaochangwei");
        
        webDriver.findElements(By.className("postTitle")).forEach(x -> {
            System.out.println(x.getText());
        });
        
        Thread.sleep(1000);
        //打开标题为 通过Dockerfile构建镜像并发布web项目 的文章
        webDriver.findElement(By.partialLinkText("通过Dockerfile构建镜像并发布web项目")).click();
        
        Thread.sleep(1000);
        //移动到底部
        //((JavascriptExecutor) webDriver).executeScript("window.scrollTo(0, document.body.scrollHeight)");
        //移动到指定的坐标(相对当前的坐标移动)  
        ((JavascriptExecutor) webDriver).executeScript("window.scrollBy(0, 700)");  
        Thread.sleep(1000);
        //移动到窗口绝对位置坐标,如下移动到纵坐标1600像素位置  
        ((JavascriptExecutor) webDriver).executeScript("window.scrollTo(0, 1600)");  
        Thread.sleep(1000);
        //移动到指定元素,且元素底部和窗口底部对齐 参考 https://www.cnblogs.com/testway/p/6693140.html
        ((JavascriptExecutor) webDriver).executeScript("arguments[0].scrollIntoView(false);", webDriver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/div/div[1]/div/div/div[2]/div[4]/div[3]/div[1]/a[5]/img")));
        //暂停五秒钟后关闭
        Thread.sleep(2000);
        webDriver.quit();
    }
}

  其中: 

    16行谷歌浏览器的驱动下载地址:https://chromedriver.storage.googleapis.com/index.html

    18行火狐浏览器的驱动下载地址:https://github.com/mozilla/geckodriver/releases

  然后运行,你就可以为所欲为了

 案列二:有弹出框及使用了Frame的页面,以百度盘登录为例

    

  代码如下: 

package com.xiao.selenium;
 
import java.util.concurrent.TimeUnit;
 
import org.openqa.selenium.By;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
 
/**
 * @Title: com.xiao.selenium.BaiduPanLogin.java
 * @Description:
 * @author [email protected]
 * @date 2018年2月5日 上午9:16:44
 * @version V1.0
 */
public class BaiduLogin {
 
    public static void main(String[] args) throws InterruptedException {
        Thread.sleep(3000);
        System.setProperty("webdriver.gecko.driver", "c://geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
//      driver.manage().window().maximize();
        driver.manage().window().setPosition(new Point(100, 50));
        driver.manage().deleteAllCookies();
        // 与浏览器同步非常重要,必须等待浏览器加载完毕
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 
        driver.get("https://pan.baidu.com/");
 
        Thread.sleep(1000);
 
        WebElement qqLoginLink = driver
                .findElement(By.xpath("/html/body/div[1]/div[3]/div[6]/div/div[4]/div[2]/div/ul/li[2]/a"));
        qqLoginLink.click();
        Thread.sleep(1000);
 
        // 获取当前页面句柄
        String handle = driver.getWindowHandle();
        // 获取所有页面的句柄,并循环判断不是当前的句柄 然后切换到子窗体
        for (String handles : driver.getWindowHandles()) {
            if (handles.equals(handle))
                continue;
            driver.switchTo().window(handles);
        }
 
        // 由于登录输入框在frame中,还需要先切换进入frame,否则,也找不到输入框的
        driver.switchTo().frame(driver.findElement(By.xpath("//*[@id='ptlogin_iframe']")));
 
        // 调试过程中,如果提示找不到元素,不知道是否切换成功了,可以把当前handler的source打印出来看看
        // System.out.println(driver.getPageSource());
 
        driver.findElement(By.xpath("//*[@id='switcher_plogin']")).click();
        driver.findElement(By.xpath("//*[@id='u']")).sendKeys("317409898");
        driver.findElement(By.xpath("//*[@id='p']")).sendKeys("xxxxxxxxx");
        driver.findElement(By.xpath("//*[@id='login_button']")).click();
         
        //由于我的账号没绑定手机,点登录后会有个提示,如果直接关闭,可能被判断为还没完成登录,没有会话,所以稍等片刻
        Thread.sleep(2000);
         
        //关闭弹出的子窗体
        driver.close();
         
        //driver.navigate() 下有很多方法,比如后退,刷新等
        Thread.sleep(2000);
    }
 
}
   

注意:1.我这里只开了两个窗口,如果是多个,而且需要在窗口间切换操作的,注意在一个窗口中完成操作后,确认是否切回到需要操作的窗口

       2.页面内部有frame的,除了切换到具体页面外,还要切换到对应的frame中才行

       3.关闭窗口时,也要确认操作的对象

原文地址:https://www.cnblogs.com/xiaochangwei/p/8405591.html

本篇文章由来,这两天整理了下自己经常使用而且很熟练的项目,今天突然想起漏了一个,补上了,但想到还没对应的博客,那就写一个简单的

我经常使用且相对熟练的部分技术如下(不知道算不算各位大神眼中的辣鸡):

传统的web项目测试,大多数都依靠测试部门小伙伴人工操作,费时费力还容易侧漏`(*∩_∩*)′  错了 是漏测,

而selenium则为项目的测试提供了很大的便利,但并不是所有项目都适合,

哪些适合呢:公司自己的产品,且需要经常回归测试,比如类似OA这类业务系统产品

不适合的呢:比如外包业务,快速交付后就不管的,只需要测试一两次

案例一:一个简单示例(无弹出框这类单一页面),先看看执行效果(点击图片查看高清版本):

效果还满意么?满意请继续往下看,不满意请关闭窗口 谢谢

1.新建maven项目,引入selenium-java

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

2.写代码(因为自动化测试速度极快,每个步骤后我都稍微停顿了下,注释也很清晰明了)

package com.xiao.selenium;

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

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class AutoTest {
    public static void main(String[] args) throws InterruptedException {
        Thread.sleep(3000);
        //System.setProperty("webdriver.chrome.driver", "c://chromedriver.exe");
        //WebDriver webDriver = new ChromeDriver();
        System.setProperty("webdriver.gecko.driver", "c://geckodriver.exe");
        WebDriver webDriver = new FirefoxDriver();
        webDriver.manage().window().maximize();
        webDriver.manage().deleteAllCookies();
        // 与浏览器同步非常重要,必须等待浏览器加载完毕
        webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        
        //打开目标地址
        webDriver.get("http://192.168.0.32:88");
        //输入账号 密码并登陆系统
        Thread.sleep(1000);
        webDriver.findElement(By.xpath("/html/body/div/div/form/div[1]/div/div/input")).sendKeys("admin");
        webDriver.findElement(By.xpath("/html/body/div/div/form/div[2]/div/div/input")).sendKeys("123456");
        webDriver.findElement(By.cssSelector("html body div#app div.loginPage form.el-form.fromBox button.el-button.loginBtn.el-button--primary")).click();
        
        //选择系统
        Thread.sleep(1000);
        webDriver.findElement(By.xpath("/html/body/div/div/div/div[1]/p")).click();
        
        //展开基础信息管理菜单
        Thread.sleep(1000);
        webDriver.findElement(By.xpath("/html/body/div/div/div/div[1]/div[3]/ul/div[1]/li/div/span")).click();
        //点击科室管理菜单
        Thread.sleep(1000);
        webDriver.findElement(By.cssSelector(".is-opened > ul:nth-child(2) > li:nth-child(1)")).click();
        
        //跳转到第2页
        Thread.sleep(1000);
        webDriver.findElement(By.xpath("/html/body/div/div/div/div[2]/div[2]/div/div[2]/div[2]/div/div[2]/ul/li[2]")).click();
        
        //点击新增按钮
        webDriver.findElement(By.xpath("/html/body/div/div/div/div[2]/div[2]/div/div[2]/div[1]/div/div[1]/div[1]/button")).click();
        
        //根据规则随机生成文本框内容
        int random = new Random().nextInt(200000000);
        Thread.sleep(1000);
        webDriver.findElement(By.xpath("/html/body/div[1]/div/div/div[2]/div[2]/div/div[3]/div/div[2]/form/div[1]/div[1]/div/div/input")).sendKeys(String.valueOf(random));
        Thread.sleep(1000);
        webDriver.findElement(By.xpath("/html/body/div[1]/div/div/div[2]/div[2]/div/div[3]/div/div[2]/form/div[1]/div[2]/div/div[1]/input")).sendKeys("自动化测试-"+random);
        
        //展开下拉框
        webDriver.findElement(By.xpath("/html/body/div[1]/div/div/div[2]/div[2]/div/div[3]/div/div[2]/form/div[2]/div[1]/div/div/div[1]/span/span/i")).click();
        Thread.sleep(1000);
        //获取下拉框size
        List<WebElement> select1 = webDriver.findElements(By.cssSelector("div.el-select-dropdown:nth-child(4) > div:nth-child(1) > div:nth-child(1) > ul li"));
        //随机选择一个项目
        int selectItem1 = new Random().nextInt(select1.size())+1;
        webDriver.findElement(By.xpath("/html/body/div[3]/div[1]/div[1]/ul/li["+selectItem1+"]")).click();
        
        //稍作停顿,然后保存
        Thread.sleep(1000);
        webDriver.findElement(By.xpath("/html/body/div[1]/div/div/div[2]/div[2]/div/div[3]/div/div[3]/div/button[1]")).click();
        
        //跳转到我的博客
        Thread.sleep(3000);
        webDriver.get("https://www.cnblogs.com/xiaochangwei");
        
        webDriver.findElements(By.className("postTitle")).forEach(x -> {
            System.out.println(x.getText());
        });
        
        Thread.sleep(1000);
        //打开标题为 通过Dockerfile构建镜像并发布web项目 的文章
        webDriver.findElement(By.partialLinkText("通过Dockerfile构建镜像并发布web项目")).click();
        
        Thread.sleep(1000);
        //移动到底部
        //((JavascriptExecutor) webDriver).executeScript("window.scrollTo(0, document.body.scrollHeight)");
        //移动到指定的坐标(相对当前的坐标移动)  
        ((JavascriptExecutor) webDriver).executeScript("window.scrollBy(0, 700)");  
        Thread.sleep(1000);
        //移动到窗口绝对位置坐标,如下移动到纵坐标1600像素位置  
        ((JavascriptExecutor) webDriver).executeScript("window.scrollTo(0, 1600)");  
        Thread.sleep(1000);
        //移动到指定元素,且元素底部和窗口底部对齐 参考 https://www.cnblogs.com/testway/p/6693140.html
        ((JavascriptExecutor) webDriver).executeScript("arguments[0].scrollIntoView(false);", webDriver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/div/div[1]/div/div/div[2]/div[4]/div[3]/div[1]/a[5]/img")));
        //暂停五秒钟后关闭
        Thread.sleep(2000);
        webDriver.quit();
    }
}

  其中: 

    16行谷歌浏览器的驱动下载地址:https://chromedriver.storage.googleapis.com/index.html

    18行火狐浏览器的驱动下载地址:https://github.com/mozilla/geckodriver/releases

  然后运行,你就可以为所欲为了

 案列二:有弹出框及使用了Frame的页面,以百度盘登录为例

    

  代码如下: 

package com.xiao.selenium;
 
import java.util.concurrent.TimeUnit;
 
import org.openqa.selenium.By;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
 
/**
 * @Title: com.xiao.selenium.BaiduPanLogin.java
 * @Description:
 * @author [email protected]
 * @date 2018年2月5日 上午9:16:44
 * @version V1.0
 */
public class BaiduLogin {
 
    public static void main(String[] args) throws InterruptedException {
        Thread.sleep(3000);
        System.setProperty("webdriver.gecko.driver", "c://geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
//      driver.manage().window().maximize();
        driver.manage().window().setPosition(new Point(100, 50));
        driver.manage().deleteAllCookies();
        // 与浏览器同步非常重要,必须等待浏览器加载完毕
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 
        driver.get("https://pan.baidu.com/");
 
        Thread.sleep(1000);
 
        WebElement qqLoginLink = driver
                .findElement(By.xpath("/html/body/div[1]/div[3]/div[6]/div/div[4]/div[2]/div/ul/li[2]/a"));
        qqLoginLink.click();
        Thread.sleep(1000);
 
        // 获取当前页面句柄
        String handle = driver.getWindowHandle();
        // 获取所有页面的句柄,并循环判断不是当前的句柄 然后切换到子窗体
        for (String handles : driver.getWindowHandles()) {
            if (handles.equals(handle))
                continue;
            driver.switchTo().window(handles);
        }
 
        // 由于登录输入框在frame中,还需要先切换进入frame,否则,也找不到输入框的
        driver.switchTo().frame(driver.findElement(By.xpath("//*[@id='ptlogin_iframe']")));
 
        // 调试过程中,如果提示找不到元素,不知道是否切换成功了,可以把当前handler的source打印出来看看
        // System.out.println(driver.getPageSource());
 
        driver.findElement(By.xpath("//*[@id='switcher_plogin']")).click();
        driver.findElement(By.xpath("//*[@id='u']")).sendKeys("317409898");
        driver.findElement(By.xpath("//*[@id='p']")).sendKeys("xxxxxxxxx");
        driver.findElement(By.xpath("//*[@id='login_button']")).click();
         
        //由于我的账号没绑定手机,点登录后会有个提示,如果直接关闭,可能被判断为还没完成登录,没有会话,所以稍等片刻
        Thread.sleep(2000);
         
        //关闭弹出的子窗体
        driver.close();
         
        //driver.navigate() 下有很多方法,比如后退,刷新等
        Thread.sleep(2000);
    }
 
}
   

注意:1.我这里只开了两个窗口,如果是多个,而且需要在窗口间切换操作的,注意在一个窗口中完成操作后,确认是否切回到需要操作的窗口

       2.页面内部有frame的,除了切换到具体页面外,还要切换到对应的frame中才行

       3.关闭窗口时,也要确认操作的对象

原文地址:https://www.cnblogs.com/xiaochangwei/p/8405591.html

猜你喜欢

转载自www.cnblogs.com/tiechui2015/p/10917540.html