当前位置: 首页 > 知识库问答 >
问题:

在SeleniumWebDriver中,当我添加参数并运行下面的程序时,会显示错误

郭翰翮
2023-03-14
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脚本运行此程序时,出现以下错误:

参数'userName'是@Test on method entCredisive所必需的,但未在C:\用户\jahansalia\AppData\本地\Temp\testng-eclipse-1960767273中标记@可选或定义\testng-customsuite.xml

共有1个答案

嵇丰
2023-03-14

我看起来你没有用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

 类似资料: