使用DataProviders登陆3次只成功1次
Using DataProviders to login 3 times but only succeeding one time
我正在学习 TestNG for selenium。我想将三个不同的用户名和密码传递给@Test,这是登录场景。场景是:
- 进入登录页面
- 单击用户名输入字段并输入用户名
- 点击密码输入框并输入密码
- 点击登录按钮
- 点击注销按钮
- 单击警报上的 "ok"。
第一个测试即将通过。另外两个因 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"},
};
}
}
更新:
通过处理警报和删除硬编码的用户名,代码现在可以正常工作了。
但是浏览器被打开了3次,3次登录。
我希望它打开一次并执行三次登录。
为此,我添加了以下代码:
@BeforeClass
public void setUp() {
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://demo.guru99.com/V1/index.php");
}
并且从 login() 函数中删除了相同的内容。现在第一次登录才成功。剩下另外两个登录。
总代码:
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"},
};
}
}
试试这个,
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.alertIsPresent());
driver.switchTo().alert().accept();
它将一直等到找不到警报为止。
看起来,您在登录测试中对用户名进行了硬编码method.hence其余两个输入的测试失败(无效的用户 ID 到密码映射抛出有效登录身份验证错误)
将用户名分配给 Userid 元素字段后,所有测试均已通过。
修改后的登录码:
@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();
}
我正在学习 TestNG for selenium。我想将三个不同的用户名和密码传递给@Test,这是登录场景。场景是:
- 进入登录页面
- 单击用户名输入字段并输入用户名
- 点击密码输入框并输入密码
- 点击登录按钮
- 点击注销按钮
- 单击警报上的 "ok"。
第一个测试即将通过。另外两个因 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"},
};
}
}
更新: 通过处理警报和删除硬编码的用户名,代码现在可以正常工作了。 但是浏览器被打开了3次,3次登录。 我希望它打开一次并执行三次登录。 为此,我添加了以下代码:
@BeforeClass
public void setUp() {
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://demo.guru99.com/V1/index.php");
}
并且从 login() 函数中删除了相同的内容。现在第一次登录才成功。剩下另外两个登录。
总代码:
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"},
};
}
}
试试这个,
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.alertIsPresent());
driver.switchTo().alert().accept();
它将一直等到找不到警报为止。
看起来,您在登录测试中对用户名进行了硬编码method.hence其余两个输入的测试失败(无效的用户 ID 到密码映射抛出有效登录身份验证错误)
将用户名分配给 Userid 元素字段后,所有测试均已通过。
修改后的登录码:
@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();
}