Jay Hansalia
Jay Hansalia

Reputation: 1

In selenium webdriver when I add parameter and run the progrmme below error is showing

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

When I run this programme using TestNG script, I get the following error:

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

Upvotes: 0

Views: 1202

Answers (1)

Shubham Jain
Shubham Jain

Reputation: 17593

I looks like you are not using testng.xml

The list of variables used to fill the parameters of this method. These variables must be defined in your testng.xml file. For example

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

and in testng.xml you need to give detail like this:

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

Reference :-

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

Upvotes: 1

Related Questions