在TESTNG和Java(selenium)中计算成功和失败的数量。

huangapple go评论89阅读模式
英文:

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和Java(selenium)中计算成功和失败的数量。

我的TestNG测试显示如下:

在TESTNG和Java(selenium)中计算成功和失败的数量。

但我希望输出告诉我有多少个测试通过了,而在我的情况下,只有一个用户名和密码正确。

预期结果:

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:

在TESTNG和Java(selenium)中计算成功和失败的数量。

My testng test shows this:

在TESTNG和Java(selenium)中计算成功和失败的数量。

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

  1. 即使您已经初始化了两个驱动程序,您的脚本只被识别为一个测试。
  2. 您需要设定一个条件来提交成功的登录。
  3. 对于usernamepassword,似乎您希望记录日志,但看起来根本没有设置。

方法:

  1. 为每个测试创建两个@Test注解。
  2. 标记成功登录的样子,大多数情况下是**welcome home**。
  3. 设置日志,使用: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"));
}

控制台输出:
在TESTNG和Java(selenium)中计算成功和失败的数量。

注意:您还可以在默认报告中查看日志 emailable-report.html

在TESTNG和Java(selenium)中计算成功和失败的数量。

顺便说一下,仍然可以使用一个**@Test**来实现这一点,您可以了解有关@DataProviderinvocationcount以重复使用不同参数运行此测试的信息。

英文:

Something wrong:

  1. Even you have 2 initialize driver, your script recognized as one test only.
  2. You have to give a condition how to submit a successful login.
  3. For username and password, looks like you want the logs, but it doesn't look like it's set at all.

Approach:

  1. Create 2 @Test annotation for each test.
  2. Mark what a successful login looks like., mostly with welcome home
  3. 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"));
}

Console output:
在TESTNG和Java(selenium)中计算成功和失败的数量。

Note : You can see logs in default report too emailable-report.html:

在TESTNG和Java(selenium)中计算成功和失败的数量。

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.

huangapple
  • 本文由 发表于 2023年1月9日 05:21:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051399.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定