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

时间:2016-05-12 00:45:14

标签: selenium selenium-webdriver testng

我是硒测试的新手。 基本上, 我有一个像以下的硒类:

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窗口。 但是,在索引报告中,我将日志视为:

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

Passed methods :    
 createConnection  

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

3 个答案:

答案 0 :(得分:1)

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

见下面的例子..

我的测试是

//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方法: 公共类SignInPage {

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

我创建了一个新类,并在那里定义了它。 这是基本流程。 希望你能谈谈这个。 回复我,如果你仍然卡住了。 快乐学习: - )

答案 1 :(得分:1)

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

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

答案 2 :(得分:1)

@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();
}