2014-03-06 99 views
1

我正面臨奇怪的問題。在寫這裏之前,我已經對此做了一些研究,但無法解決這個問題。參數在TestNG中傳遞默認值

我正在傳遞參數,但它採用參數的默認值,即「無值」。

這裏是我的代碼

 
package rough; 


import org.testng.annotations.AfterMethod; 
import org.testng.annotations.Optional; 
import org.testng.annotations.Parameters; 
import org.testng.annotations.Test; 
import org.testng.annotations.BeforeMethod; 
import org.testng.Assert; 

import java.util.regex.Pattern; 
import java.util.concurrent.TimeUnit; 

import org.junit.*; 

import static org.hamcrest.CoreMatchers.*; 

import org.openqa.selenium.*; 
import org.openqa.selenium.chrome.ChromeDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.ie.InternetExplorerDriver; 
import org.openqa.selenium.support.ui.Select; 

public class Browsers { 
    private WebDriver driver; 
    private String baseUrl; 
    private boolean acceptNextAlert = true; 
    private StringBuffer verificationErrors = new StringBuffer(); 

    @BeforeMethod 
    public void tearDown1() throws Exception { 
     System.out.println("Hello starting"); 

    } 
    @Test 
    @Parameters({ "BROWSER" }) 
    public void setUp(@Optional String BROWSER) throws Exception { 

     if(BROWSER.equalsIgnoreCase("FF")) 
     { 
      driver = new FirefoxDriver(); 
     } 
     else 
     if(BROWSER.equalsIgnoreCase("IE")) 
     {  
      System.setProperty("webdriver.ie.driver", "g:/Selenium Jar Files/IEDriverServer.exe"); 
      driver = new InternetExplorerDriver(); 
     } 

     baseUrl = "http://book.theautomatedtester.co.uk/"; 
     driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 

     System.out.println("Hello starting1111"); 
     driver.get(baseUrl+"/chapter1"); 
     driver.findElement(By.id("radiobutton")).click(); 
     Select(driver.findElement(By.id("selecttype"))).selectByVisibleText("Selenium RC"); 
    } 

    @AfterMethod 
    public void tearDown() throws Exception { 

     String verificationErrorString = verificationErrors.toString(); 
     if (!"".equals(verificationErrorString)) { 
      Assert.fail(verificationErrorString); 
     } 
    } 

    private boolean isElementPresent(By by) { 
     try { 
      driver.findElement(by); 
      return true; 
     } catch (NoSuchElementException e) { 
      return false; 
     } 
    } 

    private boolean isAlertPresent() { 
     try { 
      driver.switchTo().alert(); 
      return true; 
     } catch (NoAlertPresentException e) { 
      return false; 
     } 
    } 

    private String closeAlertAndGetItsText() { 
     try { 
      Alert alert = driver.switchTo().alert(); 
      String alertText = alert.getText(); 
      if (acceptNextAlert) { 
       alert.accept(); 
      } else { 
       alert.dismiss(); 
      } 
      return alertText; 
     } finally { 
      acceptNextAlert = true; 
     } 
    } 
} 

這裏使用的XML是

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> 
<suite name="Suite" parallel="True"> 
    <test name="Test"> 
    <parameter name = "BROWSER" value="FF"/> 
    <classes> 
      <class name="rough.Browsers"/> 
    </classes> 
    <test name="Test"> 
    <parameter name = "BROWSER" value="IE"/> 
    <classes> 
     <class name="rough.Browsers"/> 
    </classes> 
    </test> <!-- Test --> 
</suite> <!-- Suite --> 

當我運行的代碼收到此錯誤:

[TestNG] Running: 
    C:\Users\Administrator\AppData\Local\Temp\testng-eclipse-2137105747\testng-customsuite.xml 

Hello starting 
FAILED: setUp("not-found") 
java.lang.NullPointerException 
    at rough.Browsers.setUp(Browsers.java:55) 
    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:84) 
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714) 
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901) 
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231) 
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127) 
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111) 
    at org.testng.TestRunner.privateRun(TestRunner.java:767) 
    at org.testng.TestRunner.run(TestRunner.java:617) 
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334) 
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329) 
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291) 
    at org.testng.SuiteRunner.run(SuiteRunner.java:240) 
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) 
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) 
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224) 
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149) 
    at org.testng.TestNG.run(TestNG.java:1057) 
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111) 
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204) 
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175) 
+0

任何人請幫助我iam不理解爲什麼iam得到「無價值」 – Learner

+0

我看到htis param:線程「main」中的異常com.beust.jcommander.ParameterException:未知選項:-serport – Vish

回答

8

從TestNG的文檔,這是如何使用@optional的。

@Parameters("db") 
@Test 
public void testNonExistentParameter(@Optional("mysql") String db) { ... } 

If no parameter named "db" is found in your testng.xml file, your test method will receive the default value specified inside the @Optional annotation: "mysql". 

嘗試通過相應地更正您的代碼。 我認爲這應該可以解決問題。如果仍然沒有,那麼在testng.xml中的套件標籤級別聲明參數並對其進行測試。

+0

對不起回覆,謝謝,我更正了我的代碼 – Learner

+0

我看到htis param:線程「main」中的異常com.beust.jcommander.ParameterException:未知選項:-serport any idea – Vish