• 软件测试第二次上机实验


    这次上机我们主要使用Selenium进行自动化测试首先我们需要下载selenium-java的依赖项。

    若使用maven管理项目,则在.pom文件中加入如下依赖项:

    <dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>2.53.0</version>
    </dependency>
    

      

    也可以手动下载jar包,具体可以上官网下载,在此不再赘述。

    下面是我们实验内容:

    一、实验目的

    • 安装SeleniumIDE插件
    • 学会使用SeleniumIDE录制脚本和导出脚本
    • 访问http://www.ncfxy.com使用学号登录系统(账户名为学号,密码为学号后6位),进入系统后可以看到该用户的邮箱。
    • 编写Selenium Java WebDriver程序,测试info.csv表格中的学号和邮箱的对应关系是否正确。

    二、Java WebDriver代码:

    package lesson1;
    
    import static org.junit.Assert.*;
    
    import java.util.concurrent.TimeUnit;
    
    import org.junit.*;
    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.support.ui.ExpectedCondition;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    import java.io.File;   
    import java.io.FileInputStream;   
    import java.io.IOException;
    import java.io.InputStream;     
    
    import jxl.Cell;     
    import jxl.CellType;    
    import jxl.Sheet;    
    import jxl.Workbook;    
    import jxl.write.Label;   
    
    
    public class ExampleForFireFox  {
    	
    
    	  private WebDriver driver;
    	  private String baseUrl;
    	
    	@Before
    	  public void setUp() throws Exception {
    	    driver = new FirefoxDriver();
    	    baseUrl = "http://www.ncfxy.com/";
    	    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    	  }
    
    	
    	@Test
        public void Test1() throws Exception {
    		jxl.Workbook readwb = null;   
    		//构建Workbook对象, 只读Workbook对象   
            //直接从本地文件创建Workbook   
            InputStream instream = new FileInputStream("D:/info.xls");   
            
            readwb = Workbook.getWorkbook(instream);
            //System.out.println("jjjjjjj ");
            //Sheet的下标是从0开始   
            //获取第一张Sheet表   
            Sheet readsheet = readwb.getSheet(0);   
            //获取Sheet表中所包含的总列数   
            int rsColumns = readsheet.getColumns();   
            //获取Sheet表中所包含的总行数   
            int rsRows = readsheet.getRows();   
            //获取指定单元格的对象引用   
            for (int i = 0; i <rsRows; i++)   
            {   
            	driver.get(baseUrl);
            	Cell cell = readsheet.getCell(0, i);  
            	String username = cell.getContents();
            	String password = username.substring(4, 10);
                // 通过 id 找到 input 的 DOM
                WebElement element = driver.findElement(By.id("name"));
                WebElement element1 = driver.findElement(By.id("pwd"));
    
                //System.out.println(element.getSize());   
                // 输入关键字
                element.sendKeys(username);
                element1.sendKeys(password);
    
                // 提交 input 所在的  form
                element.submit();
                
                //获取得到的邮箱
                WebElement element2 = driver.findElement(By.xpath("//td[2]"));
                
                String mailByWeb = element2.getText();
                String mailByInfo = readsheet.getCell(1,i).getContents();
                
                assertEquals(mailByInfo,mailByWeb);
                
                //System.out.println(element2.getText());   
                
               // System.out.println();   
            }           
           /* //利用已经创建的Excel工作薄,创建新的可写入的Excel工作薄   
            jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(new File(   
                    "D:/info.xls"), readwb);   
            //读取第一张工作表   
            jxl.write.WritableSheet ws = wwb.getSheet(0);   
            //获得第一个单元格对象   
            jxl.write.WritableCell wc = ws.getWritableCell(0, 0);   
            //判断单元格的类型, 做出相应的转化   
            if (wc.getType() == CellType.LABEL)    
            {   
                Label l = (Label) wc;   
                l.setString("新姓名");   
            }   
            //写入Excel对象   
            wwb.write();   
            wwb.close();   */
           //关闭读入流
            readwb.close();   
           
        
             
        }
    }
    

    三、心得

    通过本次实验我们学会了如何用Selenium IDE实现简单的自动化测试,其中包括测试的录制,编写及运行,让我们更加深入的了解了测试的方法和测试的重要性。

  • 相关阅读:
    数据库中表的主键的定义
    软件的三大类型
    常用逻辑公式
    软件开发中常用英文含义
    2017.11.27T19_8zuoye
    2017.11.29T19_B1_9zuoye chihuolianmeng
    2017.12.1T19_B2_1zuoye
    2017.12.6T19_B2_3.4
    2017.12.1T19_B2_2zuoye
    2017.12.6T19_B2_3.2 zuoye
  • 原文地址:https://www.cnblogs.com/Theshy/p/5399884.html
Copyright © 2020-2023  润新知