Automation Using Selenium Webdriver

Thursday, 27 October 2016

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

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

No comments:

Post a Comment