繁体   English   中英

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

[英]Selenium Web Driver : MalformedByteSequenceException Invalid byte 2 of 2-byte UTF-8 sequence

我正在运行一个硒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的链接在这里

给定堆栈跟踪,看起来问题实际上与您的代码无关。 看起来好像是在用来设置测试套件的TestNG XML文件中。

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

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

看起来在解析TestNG XML文件时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.

希望这对您有帮助。

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

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

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

  1. 下载Notepad ++ (开源)

  2. 打开您的XML文件

  3. 单击格式-以UTF-8编码。 见屏幕: 在此处输入图片说明

  4. 保存文件,然后重试

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM