运行TestNG时Selenium配置失败

时间:2017-02-17 07:13:54

标签: selenium parameters testng

在硒中为什么结果为何 默认测试
测试运行:1,失败:0,跳过:1
配置失败:1,跳过:0来了? 为什么会如此。我试图进行测试。

public class NewTest {
WebDriver driver;
@BeforeTest
@Parameters({"browser"})
public void setup(@Optional String browser) throws Exception{
if(browser.equalsIgnoreCase("Firefox")){

  driver = new FirefoxDriver();
  }
else if(browser.equalsIgnoreCase("Chrome")){
 System.setProperty("webdriver.chrome.driver","path");
 driver = new ChromeDriver();
 }
else if(browser.equalsIgnoreCase("IE")){
 System.setProperty("webdriver.ie.driver","path");
 driver = new InternetExplorerDriver();
}
else{
 throw new Exception("Browser is not correct");
}
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
 @Test

public void testParameterWithXML() throws InterruptedException{

     driver.get("http://demo.guru99.com/V4/");
     WebElement userName = driver.findElement(By.name("uid"));
     userName.sendKeys("guru99");
     WebElement password = driver.findElement(By.name("password"));
     password.sendKeys("guru99");
}}

这是我的代码。我试图运行它,但收到错误。

FAILED CONFIGURATION: @BeforeTest setup(null)
java.lang.NullPointerException
at zproject.NewTest.setup(NewTest.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:104)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:515)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:217)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:144)
at org.testng.TestRunner.beforeRun(TestRunner.java:634)
at org.testng.TestRunner.run(TestRunner.java:602)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:387)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:382)
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:1293)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1218)
at org.testng.TestNG.runSuites(TestNG.java:1133)
at org.testng.TestNG.run(TestNG.java:1104)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)

另外

默认测试     测试运行:1,失败:0,跳过:1     配置失败:1,跳过:0

suite.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE suite SYSTEM "testng.org/testng-1.0.dtd">; 
<suite name="TestSuite" thread-count="2" parallel="tests" > 
  <parameter name="browser" value="Chrome" /> 
  <test name="ChromeTest">
   <classes>
     <class name="zproject.NewTest"></class>
   </classes>
  </test>
  <parameter name="browser" value="Firefox" />
  <test name="FirefoxTest">
    <classes>
      <class name="zproject.NewTest"></class>
    </classes> 
  </test>
  <parameter name="browser" value="IE" /> 
  <test name="IETest"> 
    <classes> 
      <class name="zproject.NewTest"> </class> 
    </classes> 
  </test> 
</suite>

1 个答案:

答案 0 :(得分:0)

@Optional表示如果您不配置它,该值将为null。

在您的情况下,我认为您没有,然后browser的值为null并生成NPE。

当您在浏览器出现错误时抛出异常,只需删除@Optional,TestNG将按预期失败。

接下来,您需要suite.xml

<suite name="My suite">
  <parameter name="browser" value="Firefox"/>
  <test name="Simple example">
    <classes>
      <class name="NewTest"/>
    </classes>
  </test>
</suite>

也可以查看the parameter part of the documentation