- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嗨,我想将结果写入第三列,但它显示了 stram 关闭错误。
package test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.awt.AWTException;
import java.awt.HeadlessException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
public class Xl2 {
public static void main( String[] args ) throws Exception{
String[][] data ;
//HSSFCell cell = null;
data = excelRead();
String expectedtitle;
for (int i = 1; i < data.length; i++ ) {
expectedtitle = login(data[i][0],data[i][1]);
System.out.println("page title after login is" + expectedtitle );
if(expectedtitle.equalsIgnoreCase(":: IRCTC :: - Plan My Travel")){
System.out.println("PASS");
String status="PASS";
excelwrite(status);
}
else{
System.out.println("FAIL");
String status = "FAIL";
excelwrite(status);
}
}
}
public static String login(String username,String password) throws InterruptedException{
//Step 1 Open Firefox
WebDriver driver = new FirefoxDriver();
//Step 2 Go to url
driver.get("https://www.irctc.co.in/");
String actualtitle= driver.getTitle();
WebDriverWait wait = new WebDriverWait(driver,60);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("userName")));
Thread.sleep(3000);
driver.findElement(By.name("userName")).sendKeys(username);
driver.findElement(By.name("password")).sendKeys(password);
driver.findElement(By.id("button")).click();
Thread.sleep(3000);
String expectedtitle= driver.getTitle();
/*if (actualtitle.equals(expectedtitle)){
System.out.println("FAIL" );
}else{
System.out.println("PASS" );
}*/
driver.close();
return expectedtitle;
}
public static String[][] excelRead() throws Exception {
File excel = new File("D:\\Work\\test2.xls");
FileInputStream fis = new FileInputStream(excel);
HSSFWorkbook wb = new HSSFWorkbook(fis);
HSSFSheet ws = wb.getSheet("Sheet1");
int rowNum = ws.getLastRowNum() + 1;
int colNum = ws.getRow(0).getLastCellNum();
String[][] data = new String[rowNum][colNum];
for (int i = 0 ; i < rowNum ; i++) {
HSSFRow row = ws.getRow(i);
for (int j=0 ; j < colNum ; j++){
HSSFCell cell = row.getCell(j);
String value = cellToString(cell);
data[i][j] = value;
// System.out.println("The value is" + value);
}
}
return data;
}
public static void excelwrite(String status) throws Exception {
try{
FileInputStream file = new FileInputStream(new File("D:\\Work\\test2.xls"));
HSSFWorkbook workbook = new HSSFWorkbook(file);
int LastRow = 0;
HSSFWorkbook wb = new HSSFWorkbook(file);
HSSFSheet sheet = wb.getSheetAt(0);
LastRow = sheet.getLastRowNum();
LastRow = LastRow + 1;
Row row = sheet.createRow(LastRow);
Cell cell2 = row.createCell(2);
cell2.setCellValue(status);
file.close();
FileOutputStream outFile =new FileOutputStream(new File("D:\\Work\\test2.xls"));
workbook.write(outFile);
outFile.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
catch (HeadlessException e)
{
e.printStackTrace();
}
}
public static String cellToString(HSSFCell cell) {
int type;
Object result ;
type = cell.getCellType();
switch (type) {
case 0 :
result = cell.getNumericCellValue();
break;
case 1 :
result = cell.getStringCellValue();
break;
default :
throw new RuntimeException("There are no support for this type of cell");
}
return result.toString();
}
}
请帮助我。我的主要目的是从 Excel 中提取值并将结果(通过/失败)写入各自的行。我是 selenium 和 java 的新手。
最佳答案
嘿,我得到了问题的答案。希望这对刚接触 java excel 领域的人有用。
package test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.awt.AWTException;
import java.awt.HeadlessException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
public class Excelreadwrite {
public static void main( String[] args ) throws Exception{
String[][] data ;
data = excelRead();
String expectedtitle;
for (int i = 1; i < data.length; i++ ) {
int LastRow = i;
expectedtitle = login(data[i][0],data[i][1]);
System.out.println("page title after login is" + expectedtitle );
if(expectedtitle.equalsIgnoreCase(":: IRCTC :: - Plan My Travel")){
System.out.println("PASS");
String status="PASS";
excelwrite(status,LastRow);
}
else{
System.out.println("FAIL");
String status = "FAIL";
excelwrite(status,LastRow);
}
}
}
public static String login(String username,String password) throws InterruptedException{
//Step 1 Open Firefox
WebDriver driver = new FirefoxDriver();
//Step 2 Go to url
driver.get("https://www.irctc.co.in/");
String actualtitle= driver.getTitle();
WebDriverWait wait = new WebDriverWait(driver,60);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("userName")));
Thread.sleep(3000);
driver.findElement(By.name("userName")).sendKeys(username);
driver.findElement(By.name("password")).sendKeys(password);
driver.findElement(By.id("button")).click();
Thread.sleep(3000);
String expectedtitle= driver.getTitle();
driver.close();
return expectedtitle;
}
public static String[][] excelRead() throws Exception {
File excel = new File("D:\\Work\\test2.xls");
FileInputStream fis = new FileInputStream(excel);
HSSFWorkbook wb = new HSSFWorkbook(fis);
HSSFSheet ws = wb.getSheet("Sheet1");
int rowNum = ws.getLastRowNum() + 1;
int colNum = ws.getRow(0).getLastCellNum();
String[][] data = new String[rowNum][colNum];
for (int i = 0 ; i < rowNum ; i++) {
HSSFRow row = ws.getRow(i);
for (int j=0 ; j < colNum ; j++){
HSSFCell cell = row.getCell(j);
String value = cellToString(cell);
data[i][j] = value;
// System.out.println("The value is" + value);
}
}
return data;
}
public static void excelwrite(String status, int LastRow) throws Exception {
try{
FileInputStream file = new FileInputStream(new File("D:\\Work\\test2.xls"));
HSSFWorkbook workbook = new HSSFWorkbook(file);
HSSFSheet sheet = workbook.getSheetAt(0);
Row row = sheet.getRow(LastRow);
Cell cell2 = row.createCell(2);
cell2.setCellValue(status);
System.out.println(status);
file.close();
FileOutputStream outFile =new FileOutputStream(new File("D:\\Work\\test2.xls"));
workbook.write(outFile);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
catch (HeadlessException e)
{
e.printStackTrace();
}
}
public static String cellToString(HSSFCell cell) {
int type;
Object result ;
type = cell.getCellType();
switch (type) {
case 0 :
result = cell.getNumericCellValue();
break;
case 1 :
result = cell.getStringCellValue();
break;
default :
throw new RuntimeException("There are no support for this type of cell");
}
return result.toString();
}
}
编辑:格式化代码。
关于java - 如何使用java apache POI :showing java. io.IOException在Excel中写入:流关闭错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20211015/
我们有一个连接到某些网络服务的 Windows 窗体应用程序。它列出了系统中的文档,当用户双击一个文件时,我们将文件下载到本地计算机并打开文档供他们编辑。一旦用户关闭文档,我们就会将其上传回系统。 对
public class SampleCloseable implements AutoCloseable { private String name; public SampleCl
我正在尝试使用 JAVA 运行一个简单的 sqoop 导入程序。 我的程序: String driver="com.vertica.Driver"; Configuration con
我需要从 Java 执行一个外部程序(使用 libreoffice 将 fodt 文件转换为 pdf,就这样发生了)我知道该程序所需的精确命令行: /usr/bin/libreoffice --hea
AFAIK,标准try-with-resources 形式 try(InputStream is= new ...){ ... some reading from is } catch (..
我观察到这两种说法都是有效的。与第二个语句相比,第一个语句中记录的额外内容是什么? 最佳答案 第一个还记录原始异常(和堆栈跟踪),第二个仅记录消息。 因此,第一个语句中记录的“额外内容”是原始异常。这
我想执行重命名和删除功能,环境是LINUX。这是我正在使用的代码, String[] command_ary = { "/usr/bin/sh", "-c", command }; Runtime r
在使用 selenium webdriver 实现 Web 应用程序的自动化时,我遇到了一种情况,我需要上传文件并进一步继续。 我们为此使用 Java 和 Tcl 脚本语言。 下面是我的 TCL 代码
我正在尝试使用 ANT 将文件从一个目录复制到 Linux 上的另一个目录。 首先我使用了复制任务,它工作正常但文件模式没有保留。然后我改为使用 ,这就是我卡住的地方。 我的目标是这样的:
当我输入命令时: ./sqoop-import --connect jdbc:mysql://localhost/sqoop2 -table sqeep2 -m 1 -hive-import 当执行这
我正在使用 Sun 的 keytool 创建一个 Bouncy caSTLe keystore 并将证书导入其中。 keytool 确实会生成一个 Bouncy caSTLe 格式的 keystore
我正在执行下面的程序,它通过 java 调用 shell,我得到了异常请帮助我。 程序: import java.io.*; import java.util.*; public class Proc
我在我的一个项目中遇到了这个错误。 FAILURE: Build failed with an exception. What went wrong: Execution failed for tas
什么情况下read end可以死对偶PipedOutputStream和 PipedInputStream ?我没有关闭任何管道。 最佳答案 我遇到了java.io.IOException: Read
我有一段从文件中读取数据的代码。我想在此代码中强制 IOException 用于测试目的(我想检查代码在这种情况下是否抛出正确的自定义异常)。 例如,有什么方法可以创建一个防止被读取的文件?也许处理一
我为MapReduce文本排序编写了这样的代码: public static class SortMapper extends Mapper { private Text citizenshi
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我要createNewFile有一条路径,但我得到了一个 IOException。问题是,详细的消息无法解释,我只能看到一堆问号。 我最初使用的是西类牙语的 Windows 10,但安装了中文语言包。
我认为这是基本的东西,但我不知道该怎么做。为什么我得到 IOException never throw in body of相应的 try 语句 public static void main(Str
我正在从 Java 项目中的类路径读取文件。 示例代码: public static Properties loadPropertyFile(String fileName) {
我是一名优秀的程序员,十分优秀!