Differences

This shows you the differences between two versions of the page.

Link to this comparison view

example_custom_plugin [2017/04/11 02:40] (current)
admin created
Line 1: Line 1:
 +  package demo.customPlugin;​ 
 +   
 +  import com.webmbt.Config;​ 
 +  import com.webmbt.MBTException;​ 
 +  import com.webmbt.ModelMgr;​ 
 +  import com.webmbt.TestOptimalServer;​ 
 +  import com.webmbt.plugin.MScriptInterface.TO_PLUGIN; ​  
 +  import com.webmbt.plugin.PluginAncestor;​ 
 +  import com.webmbt.scxml.ScxmlNode; ​  
 +  import com.webmbt.util.FileUtil;​ 
 +   
 +  /** 
 +   * Custom plugin to demonstrate how to perform Embedded System testing. 
 +   *  
 +   * This plugin provides a set of functions for MScript to call to interact with embedded systems. 
 +   *  
 +   * Any functions annotated with <​code>​@MSCRIPT_METHOD</​code>​ are automatically exposed to MScript when 
 +   * the plugin is enabled for the model. 
 +   *  
 +   * All MScript function'​s parameters must be declared as String but the function can return int, long,  
 +   * double, String, String[] and List<​String>​. 
 +   *  
 +   */ 
 +  @TO_PLUGIN 
 +  public final class EmbeddedSysPlugin extends PluginAncestor { 
 +   
 + /** 
 + * ModelMgr provides access to model attributes. 
 + */ 
 + private ModelMgr modelMgr; 
 +   
 + /** 
 + * following static native function declarations map to JNI / dll library functions. 
 + */ 
 + public static native double add(double x, double y); 
 + public static native double substract(double x, double y); 
 + public static native double multiply(double x, double y); 
 + public static native double divide(double x, double y); 
 +   
 + /** 
 + * load dll library once. 
 + */ 
 + static { 
 + // change demo_64 to demo 32 if you are running Windows 32bit OS.  DLL files must be  
 + // placed in /lib/ folder within TO_IDE project. 
 + String dllName = "​demo_64";​ 
 + String libFilePath = Config.getRootPath() + "/​lib/"​ + dllName + "​.dll";​ 
 + try{ 
 +        TestOptimalServer.logInfo("​Loading library from " + libFilePath);​ 
 +            System.load(libFilePath);​ 
 +        TestOptimalServer.logInfo("​Loaded library " + dllName); 
 +        } catch(Throwable e) {  
 +        TestOptimalServer.logError("​Error loading library " + libFilePath,​ e); 
 +        }  
 +
 +   
 + /** 
 + * Example MScript function. 
 + *  
 + * @param p1 
 + * @param p2 
 + * @return 
 + * @throws MBTException 
 + */ 
 + @MSCRIPT_METHOD 
 + public double add (String p1, String p2) throws MBTException { 
 + try { 
 +     double p1Value = Double.parseDouble(p1);​ 
 +     double p2Value = Double.parseDouble(p2);​ 
 + double result = add (p1Value, p2Value); 
 + return result; 
 + } catch (Throwable e) { 
 + this.scriptExecutor.error("​Embedded.add Errored: " + e.toString());​ 
 + throw new MBTException (e.getMessage() + ": " + e.toString());​ 
 +
 +
 +   
 + @MSCRIPT_METHOD 
 + public double substract (String p1, String p2) throws MBTException { 
 + try { 
 +     double p1Value = Double.parseDouble(p1);​ 
 +     double p2Value = Double.parseDouble(p2);​ 
 + double result = substract (p1Value, p2Value); 
 + return result; 
 + } catch (Throwable e) { 
 + this.scriptExecutor.error("​Embedded.substract Errored: " + e.getMessage());​ 
 + throw new MBTException (e.getMessage() + ": " + e.toString());​ 
 +
 +
 +   
 + @MSCRIPT_METHOD 
 + public double multiply (String p1, String p2) throws MBTException { 
 + try { 
 +     double p1Value = Double.parseDouble(p1);​ 
 +     double p2Value = Double.parseDouble(p2);​ 
 + double result = multiply (p1Value, p2Value); 
 + return result; 
 + } catch (Throwable e) { 
 + this.scriptExecutor.error("​Embedded.multiply Errored: " + e.getMessage());​ 
 + throw new MBTException (e.getMessage() + ": " + e.toString());​ 
 +
 +
 +   
 + @MSCRIPT_METHOD 
 + public double divide (String p1, String p2) throws MBTException { 
 + try { 
 +     double p1Value = Double.parseDouble(p1);​ 
 +     double p2Value = Double.parseDouble(p2);​ 
 + double result = divide (p1Value, p2Value); 
 + return result; 
 + } catch (Throwable e) { 
 + this.scriptExecutor.error("​Embedded.divide Errored: " + e.getMessage());​ 
 + throw new MBTException (e.getMessage() + ": " + e.toString());​ 
 +
 +
 +   
 + /** 
 + * Replace "​Embedded"​ with your unique plugin id.  Plugin ID is used in the namespace for MScript 
 + * functions provided by this plugin. 
 + *  
 + */ 
 + @Override 
 + public String getPluginID() { 
 + return "​Embedded";​ 
 +
 +   
 + /** 
 + * This function returns the plugin description/​name to be displayed on the Model Property. 
 + */ 
 + @Override 
 + public String getPluginDesc() { 
 + return "​Embedded:​ Embedded System Testing";​ 
 +
 +   
 + /** 
 + *  
 + * This function returns the type of plugin: DATA or AUT.  Set it to "​AUT"​ if the plugin 
 + * interacts with AUT, else return DATA.   
 + *  
 + * For example, DATAGEN oplugin only provides a set of functions to generate random test data,  
 + * so it is a DATA plugin while Selenium automates web applications,​ so it is an AUT plugin. 
 + *  
 + */ 
 + @Override 
 + public String getCatCode() { 
 + return "​AUT";​ 
 +
 +   
 + /** 
 + * This function is called when model execution stops. 
 + *  
 + */ 
 + @Override 
 + public void close() { 
 + try { 
 + // 
 +
 + catch (Exception e) { 
 + //ok 
 +
 + TestOptimalServer.logDebug(this.getPluginID() + ": closing all used resources."​);​ 
 +
 +   
 + /** 
 + * Initializes plugin. This function is called when the plugin object is instantialized and before 
 + * model execution starts. 
 + *  
 + */ 
 + @Override 
 + public synchronized void init(ScxmlNode scxmlNode_p,​ String[] pluginParams_p) throws Exception { 
 + this.setReady();​ 
 + this.modelMgr = this.getScriptExecutor().getModelMgr();​ 
 +  
 + // add your initialization code here. 
 +  
 + return; 
 +
 +   
 + /** 
 + * This function is called at the beginning of model execution before MBT_start trigger is executed. 
 + *  
 + */ 
 + @Override 
 + public void start() throws Exception { 
 +  
 + // add your code here 
 +  
 +
 +   
 + /** 
 + * This is an MScript function (accessible from MScript) as it is annotated with <​code>​@MScript_METHOD</​code>​. 
 + * It's required for all plugins to take a screenshot of AUT and return the file name of the screenshot file. 
 + *  
 + * The screenshot file should be stored in model'​s <​code>​snapscreen</​code>​ folder. 
 + *  
 + * If the plugin does not interact with AUT and can not take a screenshot of AUT, then it should return null. 
 + *  
 + */ 
 + @Override 
 + @MSCRIPT_METHOD 
 + public String snapScreen(String fileName_p) throws Exception { 
 + String modelFolderPath = Config.getModelFilePath(this.modelMgr.getModelName());​ 
 + String screenshotFile = FileUtil.concatFilePath(modelFolderPath,​ fileName_p + "​.png"​);​ 
 +  
 + // generate screenshot and save it to screeshotFile.  
 + screenshotFile = null; // return null if this plugin does not support snapScreen() 
 +  
 + return screenshotFile;​ 
 +
 +   
 +  }
Print/export
QR Code
QR Code example_custom_plugin (generated for current page)