KingJames
KingJames

Reputation: 556

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

I am running a selenium Web Driver Project. I am taking an excel sheet as an input for my test case. I am getting the following error

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

Can someone please help me with this error. Thanks

The code for Excel sheet Driver is

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();
    }

}

EDIT

The link to the testing XML is here.

Upvotes: 1

Views: 4475

Answers (3)

Pavel Janicek
Pavel Janicek

Reputation: 14748

Although your XML file has proper declaration:

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

Its not enough. The XML file can be still encoded differently. Here is what you should try:

  1. Download Notepad++ (opensource)

  2. Open your XML file

  3. Click Format - Encode in UTF-8. See screen: enter image description here

  4. Save file and try again

Ofcourse you can skip 1 and use your preferred trext editing program. I know how to do it in Notepad++ so thats why I propose this solution

Upvotes: 0

Abhishek_Mishra
Abhishek_Mishra

Reputation: 4621

Try replacing the existing jar of testng in your project with some other jar.

It looks like the xml parser is causing this exception while parsing TestNG XML file.

UPDATED:

If you are using 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.

Hope this would help you.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1503050

Given the stack trace, it looks like the problem actually has nothing to do with your code. It looks like it's in the TestNG XML file you're using to set up the test suite.

Check that XML file - my guess is that it's declaring itself to be UTF-8, but is actually some other encoding.

Upvotes: 1

Related Questions