我在TestNG下面的错误。不确定为什么
以下是完整错误: 失败:launchB org.testng.TestNGException:
无法使用[class java.lang.String]注入@Test带注释的方法[launchB]。
For more information on native dependency injection please refer to http://testng.org/doc/documentation-main.html#native-dependency-injection
at org.testng.internal.Parameters.checkParameterTypes(Parameters.java:244)
at org.testng.internal.Parameters.createParameters(Parameters.java:172)
at org.testng.internal.Parameters.createParameters(Parameters.java:458)
at org.testng.internal.Parameters.handleParameters(Parameters.java:569)
at org.testng.internal.Invoker.handleParameters(Invoker.java:1308)
at org.testng.internal.Invoker.createParameters(Invoker.java:1036)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1126)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:744)
at org.testng.TestRunner.run(TestRunner.java:602)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
at org.testng.SuiteRunner.run(SuiteRunner.java:289)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
at org.testng.TestNG.runSuites(TestNG.java:1144)
at org.testng.TestNG.run(TestNG.java:1115)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
以下是代码:
public class LaunchBrowser_2 {
WebDriver driver;
String browser = "IE";
@Test
public void launchB(String browser){
System.setProperty("webdriver.gecko.driver", "D:\\geckodriver\\geckodriver.exe");
System.setProperty("webdriver.chrome.driver", "D:\\Chromedriver\\chromedriver.exe");
System.setProperty("webdriver.ie.driver", "D:\\IEDriver\\IEDriverServer.exe");
if(browser.equals("Mozilla"))
driver = new FirefoxDriver();
else if (browser.equals("Chrome"))
driver = new ChromeDriver();
else if (browser.equals("IE")){
DesiredCapabilities cap = DesiredCapabilities.internetExplorer();
cap.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
driver = new InternetExplorerDriver(cap);
}
driver.manage().window().maximize();
//driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.rediff.com");
}
答案 0 :(得分:1)
当您定义方法launchB()
时,您已接受参数String browser
。您无法提供同一个类中的String browser
,因为方法launchB()
已使用@Test
注释。
解决方案是接受String browser
中的参数testng.xml
。所以你必须做一些修改。删除/评论第String browser = "IE";
行。在@Parameters("browser")
注释上方添加@Test
。接下来,将System.setProperty
行移动到相应的if()
块中。最后,为方便起见,将browser.equals()
替换为browser.equalsIgnoreCase
。最后的代码块将如下所示:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class Q45523264_inject
{
WebDriver driver;
@Parameters("browser")
@Test
public void launchB(String browser)
{
if(browser.equalsIgnoreCase("Mozilla"))
{
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
driver = new FirefoxDriver();
}
else if (browser.equalsIgnoreCase("Chrome"))
{
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
driver = new ChromeDriver();
}
else if (browser.equalsIgnoreCase("IE"))
{
System.setProperty("webdriver.ie.driver", "C:\\Utility\\BrowserDrivers\\IEDriverServer.exe");
DesiredCapabilities cap = DesiredCapabilities.internetExplorer();
cap.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
driver = new InternetExplorerDriver(cap);
}
driver.get("http://www.rediff.com/");
}
}
现在,您必须通过Convert to TestNG
转换您的计划。您的testng.xml
已生成。接下来,您已从String browser
传递testng.xml
的参数。您必须从<parameter name="browser" value="Mozilla"/>
传递参数<parameter name="browser" value="IE"/>
或<parameter name="browser" value="Chrome"/>
(现已提交)或testng.xml
(现已提交)。您的testng.xml
将如下所示:
<suite name="Suite">
<test name="My Test">
<parameter name="browser" value="Mozilla"/>
<!-- <parameter name="browser" value="IE"/> -->
<!-- <parameter name="browser" value="Chrome"/> -->
<classes>
<class name="demo.Q45523264_inject"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->