从CSV文件中提取数据自动化测试。

huangapple go评论58阅读模式
英文:

Put data from csv file automation testing

问题

"Hi I want to put data from my CSV file used automation testing but I have error:Attempt to read from null array
My code:

public class TestData {
    String DataCSVPath = "src/androidTest/java/md/app/TestData/Data.csv";
    protected CSVReader csvReader;
    String csvCell[];

    public String readFile() throws IOException, CsvValidationException {
        csvReader = new CSVReader(new FileReader(DataCSVPath));
        while ((csvCell = csvReader.readNext()) != null) {
            String Phone = csvCell[0];
            String IDNP = csvCell[1];
            String cardNumber = csvCell[2];

        }
        return null;
    }
}

My CSV file:
Phone,IDNP, card number

"064511418","59495955","9529525852"

And my automation step
onView(withId(R.id.et_phone_number)).perform(typeText(csvCell[0]));

In this line
onView(withId(R.id.et_phone_number)).perform(typeText(csvCell[0]));
I have error Attempt to read from null array"

英文:

Hi I want to put data from my CSV file used automation testing but I have error:Attempt to read from null array
My code:

public class TestData {
    String DataCSVPath = "src/androidTest/java/md/app/TestData/Data.csv";
    protected CSVReader csvReader;
    String csvCell[];

    public String readFile() throws IOException, CsvValidationException {
        csvReader = new CSVReader(new FileReader(DataCSVPath));
        while ((csvCell = csvReader.readNext()) != null) {
            String Phone = csvCell[0];
            String IDNP = csvCell[1];
            String cardNumber = csvCell[2];

        }
        return null;
    }
}

My CSV file:
Phone,IDNP, card number

"064511418","59495955","9529525852"
And my automation step
onView(withId(R.id.et_phone_number)).perform(typeText(csvCell[0]));

In this line
onView(withId(R.id.et_phone_number)).perform(typeText(csvCell[0]));
I have error ttempt to read from null array

Hi I want to put data from my CSV file used automation testing but I have error:Attempt to read from null array
My code:

public class TestData {
    String DataCSVPath = "src/androidTest/java/md/app/TestData/Data.csv";
    protected CSVReader csvReader;
    String csvCell[];

    public String readFile() throws IOException, CsvValidationException {
        csvReader = new CSVReader(new FileReader(DataCSVPath));
        while ((csvCell = csvReader.readNext()) != null) {
            String Phone = csvCell[0];
            String IDNP = csvCell[1];
            String cardNumber = csvCell[2];

        }
        return null;
    }
}

My CSV file:
Phone,IDNP, card number
"064511418","59495955","9529525852"

And my automation step
onView(withId(R.id.et_phone_number)).perform(typeText(csvCell[0]));

In this line
onView(withId(R.id.et_phone_number)).perform(typeText(csvCell[0]));
I have error ttempt to read from null array

答案1

得分: 1

我认为你的问题在于你的while循环。该循环不会停止,直到CSVReader#readNext()返回null,此时,null值被设置在数组变量中。稍后,当你尝试访问这个全局变量时,它是null。

相反,如果你的CSVReader类有一个hasNext()方法,你应该使用它来控制循环迭代。当有下一个元素时,代码将进入循环。然后,设置数组将是安全的。如果你没有这个方法,你可以考虑创建它。代码应该类似于这样:

while (csvReader.hasNext()) {
    csvCell = csvReader.readNext();
    // 为什么下面的字符串是必要的?你没有使用它们。
    String Phone = csvCell[0];
    String IDNP = csvCell[1];
    String cardNumber = csvCell[2];
}

这种方法可以保证数组永远不会为null,只要它至少进入循环一次。

如果你无法创建一个hasNext()方法,你需要使用一个临时数组。

public String readFile() throws IOException, CsvValidationException {
    String[] temp;
    csvReader = new CSVReader(new FileReader(DataCSVPath));
    while ((temp = csvReader.readNext()) != null) {
        csvCell = temp;
        // 移除不必要的字符串
    }
    return null;
}
英文:

I believe your problem is your while loop. The loop won't stop until CSVReader#readNext() returns null in which case, the null value is set in the array variable. Later on, when you try to access this global variable, it is null.

Instead, if your CSVReader class has a hasNext() method, you should use that to control the loop iterations. When you have a next element, the code will go inside the loop. Then, it will be safe to set the array. If you do not have this method, you may want to create it. The code should look something like this:

while (csvReader.hasNext()) {
    csvCell = csvReader.readNext();
    // Why are the strings below necessary? You aren't using them.
    String Phone = csvCell[0];
    String IDNP = csvCell[1];
    String cardNumber = csvCell[2];
}

This approach guarantees the array will never be null so long it goes into the loop at least once.

If you cannot create a hasNext() method, you need to use a temporary array.

public String readFile() throws IOException, CsvValidationException {
    String[] temp;
    csvReader = new CSVReader(new FileReader(DataCSVPath));
    while ((temp = csvReader.readNext()) != null) {
        csvCell = temp;
        // Removed unnecessary strings
    }
    return null;
}

huangapple
  • 本文由 发表于 2023年2月8日 20:53:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386097.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定