Reputation: 71
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();
}
Upvotes: 7
Views: 57327
Reputation: 71
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.
Upvotes: 0
Reputation: 250
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
Upvotes: 1
Reputation: 11
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());
Upvotes: 0
Reputation: 1
My mistake was I was passing parameter keyword as "Parameter" (P-capital)
Upvotes: 0
Reputation: 1
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"
Upvotes: -1
Reputation: 11
Even I was facing the same issue the problem simple ..
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 -->
Upvotes: 1
Reputation: 21
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>
Upvotes: 1
Reputation: 2693
Are you using a Maven repo? Try running your pom.xml as Maven clean and then Maven test. This worked for me
Upvotes: 0
Reputation: 104
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
Upvotes: 0
Reputation: 1
Mention below line at the start of your XML file:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
Upvotes: 0
Reputation: 1
You should try updating your pom with below. Simply add the below in your pom and then execute. Add it just above dependencies tag.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
Upvotes: 0
Reputation: 59
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")
Upvotes: -1
Reputation: 1289
You are passing paramter <parameter name="Visit" value="123456"/>
in your .xml file and you directly running your TestNG class. So it's not getting that parameter while compiling .
So you need to run your xml suite to provide a valid parameter to your TestNG class.
Upvotes: 10
Reputation: 732
You are getting this error as you are directly running this class. You should run your TestNG XML file.
Steps which you can follow:
Create a test suite.
Specify class and method name(Class name should be packageName.className)
Specify the parameters which should be used for a method.
Run the test suite.
Looks like you are directly trying to run the class hence it is showing that exception.
Upvotes: 3