Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

I am having two different Strings by name username and password. And i want to pass these string values one by one (ONLY one value from username and one value from password) into username and password text fields. I tried in two different ways but not sure where i am doing mistake. Below are my codes and O/P result. Expected: Two combinations (1st: @ and @, 2nd: test and test).

Expected O/P: username password username password

String[] username={"@", "test"};

String[] password= {"@", "test"};

1st method:

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TwoStrings {
public static void main(String[] args) {
String[] username={"@", "test"};
String[] password= {"@", "test"};
WebDriver d =new FirefoxDriver();
d.get("http://newtours.demoaut.com/mercurysignon.php");
d.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
for(String j:username){
d.findElement(By.name("userName")).clear();
d.findElement(By.name("userName")).sendKeys(j);
System.out.println("userName");
for(int i =0; i<password.length; i++){
d.findElement(By.name("password")).clear();
d.findElement(By.name("password")).sendKeys(password[i]);
System.out.println("password");
}
}
d.findElement(By.name("login")).click();
}
}

O/P: userName password password userName password password

2nd method which i tried:

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TwoStrings {
public static void main(String[] args) {
String[] username={"@", "test"};
String[] password= {"@", "test"};
WebDriver d =new FirefoxDriver();
d.get("http://newtours.demoaut.com/mercurysignon.php");
d.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
for(String j:username){
for(String k:password){
d.findElement(By.name("userName")).clear();
d.findElement(By.name("userName")).sendKeys(j);
System.out.println("userName");
d.findElement(By.name("password")).clear();
d.findElement(By.name("password")).sendKeys(k);
System.out.println("password");
}
}
d.findElement(By.name("login")).click();
}
}

O/P: userName password userName password userName password userName password

share|improve this question

1 Answer 1

up vote 2 down vote accepted

So if you want to stick with String[] for you two username and password values here is one approach. Instead of your current loop I would use a traditional for loop and access the array elements by index in a single loop. So you code might looks something like this:

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TwoStrings 
{
    public static void main(String[] args) 
    {
        String[] username={"@", "test"};
            String[] password= {"@", "test"};

        WebDriver d =new FirefoxDriver();
        d.get("http://newtours.demoaut.com/mercurysignon.php");
        d.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

            for(int i = 0; i < username.length ; i++)
            {
               d.findElement(By.name("userName")).clear();
               d.findElement(By.name("userName")).sendKeys(username[i]);
               System.out.println(username[i]);
               d.findElement(By.name("password")).clear();
               d.findElement(By.name("password")).sendKeys(password[i]);
               System.out.println(username[i]);

               d.findElement(By.name("login")).click();
             }
    }
}

This should run the user name and password combinations in a single loop assuming that is what you were attempting to do.

Here is one alternative using a custom collection and a List<>:

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TwoStrings 
{
    public static void main(String[] args) 
    {
        List<LoginData> dataCollection = new ArrayList<LoginData>();
        data.add(new LoginData("username","password"));
        data.add(new LoginData("username1","password2"));


        WebDriver d =new FirefoxDriver();
        d.get("http://newtours.demoaut.com/mercurysignon.php");
        d.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

            for(LoginData data : dataCollection)
            {
               d.findElement(By.name("userName")).clear();
               d.findElement(By.name("userName")).sendKeys(data.get_username());
               System.out.println(data.get_username());
               d.findElement(By.name("password")).clear();
               d.findElement(By.name("password")).sendKeys(data.get_password());
               System.out.println(data.get_password());

               d.findElement(By.name("login")).click();
             }
    }
}

public class LoginData
{
    private String _username;
    private String _password;

    public LoginData(String username, String password)
    {
        this.set_username(username);
        this.set_password(password);    
    }

    public String get_username()
    {
        return _username;
    }

    private void set_username(String _username)
    {
        this._username = _username;
    }

    public String get_password()
    {
        return _password;
    }

    private void set_password(String _password)
    {
        this._password = _password;
    }

}
share|improve this answer
    
Dan in my case i have different roles/users (Ex: Admin and Employee- These options are under dropdown) so when you select user/role as Admin and you enter Employee credentials then application will throw an error message. So i am using String [] array in code. (Above url is sample application - just for reference). –  QA4it Apr 11 at 16:58
    
Ok - based on your comment is sounds like you need to modify your looping structure. I am assuming the order of the usernames and passwords line up so index 0 and 1 in both arrays? Working from that I will modify my answer. –  Dan Snell Apr 11 at 17:03
    
AWESOME....!!!! It worked as per my expectation. But if you brief about below logic than its good for me. i < username.length you used only for username but not password?. –  QA4it Apr 11 at 17:24
1  
Since the arrays are the same size in this case I am only using it to define the upper bounds of the loop. So I could have used password.length but it is pretty much interchangeable. There are two other possible approaches you could take. One would have been to use a Map (key/value pairs). Or you could have created a custom object to hold the values and stored those in a List<>. –  Dan Snell Apr 11 at 17:33
    
I am not good coder but I will work on your suggestions. Thanks much friend.. –  QA4it Apr 11 at 17:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.