TestNG:方法需要 2 个参数,但在 @Test 注释中提供了 0 个参数

TestNG: Method requires 2 parameters but 0 were supplied in the @Test annotation

我是硒测试的新手。 基本上, 我有一个 selenium class 像:

import org.testng.annotations.Test;
import org.testng.annotations.Test;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import org.testng.annotations.Test;
import com.google.common.base.Function;

import java.awt.AWTException;
import java.awt.Robot;

import org.openqa.selenium.By;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;
import org.openqa.selenium.firefox.FirefoxDriver;

public class SeleniumTests {

    public void commonFunction(){
        System.setProperty("webdriver.chrome.driver", "C://Downloads//chromedriver_win32//chromedriver.exe");
        WebDriver driver  = new ChromeDriver();
        Actions builder = new Actions(driver);
        driver.get("http://localhost:3000");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        test1(driver,builder);
    }
    //The driver,builder will be used by all the below 3 test functions.

    /**
     * Calling the Most Visited API -- TEST1
     */
    public void test1(WebDriver driver,Actions builder){
        WebElement most_visited_link = driver.findElement(By.id("mostVisited"));
        Thread.sleep(2000);

        Action mouseOvermost_visited_link = builder.moveToElement(most_visited_link).build();
        mouseOvermost_visited_link.perform(); 
        Thread.sleep(2000);
        driver.findElement(By.id("mostVisited")).click();
        Thread.sleep(1000);
        WebElement most_Visited_button = driver.findElement(By.id("datePicked"));
        Action mouse_most_VisitedOverDatePicker = builder.moveToElement(most_Visited_button).build();
        mouse_most_VisitedOverDatePicker.perform(); 
        Thread.sleep(2000);
        driver.findElement(By.id("datePicked")).click();
        Thread.sleep(2000);

        driver.findElement(By.linkText("6")).click();
        Thread.sleep(5000);
        test2(driver,builder);
    }

    /**
     * Calling the Status Pie API  -- TEST2
     * @param builder 
     * @param driver 
     */
    public void test2(WebDriver driver,Actions builder){
        WebElement status_pie_link = driver.findElement(By.id("statusPie"));
        Thread.sleep(2000);
        //Actions builder = new Actions(driver);
        Action mouseOverStatusPie = builder.moveToElement(status_pie_link).build();
        mouseOverStatusPie.perform(); 
        Thread.sleep(1000);
        driver.findElement(By.id("statusPie")).click();
        Thread.sleep(1000);

        WebElement status_pie_button = driver.findElement(By.id("datePicked"));
        Action mouseOver_Status_pie_DatePicker = builder.moveToElement(status_pie_button).build();
        mouseOver_Status_pie_DatePicker.perform(); 
        Thread.sleep(2000);
        driver.findElement(By.id("datePicked")).click();
        Thread.sleep(2000);
        driver.findElement(By.linkText("6")).click();
        Thread.sleep(5000);
        test3(driver,builder);
    }

    /**
     * Calling the Old Data API -- TEST3
     */
    public void test2(WebDriver driver,Actions builder){
        WebElement old_data_link = driver.findElement(By.id("oldData"));
        Thread.sleep(2000);
        // Actions builder = new Actions(driver);
        Action mouseOverOldData = builder.moveToElement(old_data_link).build();
        mouseOverOldData.perform(); 
        Thread.sleep(2000);
        driver.findElement(By.id("oldData")).click();
        Thread.sleep(1000);

        WebElement old_data_button = driver.findElement(By.id("datePicked"));
        Action mouseOverDatePicker = builder.moveToElement(old_data_button).build();
        mouseOverDatePicker.perform(); 
        Thread.sleep(2000);
        driver.findElement(By.id("datePicked")).click();
        Thread.sleep(2000);
        driver.findElement(By.linkText("6")).click();
        Thread.sleep(5000);
        driver.quit();
    }

}

代码在浏览器上按预期执行并关闭 chrome window。 但是,在索引报告中,我看到日志为:

3 methods, 2 skipped, 1 passed 
Skipped methods :    
 mostVisitedfunction   
 statuspiefunction   

Passed methods :    
 createConnection  

知道我哪里出错了吗? 提前致谢。

方法 mostVisitedfunction 不应该有注释,因为它是 yoyr 测试用例的帮助方法。而是创建一个新的 class,在 @test class 中创建一个相同的对象,然后调用该方法..

看下面的例子..

我的测试是

//MAximize the Screen
    driver.manage().window().maximize(); 
//Go to Gmail Login Page
    SignInPage SignInPage = WebUtils.GoToSignInPage(driver);
//Sign in to Login page -Send Username 
    SignInPage.SendkeysMethodForSignInPAge(driver, By.cssSelector("input[id='Email']") , "kishanpatelllll.8@gmail.com" );
//Click on Next
    SignInPage.ClickToLogin(driver, By.cssSelector("input[id='next']"));
//Wait for password field to be visible
    SignInPage.WaitForElementTobeVisible(driver, By.cssSelector("input[id='Passwd'][type='password']"));

所以当我调用方法 SendkeysMethodForSignInPAge 时,我不会将它写在@Test 中。

参见 SendkeysMethodForSignInPAge 方法: public class 登录页面 {

public void SendkeysMethodForSignInPAge(WebDriver driver, By by, String s) {
    WebUtils.Sendkeys(driver,by,s);
}

我创建了一个新的 class 并在那里定义了它。 这是基本流程。 希望你能把这个联系起来。 如果您仍然卡住,请回复我。 快乐学习:-)

出现错误是因为如果您的@Test 注释方法中有参数,那么这些参数需要来自@Parameters 或@DataProvider。否则,如果注释的 none 提供了参数,这是您的情况,则会抛出错误。

除此之外,@Kishan 在代码结构方面的建议是正确的。您需要区分测试和常用功能。

@Test 
public void assertBackToLogin(WebDriver driver) throws InterruptedException {
    LoginPage login = new  LoginPage (driver); 
    login.assertLogin();
}

不工作

删除所有 参数运行

@Test 
public void assertBackToLogin() throws InterruptedException {
    LoginPage login = new  LoginPage (driver); 
    login.assertLogin();
}