代码之家  ›  专栏  ›  技术社区  ›  Dinu

TestNG数据提供程序中的NumberFormatException

  •  1
  • Dinu  · 技术社区  · 8 年前

    我已使用以下方法在应用程序中输入凭据。

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

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

    在测试类中,我编写了以下代码

        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处为空(未知源) java.lang.Integer.parseInt(未知源)

    请帮助解决同样的问题。据我所知,这个错误是由整数转换成字符串引起的。但我不能解决同样的问题。

    1 回复  |  直到 8 年前
        1
  •  -1
  •   Amit Jain    8 年前

    数字格式异常 是由 帕森特 整数 上课。这明确表明,为键“Control_Wait”返回“String”值的方法getProperties返回的是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")));
    
    推荐文章