• 软件测试Lab2————用selenium进行自动化测试


    本次实验内容为用selenium进行自动化测试

    主要分为以下几个步骤:

    1、安装SeleniumIDE插件

    2、学会使用SeleniumIDE录制脚本和导出脚本

    3、访问http://www.ncfxy.com使用学号登录系统(账户名为学号,密码为学号后6位),进入系统后可以看到该用户的邮箱。

    4、编写Selenium Java WebDriver程序,测试info.csv表格中的学号和邮箱的对应关系是否正确。

    5、将测试代码提交到github上。

    一、安装SeleniumIDE插件

    SeleniumIDE是Firefox的附加组件之一,首先我们要下载这个插件

    我们点击Firefox浏览器的菜单,选择附加组件

    在右上角的搜索框中搜索Selenium IDE

    我们安装使用用户最多排序,第一个就是我们需要安装的插件

    我们安装组件并重启之后,就可以正常使用这个组件了

    二、学会使用SeleniumIDE录制脚本和导出脚本

     我们首先点击菜单中的开发者

    打开其中的Selenium IDE

     现在来介绍一下Selenium IDE的操作界面

    左侧为测试用例列表,用于选择进行测试的脚本

    Base URL为我们录制脚本的目标网址,我们在该网站上的所有操作都会被录制下来

    fast-low用于调节测试时的速度,有的时候测试速度过快容易造成测试结果的异常

    中部的两个按钮分别是测试多个样例和测试单一样例

    右侧的红色按钮标示录制的状态,中间为浅红色时为录制中

    主体部分为已经录制的脚本内容,主要记录用户在网页上的相关操作

    我们以百度为例进行脚本的录制

    我们首先在Base URL输入百度的网址,然后打开百度首页,并搜索“123”

    录制结果如下

    我们运行刚刚录制的脚本

    单一用例测试成功

    我们接下来新建一个脚本文件,这次尝试通过百度搜索“456”

    同时测试两个脚本

    我们对于录制的脚本可以进行保存和导出,以第一个脚本为例,我们导出为Java/Junit/WebDriver Backed

    代码内容如下:

    package com.example.tests;
    
    import com.thoughtworks.selenium.Selenium;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.WebDriver;
    import com.thoughtworks.selenium.webdriven.WebDriverBackedSelenium;
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    import static org.junit.Assert.*;
    import java.util.regex.Pattern;
    import static org.apache.commons.lang3.StringUtils.join;
    
    public class testcase {
        private Selenium selenium;
    
        @Before
        public void setUp() throws Exception {
            WebDriver driver = new FirefoxDriver();
            String baseUrl = "https://www.baidu.com/";
            selenium = new WebDriverBackedSelenium(driver, baseUrl);
        }
    
        @Test
        public void testTestcase() throws Exception {
            selenium.open("/s?ie=utf-8&f=8&rsv_bp=0&rsv_idx=1&tn=monline_3_dg&wd=123&rsv_pq=bbc493ca0001f3db&rsv_t=cf61DU8FFmN%2FwF8kfsBVvVjHVj4%2F98NLPfJbcMPOhuJh%2BZ8Wy%2BtPsx2zih2QNvtABjhg&rsv_enter=0&rsv_sug3=4&rsv_sug1=3&rsv_sug7=100&inputT=1906&rsv_sug4=2804");
            selenium.click("css=img[alt="到百度首页"]");
            selenium.waitForPageToLoad("30000");
            selenium.click("id=kw");
            selenium.type("id=kw", "123");
            selenium.click("id=su");
        }
    
        @After
        public void tearDown() throws Exception {
            selenium.stop();
        }
    }

     三、访问http://www.ncfxy.com使用学号登录系统(账户名为学号,密码为学号后6位),进入系统后可以看到该用户的邮箱

    我们首先访问http://www.ncfxy.com

    在首页我们通过输入用户名和密码可以查看邮箱信息

    我们第四步的任务就是测试本地文件info.csv中用户名和邮箱的与网站上的信息是否完全一致

    四、编写Selenium Java WebDriver程序,测试info.csv表格中的学号和邮箱的对应关系是否正确

    csv文件中每一行仅包括学号和邮箱,我们编写WebDriver程序有两部分主要功能,一部分是解析本地的info.csv文件获取其中的信息,另外一部分是利用解析到的信息验证邮箱的正确性

    csv文件以逗号作为分隔符,一行代表一条数据

    对于csv文件的解析,我们现在导入jar包的方式完成解析工作,我们小组使用的是javacsv,这个jar包只有14k的大小,但是却足够完整csv文件解析

    首先我们在原有csv文件中添加表头id、email

    具体csv文件解析过程如下:

    //读取csv文件
            CsvReader r = new CsvReader("F://info.csv", ',',Charset.forName("utf-8"));
            r.readHeaders();
            
            while (r.readRecord()) {
                String name = r.get("id");
                String password = name.substring(4);
                String email = r.get("email");
                
            }
            r.close();

     第二部分内容,我们首先要查看目标网址的页面元素

     在第一个页面我们主要需要以下三个信息

     

    有id元素我们可以直接通过By.id()的方法获取

    跳转页面之后,我们需要获取表格中的第一行的第二列也就是邮箱信息

    由于表格中的第一行的第二列没有id值和name值,所以我们可以通过以下两种方法完成

    WebElement text = driver.findElement(By.xpath(".//*[@id='table-main']/tr[1]/td[2]"));
    WebElement text = driver.findElement(By.cssSelector("#table-main tr:first-child td:last-child"));

     在实验过程中我们总共使用了四个浏览器完成实验内容,完整代码如下

    import static org.junit.Assert.*;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.nio.charset.Charset;
    
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.chrome.*;
    import org.openqa.selenium.edge.EdgeDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.ie.InternetExplorerDriver;
    import org.openqa.selenium.remote.DesiredCapabilities;
    
    import com.csvreader.CsvReader;
    
    
    public class test1 {
    
        @Before
        public void setUp() throws Exception {
        }
    
        @After
        public void tearDown() throws Exception {
        }
    
        @Test
        public void test() throws Exception {
            WebDriver driver=null;
            //在通过某些浏览器进行测试时,页面加载慢于程序执行,我们需要让程序等待页面加载,比如说通过创建新的线程让程序暂停一段时间
            Thread thread = new Thread();
            //读取csv文件
            CsvReader r = new CsvReader("F://info.csv", ',',Charset.forName("utf-8"));
            r.readHeaders();
            /*
            我们一共在四个浏览器上尝试了selenium的相关功能,为了增强代码的可移植性,我们将所有浏览器的驱动放在工程目录下,
            通过设置webdriver.XXX.bin和webdriver.XXXX.driver(工程目录下的driver文件夹),来启动浏览器
            */
            //for firefox
            
             
            //System.setProperty("webdriver.firefox.bin", "E:/firefox/firefox.exe"); 
            driver = new FirefoxDriver();    
            
            
            //for Chrome
            /*
            String ch_driver = new File(new File(".").getCanonicalPath() + "\" +"driver/chromedriver.exe").getCanonicalPath();
            //System.out.println(driver);
            System.setProperty("webdriver.chrome.driver", ch_driver);
            System.setProperty("webdriver.chrome.bin", "C:\Users\敬成\AppData\Local\Google\Chrome\Application\chrome.exe");
            driver = new ChromeDriver();
            */
            //open IE
            /*
            String iedriver =    new File(new File(".").getCanonicalPath() + "\" +"driver/IEDriverServer.exe").getCanonicalPath();
    
            //System.out.println(iedriver);
            System.setProperty("webdriver.ie.driver", iedriver);
            System.setProperty("webdriver.chrome.bin", "C:\Program Files\Internet Explorer\iexplore.exe");
            DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();   
            ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);   
            driver = new InternetExplorerDriver(ieCapabilities);
            */
            //open Edge
            /*
            String edgedriver =    new File(new File(".").getCanonicalPath() + "\" +"driver/MicrosoftWebDriver.exe").getCanonicalPath();
    
            //System.out.println(iedriver);
            System.setProperty("webdriver.edge.driver", edgedriver);
            //System.setProperty("webdriver.edge.bin", "C:\Program Files\Internet Explorer\iexplore.exe");
            
            driver = new EdgeDriver();
            */
            while (r.readRecord()) {
                String name = r.get("id");
                String password = name.substring(4);
                String email = r.get("email");
                driver.get("http://www.ncfxy.com/");
                //窗口最大化
                driver.manage().window().maximize();
                
                WebElement txtbox1 = driver.findElement(By.id("name"));
                txtbox1.sendKeys(name);
                WebElement txtbox2 = driver.findElement(By.id("pwd"));
                txtbox2.sendKeys(password);
                WebElement btn = driver.findElement(By.id("submit"));
                btn.click();
                thread.sleep(1000);
                //以下两句话的运行结果相同
                //WebElement text = driver.findElement(By.xpath(".//*[@id='table-main']/tr[1]/td[2]"));
                WebElement text = driver.findElement(By.cssSelector("#table-main tr:first-child td:last-child"));
                String email2 = text.getText();
                assertEquals(email, email2);
            }
            r.close();
        }
    
    }

    以Firefox为例,运行结果如下:

    至此实验的所有内容都已经完成总结一下实验中遇到的一些问题及解决方法

    (1)、浏览器打开后无法访问目标网页,超时后程序运行中止:

    可能原因:1、Firefox版本与selenium的jar包版本不匹配,推荐使用Firefox45.0.1和selenium2.53.0

    2、在本机上安装有多种Java IDE,某些IDE安装时会更改设置,如果一个IDE无法正常运行,请尝试其他

    (2)、程序运行过程中提示No Such Element

     

    可能原因:页面加载速度慢于程序执行速度,每当打开一个页面时应该让程序暂停等待

  • 相关阅读:
    CSS使用position:sticky 实现粘性布局
    【笔记】原生JS实现验证框架 checkFun
    jQuery与vue分别实现超级简单的绿色拖动验证码功能
    vue从入门到进阶:Vuex状态管理(十)
    vue从入门到进阶:vue-router路由功能(九)
    vue从入门到进阶:渲染函数 & JSX(八)
    vue从入门到进阶:自定义指令directive,插件的封装以及混合mixins(七)
    vue从入门到进阶:组件Component详解(六)
    vue从入门到进阶:过滤器filters(五)
    vue从入门到进阶:Class 与 Style 绑定(四)
  • 原文地址:https://www.cnblogs.com/Elson8080/p/5402338.html
Copyright © 2020-2023  润新知