I'm creating automated tests for an android app with very little knowledge of Java. I'm using a test recorder to generate the basic robotium scripts and then updating them manually. The tests are working fine with the log in credentials hard coded in the scripts, but to apply these tests to more than one account I wanted to read the user name and password in from a text file. A Scanner instance seemed to me to the the logical method to read line delimited strings from a text file into the variables for the log in credentials but I'm having trouble figuring out where to put the code. It seems like whatever I try the variables are either out of scope (not being used) or I come up with a some exception or other.
Here's the Scanner code I'm using, currently positioned at the top of my test code:
public void testRecorded() throws Exception {
String userName;
String passWord;
Scanner scanFile = null;
try {
scanFile = new Scanner("C:/...path to file...file.txt");
//Read the password from file
userName = scanFile.nextLine();
//Consume the line break
scanFile.nextLine();
//Read the password from file
passWord = scanFile.nextLine();
}
finally {
scanFile.close();
}
Test code continues...
Running this fails at the nextLine statement where I consume the line break with a NoSuchElementException
Is Scanner the best method to use to read user names and passwords from text files? Is there something wrong with my Scanner code? Where do I put the Scanner code in my test? Does it require a separate class or can I keep this code with the rest of my test code?