簡體   English   中英

TestNG數據提供程序中的NumberFormatException

[英]NumberFormatException in TestNG dataprovider

我已使用以下方法在應用程序中輸入憑據。

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

其中EnterText的定義如下:

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

在Test類中,我編寫了以下代碼

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

正在收到以下錯誤:

失敗:loginProxy(“ 11”,“ Priya”)java.lang.NumberFormatException:在java.lang.Integer.parseInt(未知源)處為null

請幫助解決相同的問題。 據我所知,此錯誤是由於整數到字符串的轉換引起的。 但是無法解決相同的問題。

數字格式異常Integer類的parseInt方法引發。 這肯定表明方法getProperties返回鍵“ Control_Wait”的'String'值,返回null,並且可能有'n'個原因。

密鑰在屬性文件中不存在。

鍵值在屬性文件中不存在。

getProperties()獲取鍵值的邏輯是錯誤的。

屬性文件未初始化或未正確加載

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

}

用法

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM