英文:
Counting number of successes and failures in TESTNG and Java (selenium)
问题
我有一个小的JAVA程序,用于登录一个网站
package testingProgram;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class testingProgram {
@Test
public void helloWorldTest() {
System.out.println("Hello World!");
System.setProperty("webdriver.chrome.driver","C:\\Users\\Steve\\Desktop\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://website234/backend/");
driver.findElement(By.id("username")).sendKeys("admin");
driver.findElement(By.id("login")).sendKeys("admin");
driver.close();
WebDriver driver2 = new ChromeDriver();
driver2.get("https://website234/backend/");
driver2.findElement(By.id("username")).sendKeys("admin");
driver2.findElement(By.id("login")).sendKeys("admin2");
}
}
注意:在我的示例中,正确的登录信息是:
用户名 = admin 和密码 = admin
它打开2个Chrome会话并尝试使用不同的密码登录。
如果登录失败,它会抛出以下错误消息:
我的TestNG测试显示如下:
但我希望输出告诉我有多少个测试通过了,而在我的情况下,只有一个用户名和密码正确。
预期结果:
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Passes: 1 (username = admin, password = admin), Failures: 1 (username = admin, password = admin2), Skips: 0
因为我能够使用用户名 = admin 和密码 = admin登录,但无法使用用户名 = admin 和密码 = admin2登录。
如何实现类似这样的结果?
英文:
I have a little JAVA program that login into a website
package testingProgram;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class testingProgram {
@Test
public void helloWorldTest() {
System.out.println("Hello World!");
System.setProperty("webdriver.chrome.driver","C:\\Users\\Steve\\Desktop\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://website234/backend/");
driver.findElement(By.id("username")).sendKeys("admin");
driver.findElement(By.id("login")).sendKeys("admin");
driver.close();
WebDriver driver2 = new ChromeDriver();
driver2.get("https://website234/backend/");
driver2.findElement(By.id("username")).sendKeys("admin");
driver2.findElement(By.id("login")).sendKeys("admin2");
}
}
> Note: in my example the correct login is:
>
> username = admin and password = admin
It opens 2 chrome sessions and tries to login by using different passwords.
If it fails login it throws and error message like this:
My testng test shows this:
But i want the output to tell me how many tests passed and in my case only one username and password worked correctly.
Expected result:
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Passes: 1 (username = admin, password = admin), Failures: 1 (username = admin, password = admin2), Skips: 0
because i was able to login by using username = admin and password = admin but it wont be able to login using username = admin and password = admin2
How can i achieve something like that?
答案1
得分: 1
- 即使您已经初始化了两个驱动程序,您的脚本只被识别为一个测试。
- 您需要设定一个条件来提交成功的登录。
- 对于
username
和password
,似乎您希望记录日志,但看起来根本没有设置。
方法:
- 为每个测试创建两个
@Test
注解。 - 标记成功登录的样子,大多数情况下是**
welcome home
**。 - 设置日志,使用:
import org.testng.Reporter;
@Test
public void helloWorldTest1() {
....
// 设置日志
String username = "admin";
String password = "admin";
Reporter.log("username = " + username, true);
Reporter.log("password = " + password, true);
driver.get("https://website234/backend/");
driver.findElement(By.id("username")).sendKeys(username);
driver.findElement(By.id("login")).sendKeys(password);
// 在这里,我认为需要提交登录按钮
buttonLogin.click();
// 在这里,找到一个欢迎首页的定位器可能会有帮助
WebElement welcomeHome = driver.findElement(By.name("a locator"));
}
@Test
public void helloWorldTest2() {
....
// 设置日志
String username = "admin";
String password = "admin2";
Reporter.log("username = " + username, true);
Reporter.log("password = " + password, true);
driver.get("https://website234/backend/");
driver.findElement(By.id("username")).sendKeys(username);
driver.findElement(By.id("login")).sendKeys(password);
// 在这里,我认为需要提交登录按钮
buttonLogin.click();
// 在这里,找到一个欢迎首页的定位器可能会有帮助
WebElement welcomeHome = driver.findElement(By.name("a locator"));
}
注意:您还可以在默认报告中查看日志 emailable-report.html
:
顺便说一下,仍然可以使用一个**@Test
**来实现这一点,您可以了解有关@DataProvider
和invocationcount
以重复使用不同参数运行此测试的信息。
英文:
Something wrong:
- Even you have 2 initialize driver, your script recognized as one test only.
- You have to give a condition how to submit a successful login.
- For
username
andpassword
, looks like you want the logs, but it doesn't look like it's set at all.
Approach:
- Create 2
@Test
annotation for each test. - Mark what a successful login looks like., mostly with
welcome home
- Logs set, with :
import org.testng.Reporter;
@Test
public void helloWorldTest1() {
....
//logs set
String username = "admin";
String password = "admin";
Reporter.log("username = " +username, true);
Reporter.log("password = " +password, true);
driver.get("https://website234/backend/");
driver.findElement(By.id("username")).sendKeys(username);
driver.findElement(By.id("login")).sendKeys(password);
//here, i think need to submit login button
buttonLogin.click();
//here, find a welcome home locator maybe
WebElement welcomeHome = driver.findElement(By.name("a locator"));
}
@Test
public void helloWorldTest2() {
....
//logs set
String username = "admin";
String password = "admin2";
Reporter.log("username = " +username, true);
Reporter.log("password = " +password, true);
driver.get("https://website234/backend/");
driver.findElement(By.id("username")).sendKeys(username);
driver.findElement(By.id("login")).sendKeys(password);
//here, i think need to submit login button
buttonLogin.click();
//here, find a welcome home locator maybe
WebElement welcomeHome = driver.findElement(By.name("a locator"));
}
Note : You can see logs in default report too emailable-report.html
:
Btw there is way to achive this still with one @Test
, you can learn about @DataProvider
and invocationcount
to repeat this test with various parameter.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论