0

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?

TimM
  • 1
  • 2
  • It's failing at `scanFile.nextLine()` and not `passWord = scanFile.nextLine();`? Just making sure. – BVB Mar 07 '14 at 18:52
  • Yes, but if I comment scanFile.nextLine() it fails at passWord = scanFile.nextLine(), so it seems to be failing at the second "scanFile" statement, whatever it happens to be. – TimM Mar 07 '14 at 20:43

1 Answers1

0
  1. You want to scan file from host machine ("C:/..."). Device has no access to that. You have to put file to device, for instance on sdcard.

  2. If you want to use Scanner for scanning files you cannot pass String there. You should rather use:

    // if you put file.txt directly on sdcard (/sdcard/file.txt)
    String path = String.format("%s/%s", Environment.getExternalStorageDirectory(), "file.txt");
    Scanner scan = new Scanner(new File(path));
    ...
    
maszter
  • 3,680
  • 6
  • 37
  • 53
  • Duh! I never considered that the test was not running on the machine where I launch it but on the device itself. I'll give this a try, thanks. – TimM Mar 07 '14 at 20:50
  • I'm afraid this didn't work. Tried the following code and the "NoSuchElementException" error remains at the same place. – TimM Mar 10 '14 at 14:25
  • Scanner scanFile = null; try { String path = String.format("%s/%s", Environment.getExternalStorageDirectory(), "rscData.txt"); scanFile = new Scanner(path); //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(); } – TimM Mar 10 '14 at 14:33
  • read again my answer, you have to use file, instead of string with path to that file – maszter Mar 10 '14 at 16:54
  • Thanks for pointing out my mistake. The error has changes from the NoSuchElementException to a FileNotFoundException @ /storage/sdcard0/file name. Is this because the sd card is mounted on my computer? How do I run a test against the handset without mounting the sd card? – TimM Mar 11 '14 at 14:14
  • use adb push before running test: http://stackoverflow.com/questions/20834241/how-to-use-adb-command-to-push-a-file-on-device-without-sd-card – maszter Mar 11 '14 at 18:16