I am learning TestNG for selenium. I want to pass three different usernames and passwords to the @Test, which is the login scenario. The scenario is:
The first test is getting passed. The other two are getting failed with UnhandledAlertException.
package testNG;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class testData {
WebDriver driver;
@Test(dataProvider="data")
public void login(String userName, String password) {
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://demo.guru99.com/V1/index.php");
WebElement userID = driver.findElement(By.xpath("//input[@name='uid']"));
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(userID));
userID.sendKeys(userName);
driver.findElement(By.xpath("//input[@name='password']")).sendKeys(password);
driver.findElement(By.xpath("//input[@name='btnLogin']")).click();
driver.findElement(By.xpath("//a[@href='Logout.php']")).click();
driver.switchTo().alert().accept();
driver.quit();
}
@DataProvider(name="data")
public Object[][] getUserData(){
return new Object[][] {
{"mngr137366", "jUgyjAn"},
{"mngr137370", "uvetahA"},
{"mngr137371", "utYmEqY"},
};
}
}
Update: With the handling of the alert and the removing of the hardcoded username, the code is working fine now. But the browser is being opened for three times for three logins. I want it to open one time and perform three logins. For that I have added the below code:
@BeforeClass
public void setUp() {
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://demo.guru99.com/V1/index.php");
}
and the same is removed from the login() function. Now the first login is only successful. The other two logins are left.
The total code:
public class testData {
//public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver;
@BeforeClass
public void setUp() {
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://demo.guru99.com/V1/index.php");
}
@Test(dataProvider="data")
public void login(String userName, String password) {
WebElement userID = driver.findElement(By.xpath("//input[@name='uid']"));
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(userID));
userID.sendKeys(userName);
driver.findElement(By.xpath("//input[@name='password']")).sendKeys(password);
driver.findElement(By.xpath("//input[@name='btnLogin']")).click();
driver.findElement(By.xpath("//a[@href='Logout.php']")).click();
WebDriverWait waitAlert = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.alertIsPresent());
driver.switchTo().alert().accept();
WebDriverWait wait1 = new WebDriverWait(driver, 20);
wait1.until(ExpectedConditions.elementToBeClickable(userID));
}
@DataProvider(name="data")
public Object[][] getUserData(){
return new Object[][] {
{"mngr137366", "jUgyjAn"},
{"mngr137370", "uvetahA"},
{"mngr137371", "utYmEqY"},
};
}
}
Try this,
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.alertIsPresent());
driver.switchTo().alert().accept();
It will wait until it will not find alert.