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;
}
在测试中 class 我写了下面的代码
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;
}
我收到以下错误:
FAILED: loginProxy("11", "Priya") java.lang.NumberFormatException:
null at java.lang.Integer.parseInt(Unknown Source) at
java.lang.Integer.parseInt(Unknown Source)
请帮忙解决。据我所知,这个错误是由于整数到字符串的转换引起的。但是我无法解决同样的问题。
数字格式异常被parseInt方法抛出 Integer class .这明确表明方法 getProperties 返回键 "Control_Wait" 的 'String' 值返回 null,可能有 'n' 个原因。
属性文件中不存在密钥。
键值不在属性文件中。
getProperties()获取key值的逻辑错误
属性 文件未正确初始化或加载
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")));
我已使用以下方法在应用程序中输入凭据。
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;
}
在测试中 class 我写了下面的代码
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;
}
我收到以下错误:
FAILED: loginProxy("11", "Priya") java.lang.NumberFormatException: null at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source)
请帮忙解决。据我所知,这个错误是由于整数到字符串的转换引起的。但是我无法解决同样的问题。
数字格式异常被parseInt方法抛出 Integer class .这明确表明方法 getProperties 返回键 "Control_Wait" 的 'String' 值返回 null,可能有 'n' 个原因。
属性文件中不存在密钥。
键值不在属性文件中。
getProperties()获取key值的逻辑错误
属性 文件未正确初始化或加载
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")));