2017-11-25 196 views
0

之前運行的方法我有一個參數化方法(readConfig_1(String path))被稱爲@onStart方法myListener的類在TestNG的套房如何參數傳遞給@onStart方法myListener的類或@onStart

這裏的參數化方法,我已經寫了,它實際上需要一個路徑來存儲XML文件。

public static void readConfig_1(String configXmlPath) 
{ 

      browser = CoreLib.fGetNodeText(configXmlPath, "config", 
        "browser"); 
      env = CoreLib.fGetNodeText(configXmlPath, "config", "env"); 
      release = CoreLib.fGetNodeText(configXmlPath, "config", "release"); 
      serverName = CoreLib.fGetNodeText(configXmlPath, env, 
        "serverName"); 
      host = CoreLib.fGetNodeText(configXmlPath, env, "host"); 
      userName = CoreLib.fGetNodeText(configXmlPath, env, "userName"); 
      passWord = CoreLib.fGetNodeText(configXmlPath, env, "passWord"); 
      portNumber = CoreLib.fGetNodeText(configXmlPath, env, 
        "portNumber"); 
      schema = CoreLib.fGetNodeText(configXmlPath, env, "schema"); 
      url = CoreLib.fGetNodeText(configXmlPath, env, "url"); 
      screenShotForPass=CoreLib.fGetNodeText(configXmlPath, env, "SCreenShotForPass"); 

      screenShotForFail=CoreLib.fGetNodeText(configXmlPath, env, "SCreenShotForFail"); 


      CoreLib.LOGGER.info("****************************************************"); 
      CoreLib.LOGGER.info("   Configuration Details     "); 
      CoreLib.LOGGER.info("****************************************************"); 

      CoreLib.LOGGER.info("Browser ::" + browser); 
      CoreLib.LOGGER.info("env ::" + env); 
      CoreLib.LOGGER.info("serverName ::" + serverName); 
      CoreLib.LOGGER.info("host ::" + host); 
      CoreLib.LOGGER.info("userName ::" + userName); 
      CoreLib.LOGGER.info("passWord ::" + passWord); 
      CoreLib.LOGGER.info("portNumber ::" + portNumber); 
      CoreLib.LOGGER.info("schema ::" + schema); 
      CoreLib.LOGGER.info("url::" + url); 
      CoreLib.LOGGER.info("ScreenSnapShotForPass::"+screenShotForPass); 
      CoreLib.LOGGER.info("ScreenSnapShotForFail::"+screenShotForFail); 

     } 

在這TestNG的套房下面看到,我需要調用傳遞一個參數,它上面的方法可以繼續運行寫入另一個類的測試之前。

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> 
<suite name="Smoke Suite" parallel="false" preserve-order="true"> 

    <listeners> 
     <listener class-name="com.healthcare.reports.MyListener"></listener> 
    </listeners> 



    <test name="XYZ Tests"> 
     <classes> 

      <class name="com.healthcare.utility.Config"> 
       <methods> 
        <include name="readConfig_1"></include> 
       </methods> 
      </class> 

      <class name="com.healthcare.businessLib.xyz.AddUserTests" /> 

     </classes> 

    </test> 


</suite> 

通過這個我打算來限制TestNG的套房閱讀,這將有自己的價值觀,如信封,URL,瀏覽器等,可以執行測試之前設置一個特定的config.xml文件。有沒有辦法實現這一目標?

- 通過對其他帖子的建議,我認爲將readConfig_1添加到@BeforeClass註釋可以解決問題。但還有更多的-----

我的聽衆類別有@onStart註釋需要配置文件在套件的開始運行。如您在下面看到的,我的Listener類具有來自Config文件的變量release_1

public class MyListener implements ITestListener, ISuiteListener { 

     // This belongs to ISuiteListener and will execute before the Suite start 

     ReportLib report=new ReportLib(); 

     @Override 
     public void onStart(ISuite arg0) { 

      Config.readConfig_1(configXlsPath); 
      ExportTestResults export = new ExportTestResults(); 
      export.exportExcelHeader(Config.release_1); 
      CoreLib.fCreateLogger(Config.release_1); 

     } 

但如果我把它放在@BeforeClass在識別TestClass這些變量(Config.release_1)正在返回空,因爲他們將測試類之前運行。所以我需要readconfig_1()到之前或與Listener類運行,並且無法向onStart(ISuite arg0)添加參數。而且我也不能在@BeforeClass

運行readConfig方法

通過試了幾件事情:

  1. 運行中的testng.xml的readConfig_1()作爲第一種方法監聽器類可稱爲甚至在。

  2. 將@BeforeClass註釋放入Listener類中,其中參數化爲readConfig_1()的方法,希望在執行onStart()之前調用readConfig。

    public class MyListener2 implements ITestListener, ISuiteListener{ 
    

    ReportLib report = new ReportLib();

    @BeforeClass 
        @Parameters("configXlsPath") 
        public void beforeSuite(String configXlsPath) 
        { 
         Config.readConfig_1(configXlsPath); 
        } 
    
    
        @Override 
        public void onStart(ISuite arg0) { 
         ExportTestResults export = new ExportTestResults(); 
         System.out.println("In onStart(Listener_2)"+ Config.release_1); 
         export.exportExcelHeader(Config.release_1); 
         CoreLib.fCreateLogger(Config.release_1); 
    
        } 
    } 
    

但沒有奏效。現在有沒有辦法解決這個問題?

---轉貼我以前---

回答

0

這個問題,把你的public static void readConfig_1(String configXmlPath)在一個班,進行測試和<listener>之前調用測試,並在監聽器類這樣

<suite name="Default suite" thread-count="5" group-by-instances="false" verbose="1" allow-return-values="false" parallel="false" data-provider-thread-count="10" junit="false" configfailurepolicy="skip" preserve-order="true" skipfailedinvocationcounts="false"> 
    <test verbose="2" name="pTest"> 

    <classes> 
     <class name="firsttestngpackage.customParameter"> 
      <methods> 
      <include name="getParams"/> 
     </methods> 
     </class> 
    </classes> 
    </test> 
    <listeners> 
     <listener class-name = "firsttestngpackage.myListener1" /> 
    </listeners> 
    ... 
    ... 
閱讀方法下面

customeParameter class,這類似於你readConfig_1

package firsttestngpackage; 



public class customParameter { 

    public static String getParams() { 

     return new String ("testParam1"); 
     } 
} 

listener class

package firsttestngpackage; 

import org.testng.ITestContext; 
import org.testng.ITestListener; 
import org.testng.ITestResult; 
import firsttestngpackage.customParameter; 

public class myListener1 implements ITestListener { 

    String tParam = customParameter.getParams(); 
    @Override 
    public void onTestSuccess(ITestResult result) { 


     System.out.println("the method" + " "+result.getMethod().getMethodName() +" "+ "passed and custom parameter is " + tParam); 
    } 

上面打印

the method gotoURL passed and custom parameter is testParam1

gotoURL是我沒有在此示出在我的測試類的方法。您應該可以使用readConfig_1方法與readConfig_1方法一樣完成,並在您的聽衆類中調用它,如yourClass.readConfig_1

+0

感謝您的建議。今天我會試試這個,讓你知道這對我有用。 –

相關問題