parameter is required by @ Test but has not been marked @optional or defined

Viewed 38738

Hi I am seeing this error, please help

Parameter 'Visit' is required by @Test on method searchByVisitNo but has not been marked @Optional or defined.

I don't know why is there a need to mark it optional when it is defined in testng xml file

Here is the entire code I used

<suite name="Suite" parallel="tests">
    <test name="SearchByVisit">
        <parameter name="Visit" value="123456"/>
        <classes>
            <class name="abc"/>
        </classes>
    </test>
</suite>

@Parameters({"Visit"})
@Test(priority=3)
public void searchByVisitNo(String VisitNumber)throws InterruptedException
{
    searchByVisit(VisitNumber);
}

public void searchByVisit(String Visit) throws InterruptedException
{
    WebDriverWait wait = new WebDriverWait(driver, 20);
    WebElement element = wait.until(
        ExpectedConditions.visibilityOfElementLocated(By.id("search-input"))
    );
    element.sendKeys(Visit);
    clickSearch();
}
14 Answers

In your TestNG.xml file, you need to provide your test class name and in class name you need to provide the class name along with package. That would resolve the issue!

Consider below example for reference :

If the test class name is Testclass and it's under package com.testpackage, then your testNG.xml file would look like below:

<suite name="Suite" parallel="tests">
    <test name="Testclass">
        <parameter name="Visit" value="123456"/>
        <classes>
            <class name="com.testpackage.Testclass"/>
        </classes>
    </test>
</suite>

Even I was facing the same issue the problem simple ..

  1. In Testng XML I was passing the parameters in the day1 code instead of day4
  2. Pass these all parameter to correct test cases folder in testng XML

Consider belows Day1 code and Day4 code two are there

In Testng xml passing the parameter in day4 instead of day1 will throw this error

Day 1

package test2;

import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class Day1 {
@Parameters({"URL"}) //Only to Particular test method only
    @Test
    public void test11(String urlname)
    {
        System.out.println(urlname);
        System.out.println("Test case 11 in Day1");     
    }
    @Test
    public void test12()
    {
        System.out.println("Test case 12 in Day1");
    }
    @Test
    public void test13()
    {
        System.out.println("Test case 13 in Day1\n");
    }

}

Day4

package test2;

import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class Day4 {

@Parameters({"URL","API/username"})
    @Test
    public void test41(String urlname, String name)
    {
        System.out.println(urlname);
        System.out.println("Test case 41 in Day4");
        System.out.println(name);       
    }
    @Test
    public void test42()
    {
        System.out.println("Test case 42 in Day4");
    }

}

XML ERROR

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">

  <parameter name = "URL" value = "https://www.youtube.com"/>

 <**test name = "Day2Testcase">
   <parameter name = "URL" value = "day2test2.com"/>
   <parameter name = "API/username" value = "12345"/>** 

    <classes>
        <class name = "test2.Day1">
        </class> 
    </classes>

 </test>
  <test name = "Day4Testcase">
   <parameter name = "URL" value = "day4test2.com"/>
    <classes>
        <class name = "test2.Day4">
        </class> 
    </classes>   
 </test>

ERROR

[Utils] [ERROR] [Error] org.testng.TestNGException: Parameter 'API/username' is required by @Test on method test41 but has not been marked @Optional or defined in F:\Selenium WebDriver with Java -Basics to Advanced+Frameworks by Rahul Shetty\Workspace\TestNG_Tutorial\testng.xml

Corrected the XML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="Suite">

 <test name = "Day2Testcase">
   <parameter name = "URL" value = "day2test2.com"/>
    <classes>
        <class name = "test2.Day1">
        </class> 
    </classes>

 </test>
  <test name = "Day4Testcase">
   <parameter name = "URL" value = "day4test2.com"/>
   <parameter name = "username" value = "12345"/> 
    <classes>
        <class name = "test2.Day4">
        </class> 
    </classes>   
 </test>

</suite> <!-- Suite -->

Step1:- In Testng.xml define class name as: "packagname.abc"

Step2:- Now right click on testng.xml and Run As-> TestNG Suite.

Note: If you directly run testng class "abc.java" with Testng Test command it will not pic parameters value from testng.xml so you will have to run the testng.xml instead of running testNG class "abc.java" as TestNG Test

You need to add @Optional("Abc") in your method, which specifies that the current parameter is optional. TestNG will pass in a specified default value, or null if none is specified.

Try doing public void searchByVisit(@Optional("Abc") String Visit) throws InterruptedException

Are you using a Maven repo? Try running your pom.xml as Maven clean and then Maven test. This worked for me

My mistake was I was passing parameter keyword as "Parameter" (P-capital)

So, I faced the same issue while passing the parameter so I added @Optional to my function parameter.

@Test(priority = 2)
@Parameters("myName")
public void LoginAccountProcess(@Optional("Abc")String name) throws
FileNotFoundException, IOException, InterruptedException {
System.out.println("Name of th login Id is ==>"+name);
WebDriver chromeDriverObj=reusableComp.getLoadDriver();
chromeDriverObj.get(reusableComp.getUrl());

Eclipse Editor provides running from both XML and directly from the test file. Try that or run with XML file by using 'mvn test' command.

Seems the issue is with using curly-braces. I had tried with below snippet:

@Parameters({"browser"})
@BeforeMethod
public void setUp(String browser){
    if(browser.equalsIgnoreCase("Mozilla Firefox")){
    driver = new FirefoxDriver();
}

was getting the below error:

Parameter '{browser}' is required by BeforeMethod on method setUp but has not been marked @Optional or defined

It started working after removing the curly-braces as below:

@Parameters("browser")

After Opening browser, no URL is being passed and it last with Error which says "Parameter 'browser' is required by BeforeTest on method setup but has not been marked @Optional or defined in C:\Users\hassan.anwer\IdeaProjects\CRBTesting\testing.xml"

Related