Domanda

I have a text file with the following format: UserName Password On two separate lines. I am trying to enter the user name into the user name field (obviously) and the same with the password only into the password field. My test used to have a String element defined for each and all was well and good as I would simply call the String element. The problem is that I cannot check in my code, because it has my personal active directory login information. So I am trying to read it from a text file. I have the following code to do this:

        try
    {
        FileInputStream fstream = new FileInputStream(".../Documents/userInfo.txt");
        // Use DataInputStream to read binary NOT text.
        BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
        String strLine;
        int count = 0;
        strLine = br.readLine();
        count++;

        while(strLine!= null)
        {
            //Enter userName
            WebElement userName = driver.findElement(By.id("username"));
            userName.clear();
            userName.sendKeys(strLine);
            System.out.println(strLine);

            strLine = br.readLine();
            count++;
            //Enter Password
            WebElement password = driver.findElement(By.id("pword"));
            password.clear();
            password.sendKeys(strLine);
            System.out.println(strLine);
        }
        in.close();
        br.close();
    }
    catch (Exception e)
    {
        System.err.println("Error: " + e.getMessage());
    }

The problem that I am running into is that it enters the user name and password and then runs through it again only putting in the password into the User Name field. I know that this is probably something simple that I am over looking. Please help.

È stato utile?

Soluzione 2

Replace with below code:

         try
            {
                FileInputStream fstream = new FileInputStream("c:/Test/userInfo.txt");
                // Use DataInputStream to read binary NOT text.
                BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
                String strLine;
                int count = 0;

                count++;

                while((strLine = br.readLine())!= null)
                {
                    //Enter userName
                    WebElement userName = driver.findElement(By.id("username"));
                    userName.clear();
                    userName.sendKeys(strLine);
                    System.out.println(strLine);

                    strLine = br.readLine();
                    count++;
                    //Enter Password
                    WebElement password = driver.findElement(By.id("pword"));
                    password.clear();
                    password.sendKeys(strLine);
                    System.out.println(strLine);
                }
                in.close();
                br.close();
            }
            catch (Exception e)
            {
                System.err.println("Error: " + e.getMessage());
            }

    }

However I did not understand why you are looping. Do you have just one username/password in file...or lots of usernames/passwords?

Altri suggerimenti

Did you tried using java.util.Properties class for reading the keys from text file? It is designed for these purposes and can be used to both read/write properties

Example code

        Properties prop = new Properties();
        prop.load(new FileInputStream(".../Documents/userInfo.txt"));
        String userName = prop.getProperty("username");
        // Password should always be stored in the char array.
        char[] password = null;
        if (prop.getProperty("pword") != null) {
            password = prop.getProperty("pword").toCharArray();
        }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top