1

我正在运行一个硒 Web 驱动程序项目。我正在使用一个 Excel 表作为我的测试用例的输入。我收到以下错误

org.testng.TestNGException: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 2 of 2-byte UTF-8 sequence.
at org.testng.TestNG.initializeSuitesAndJarFile(TestNG.java:340)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:88)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175) Caused by: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 2 of 2-byte UTF-8 sequence.
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.arrangeCapacity(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.skipString(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(Unknown Source)
at org.testng.xml.SuiteXmlParser.parse(SuiteXmlParser.java:17)
at org.testng.xml.SuiteXmlParser.parse(SuiteXmlParser.java:10)
at org.testng.xml.Parser.parse(Parser.java:172)
at org.testng.TestNG.initializeSuitesAndJarFile(TestNG.java:310)
... 3 more

有人可以帮我解决这个错误。谢谢

Excel工作表驱动程序的代码是

package com.bigMachines.TCL.ExcelShhetDataProvider;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Set;
import org.testng.Assert;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class ExcelSheetDriver {

    private Sheet wrksheet;
    private Workbook wrkbook =null;
    private HashMap<String, Integer> dict= new HashMap<String, Integer>();
    //Create a Constructor
    public ExcelSheetDriver(String ExcelSheetPath){ 
        try {
            wrkbook = Workbook.getWorkbook(new File(ExcelSheetPath));
            wrksheet = wrkbook.getSheet(0);
            columnDictionary();
        } catch (BiffException e) {
            Assert.fail(e.getMessage());
        } catch (IOException e) {
            Assert.fail(e.getMessage());
        }
    }

    //Returns the Number of Rows
    public int rowCount()
    {
        return wrksheet.getRows();
    }

    //Returns the Cell value by taking row and Column values as argument
    public String readCell(int column,int row)
    {
        return wrksheet.getCell(column,row).getContents();
    }

    public String readCell(String columnName,int row)
    {
        return readCell(getColumnNo(columnName), row);
    }

    //Create Column Dictionary to hold all the Column Names
    private void columnDictionary()
    {
        //Iterate through all the columns in the Excel sheet and store the value in Hashtable
        for(int col=0;col<wrksheet.getColumns();col++)
        {
            dict.put(readCell(col,0), col);
        }
    }

    //Read Column Names
    public int getColumnNo(String colName)
    {
        try {
            int value;
            value = ((Integer) dict.get(colName)).intValue();
            return value;
        } catch (NullPointerException e) {
            return (0);

        }
    }
    public Set<String> getColumnNameList(){
        return dict.keySet();
    }

}

编辑

测试 XML 的链接在这里

4

3 回答 3

1

鉴于堆栈跟踪,看起来问题实际上与您的代码无关。看起来它位于您用来设置测试套件的 TestNG XML 文件中。

检查那个 XML 文件——我的猜测是它声明自己是 UTF-8,但实际上是其他一些编码。

于 2012-11-20T11:09:55.347 回答
0

尽管您的 XML 文件有正确的声明:

<?xml version="1.0" encoding="UTF-8"?>

这不够。XML 文件仍然可以进行不同的编码。这是您应该尝试的:

  1. 下载记事本++(开源)

  2. 打开您的 XML 文件

  3. 单击格式 - 以 UTF-8 编码。见屏幕: 在此处输入图像描述

  4. 保存文件并重试

当然,您可以跳过 1 并使用您喜欢的 trext 编辑程序。我知道如何在 Notepad++ 中做到这一点,所以这就是我提出这个解决方案的原因

于 2012-11-20T12:25:20.807 回答
0

尝试用其他 jar 替换项目中现有的 testng jar。

看起来 xml 解析器在解析 TestNG XML 文件时导致了这个异常。

更新:

如果您使用的是日食:

Right click on xml file and go to Properties.
There will be an option for Text File Encoding.
Check that it is set as UTF-8.
If not then made it as UTF-8.

希望这会对你有所帮助。

于 2012-11-20T11:47:20.130 回答