selenium webdriver学习--利用POI实现数据驱动

包:HSSF          读写Excel   xls格式文档;

         XSSF          读写Excel   ooxml、xlsx格式;

         HWPT       读写Word、doc格式

         HSLF                   读写ppt;

         HDGF        读写Visio;

         HPBF         读写publisher;

         HSMF        读写Outlook;

类:HSSFWorkBook         Excel文档对象;

         HSSFSheet                  Excel表单;

         HSSFCell                     Excel表格子单元;

         HSSFRow                    Excel行;

         HSSFFont                    Excel字体;

         HSSFDateFormat     Excel格子单元的日期格式;

         HSSFHeader              sheet的页眉;

         HSSFFooter                sheet的页脚

         HSSFCellStyle            格子单元格式

         HSSFDateUtil            日期

         HSSFPrintSetup         打印

         HSSFErrorConstants         错误信息表



主要是用了对Excel处理的包,步骤其实也不难。

先声明个文档对象,之后再就是sheet表单,最后就是行和列了。你要去的某个东西就好比是一所屋子里某个柜子抽屉内的物品,声明workbook就是进入房子,sheet就是你要找的抽屉所在的柜子,根据行和列既可以确定。

下面是写的一个简单的实例,大家参考下吧。


package com.monkey.selenium;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.concurrent.TimeUnit;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class LoginExcelTest {
	public static void main(String[] args) throws IOException {
		String filepath = "D:\\POI\\logininfo.xlsx";
		System.setProperty("webdriver.firefox.bin", "D:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
		WebDriver driver = new FirefoxDriver();
		driver.get("http://ip:10103/login.html"); //获取URL
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		//开始获取Excel文件内的登录信息
		FileInputStream in = new FileInputStream(filepath);
		XSSFWorkbook workbook = new XSSFWorkbook(in);
		XSSFSheet sheet = workbook.getSheetAt(0);
		XSSFRow row = sheet.getRow(1);
		int i = 0;
		XSSFCell cell = row.getCell(i);
		i++;
		Double userName = cell.getNumericCellValue();
		XSSFCell cell2 = row.getCell(i);
		Double passWord = cell2.getNumericCellValue();
		System.out.println(userName + "," + passWord);
		in.close();
		//传递参数用户名和密码文本框
		driver.findElement(By.id("name")).sendKeys((new BigDecimal(userName)).toString());
		driver.findElement(By.id("pwd")).sendKeys((new BigDecimal(passWord)).toString());
		driver.findElement(By.id("loginBtn")).click();
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		String url = driver.getCurrentUrl();
		String result = null;
		//写入测试结果
		if ("http://10.100.143.26:10103/usrHome.html".equals(url)) {
			result = "pass";
		}else {
			result = "fail";
		};
		XSSFCell newCell = row.createCell(2);
		newCell.setCellValue(result);
		FileOutputStream out = new FileOutputStream(filepath);
		workbook.write(out);
		workbook.close();
		out.close();
		driver.quit();
		System.out.println("Down!");
	}

}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值