Search code examples
javaseleniumselenium-webdrivertestngtestng-dataprovider

NumberFormatException in TestNG dataprovider


I have used the below method to enter credentials in an application.

public LoginPage enterCredentials(String userName, String password){
        actions.EnterText(userId, userName)
               .EnterText(userPassword, password);
        return this;

in which EnterText is defined as below:

 public Actions EnterText(ObjectLocator locator, String text){
            driverWait(Integer.parseInt(getProperties("Control_Wait")));
            FindElement(locator).clear();
            FindElement(locator).sendKeys(text);
            return this;
        }

And in Test class i have written the below code

    public class LoginTests extends TestSetup{
    @Test(dataProvider="Credentials")
    public void loginProxy(String usrName, String usrPassword){
        LoginPage login = new LoginPage();
        login.navigateUrl()
             .enterCredentials(usrName, usrPassword)
             .clickLogin();
    }
    @DataProvider(name ="Credentials")
    public Object[][] getData(){
        Object[][] data = new Object[3][2];
        data[0][0] = "11";
        data[0][1] = "Priya";
        data[1][0] = "108";
        data[1][1] = "Logan";
        data[2][0] = "36";
        data[2][1] = "Geller";
        return data;
    }

Am getting the below error:

FAILED: loginProxy("11", "Priya") java.lang.NumberFormatException: null at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source)

Please help in resolving the same. As per my knowledge this error is caused due to the conversion of integer to string. But am not able to resolve the same.


Solution

  • Number format exception is thrown by parseInt method of Integer class. This definitely shows that method getProperties which is returning 'String' value for key "Control_Wait" is returning null and there may be 'n' number of reasons for that.

    Key is not present in properties file.

    Value of key is not present in properties file.

    Logic of getProperties() to get key value is wrong.

    Property file is not initialized or loaded correctly

    public class PropertyTest {
    
    public static Properties properties = new Properties();
    
    private static void loadProperties() {
        FileInputStream fis;
        try {
            fis = new FileInputStream("src/test/resources/property/android.properties");
            properties.load(fis);
            fis.close();
        } catch (FileNotFoundException e) {
    
        } catch (IOException e) {
    
        }
    } 
    public static String getProperty(String key) {
        String value = "";
        if (key != "") {
            loadProperties();
            try {
                if (!properties.getProperty(key).trim().isEmpty())
                    value = properties.getProperty(key).trim();
            }
    
            catch (NullPointerException e) {
    
            }
        }
    
        return value;
    }       
    
    }
    

    Usage

    driverWait(Integer.parseInt(getProperty("Control_Wait")));