[英]Using TestNG notations like @parameters or @dataprovider… every time it skips the method
Everytime it skip the method when I use TESTNG annotations @dataprovider or @Parameters. 每当我使用TESTNG注释@dataprovider或@Parameters时跳过该方法。 My code is mentioned below.
我的代码如下所述。 enter code here
在这里输入代码
package crossbrowser;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class CrossTest
{
WebDriver driver;
@Test
@Parameters("browser")
public void crosstest1(String BrowserName)
{
if(BrowserName.equalsIgnoreCase("firefox"))
{
driver=new FirefoxDriver();
}
String baseUrl = "http://stackoverflow.com/questions/ask";
driver.get(baseUrl);
System.out.print("hello")
}
}
Every I time run this code I get this error 每次我运行此代码我都会收到此错误
SKIPPED: crosstest1
org.testng.TestNGException:
Parameter 'browser' is required by @Test on method crosstest1 but has not been marked @Optional or defined
Same problem is with @DataProvider also. @DataProvider也存在同样的问题。
XML file for the above code is 上面代码的XML文件是
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Test">
<parameter name="browser" value="firefox"/>
<classes>
<class name="crossbrowser.CrossTest"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
On seeing the pasted XML file below closing tag written twice 在看到粘贴的XML文件下面写两次关闭标记
</test>
if it not pasting problem then it should be issue in testng.xml file so update it correctly. 如果它没有粘贴问题,那么它应该是testng.xml文件中的问题,所以正确更新它。
Thank You, Murali 谢谢你,穆拉利
Your xml file to use with @parameters should look like this: 与@parameters一起使用的xml文件应如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" verbose="1">
<test name="Test">
<parameter name="browser" value="chrome"/>
<classes>
<class name="crossbrowser.CrossTest" />
</classes>
</test>
</suite>
In your xml you're targeting wrong class, also you shouldn't run the code but xml config file. 在您的xml中,您的目标是错误的类,您也不应该运行代码而是运行xml配置文件。
In your code parameter name must match with parameters in method: 在您的代码中,参数名称必须与方法中的参数匹配:
@Test
@Parameters("BrowserName")
public void crosstest1(String BrowserName)
Just a heads up before you hit the problem. 在遇到问题之前,请先进行提醒。 You can not pass parameters into data providers from the xml file like you do @test.
您不能像执行@test那样从xml文件将参数传递到数据提供程序。 You will need to something like the following.
您将需要类似以下内容。
public String myTestDataProvider(ITestContext context) throws Exception{
String browser = context.getCurrentXmlTest().getParameter("browser");
//Do Something
return browser;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.