How to pass two strings (Username and Password) one by one in the two different text fields



  • 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



  • 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; } }



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2