Selenium Web驱动程序:MalformedByteSequenceException 2字节UTF-8序列的字节2无效

时间:2012-11-20 10:36:06

标签: java selenium testng

我正在运行selenium Web Driver Project。我正在将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的链接是here

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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

看起来xml解析器在解析TestNG XML文件时会导致此异常。

<强>更新:

如果你正在使用eclipse:

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.

希望这会对你有所帮助。

答案 2 :(得分:0)

虽然你的XML文件有适当的声明:

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

还不够。 XML文件仍然可以以不同方式编码。这是你应该尝试的:

  1. 下载Notepad++(opensource)

  2. 打开您的XML文件

  3. 单击格式 - 以UTF-8编码。见屏幕: enter image description here

  4. 保存文件,然后重试

  5. 当然,您可以跳过1并使用您首选的trext编辑程序。我知道如何在Notepad ++中这样做,这就是我提出这个解决方案的原因