java 在使用TestNG的selenium自动化中出现“No tests found. Nothing was run”错误

dohp0rv5  于 2023-04-19  发布在  Java
关注(0)|答案(4)|浏览(340)

下面给出的是我在eclipse中使用selenium编写的代码。

import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
class GoogleSearchTest {

    @Test
    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.gecko.driver","C:\\Users\\acer\\Downloads\\selenium\\geckodriver.exe");
        WebDriver driver =new FirefoxDriver();
        driver.get("https://www.google.com/");
        WebElement we1 = driver.findElement(By.name("q"));
        we1.sendKeys("GMAIL");
        we1.sendKeys(Keys.ENTER); 
        WebDriverWait wait1 = new WebDriverWait(driver, 5);
        wait1.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h3[text()='E-mail - Sign in - Google Accounts']"))).click();
        Thread.sleep(10000); 
        driver.findElement(By.id("identifierId")).sendKeys("2017cs102@stu.ucsc.cmb.ac.lk");
        driver.findElement(By.xpath("//*[@id='identifierNext']/div/span/span")).click();
        Thread.sleep(10000);
        driver.findElement(By.name("password")).sendKeys("mmalsha425@gmail.com");
        driver.findElement(By.xpath("//*[@id='passwordNext']/div/span/span")).click();

    }

}

它给出以下错误。

[TestNG] No tests found. Nothing was run
Usage: <main class> [options] The XML suite files to run

我已经安装了TestNG插件。我可以做些什么来解决这个问题??

jjjwad0x

jjjwad0x1#

更改主函数名。在使用TestNG时不应使用它

@Test
public void test() throws InterruptedException {
    System.setProperty("webdriver.gecko.driver","C:\\Users\\acer\\Downloads\\selenium\\geckodriver.exe");
    WebDriver driver =new FirefoxDriver();
dfuffjeb

dfuffjeb2#

你不需要写main()方法,TestNg自己做。

class GoogleSearchTest {
    @Test
    public void test() throws InterruptedException{
        //your code here
        .....
    }
}

如果您想从main()呼叫,只需:

import org.testng.TestNG;

public class Master {
    public static void main(String[] args) {
        TestNG testng = new TestNG();
        testng.setTestClasses(new Class[] { GoogleSearchTest.class });
        testng.run();
    }
}

但是注意,在GoogleSearchTest类中,不要为@Test放置**public static void main**

hgtggwj0

hgtggwj03#

此错误消息...

[TestNG] No tests found. Nothing was run
Usage: <main class> [options] The XML suite files to run

...意味着 TestNG 没有找到任何测试,因此没有执行任何测试。

测试NG

TestNG是一个基于注解的测试框架,它需要一个标记注解类型来指示一个方法是一个测试方法,并且应该由测试工具运行。因此,当使用testng时,就像使用annotations一样,你不必显式地调用main()方法。

解决方案

简单的解决方案是将main()方法替换为测试方法名称,例如 test(),并保留@Test注解如下:

// your imports

class GoogleSearchTest {

    @Test
    public void test() throws InterruptedException {
        System.setProperty("webdriver.gecko.driver","C:\\Users\\acer\\Downloads\\selenium\\geckodriver.exe");
        // other lines of code
    }
}
8ulbf1ek

8ulbf1ek4#

public static void main(String[] args)throws InterruptedException {
删除static和main,并重新构造为public void search(){ -----你的代码----
}

相关问题