java - 在 selenium webdriver 中,当我添加参数并运行下面的程序时,显示错误

标签 java selenium-webdriver testng

package webdriver3;

import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Reporter;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class TestingAmazon {

    WebDriver driver;

    @BeforeClass

    public void setUp(){

        driver = new FirefoxDriver();

        driver.get("http://google.co.in");
        driver.manage().window().maximize();
        openAmazon();
    }


    public void openAmazon(){
        driver.findElement(By.xpath("//input[@id='lst-ib']")).sendKeys("Amazon india");
        driver.findElement(By.xpath("//div[@id='sblsbb']/button")).click();
        driver.manage().timeouts().implicitlyWait(2000, TimeUnit.SECONDS);
        driver.findElement(By.xpath("//a[text()='Amazon']")).click();
        hoverLogin();
    }


    public void hoverLogin(){
        Actions builder=new Actions(driver);
        WebElement mainmenu= driver.findElement(By.xpath("//a[@id='nav-link-yourAccount']/span[1]"));
        builder.moveToElement(mainmenu).build().perform();

        WebDriverWait wait = new WebDriverWait(driver, 5); 
        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@id='nav-flyout-ya-signin']/a/span")));
        WebElement submenu=driver.findElement(By.xpath("//div[@id='nav-flyout-ya-signin']/a/span"));
        submenu.click();
        Reporter.log("Done.....",true);
    }


    @Parameters({"userName","password"})
    @Test
    public void enterCreditial(String userName, String password){

        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        WebElement loginbox= driver.findElement(By.xpath("//input[@id='ap_email']"));
        WebElement passwordbox= driver.findElement(By.xpath("//input[@id='ap_password']"));
        WebElement signin= driver.findElement(By.xpath("//input[@id='signInSubmit']"));
            if(loginbox.isDisplayed()){
                Reporter.log("You can enter log in name");
                loginbox.sendKeys(userName);
                    if(passwordbox.isDisplayed()){
                        Reporter.log("You can enter password");
                        passwordbox.sendKeys(password);
                            if(signin.isDisplayed()){
                                Reporter.log("You can signin");
                                signin.click();
                            }
                    }
            }
    }


    public void informationCommand(){
        String titletext=driver.getTitle();
        System.out.println("Title is:" + titletext);

        String pageurl= driver.getCurrentUrl();
        System.out.println("Page url:" + pageurl);

        String windowhandle= driver.getWindowHandle();
        System.out.println("Windowhandle:" + windowhandle);

        Set<String>windowhandles= driver.getWindowHandles();
        System.out.println("Windowhandles:" + windowhandles);
    }


    public void signout(){
        Actions builder=new Actions(driver);
        WebElement mainmenu= driver.findElement(By.xpath("//a[@id='nav-link-yourAccount']/span[1]"));
        builder.moveToElement(mainmenu).build().perform();

        WebDriverWait wait=new WebDriverWait(driver,5);
        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//a[@id='nav-item-signout']/span")));
        WebElement signout=driver.findElement(By.xpath("//a[@id='nav-item-signout']/span"));
        signout.click();
        Reporter.log("Successfuly signout.......",true);
        driver.quit();
    }
}

当我使用 TestNG 脚本运行该程序时,出现以下错误:

Parameter 'userName' is required by @Test on method enterCreditial but has not been marked @Optional or defined in C:\Users\jahansalia\AppData\Local\Temp\testng-eclipse-1960767273\testng-customsuite.xml

最佳答案

我看起来你没有使用 testng.xml

用于填充此方法的参数的变量列表。这些变量必须在 testng.xml 文件中定义。例如

@Parameters({ "xmlPath" })
@Test
public void verifyXmlFile(String path) { ... }

在 testng.xml 中,您需要提供如下详细信息:

<parameter name="xmlPath" value="account.xml" />

引用:-

http://www.tutorialspoint.com/testng/testng_parameterized_test.htm

关于java - 在 selenium webdriver 中,当我添加参数并运行下面的程序时,显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34529942/

相关文章:

java - 在Android Studio中标记为错误代码,但在设备上成功构建gradle并运行

java - 您如何模拟 JavaFX 工具包初始化?

java - 断言失败,即使它不应该

java - LibGDX/Java 游戏 - 制作 4 种随机颜色

java - 实践中的 Http GET 调用失败(通过 web HTTP 测试检查并成功)

javascript - elementExplorer 字符转义在 Protractor 1.8.0 和 2.0.0 上回归?

javascript - 如何通过图像查找元素

java - 使用 Java 检查 Selenium WebDriver 中元素的位置

java - 当我们在类级别实现多线程时,testNG 报告在内部如何工作?

java - Java 方法中的匿名内部 Comparable 类?