Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
example_custom_plugin [2020/04/26 03:33] 127.0.0.1 external edit |
— (current) | ||
|---|---|---|---|
| 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 < | + | |
| - | * the plugin is enabled for the model. | + | |
| - | * | + | |
| - | * All MScript function' | + | |
| - | * double, String, String[] and List< | + | |
| - | * | + | |
| - | */ | + | |
| - | @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 = " | + | |
| - | String libFilePath = Config.getRootPath() + "/ | + | |
| - | try{ | + | |
| - | TestOptimalServer.logInfo(" | + | |
| - | System.load(libFilePath); | + | |
| - | TestOptimalServer.logInfo(" | + | |
| - | } catch(Throwable e) { | + | |
| - | TestOptimalServer.logError(" | + | |
| - | } | + | |
| - | } | + | |
| - | + | ||
| - | /** | + | |
| - | * 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(" | + | |
| - | 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(" | + | |
| - | 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(" | + | |
| - | 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(" | + | |
| - | throw new MBTException (e.getMessage() + ": " + e.toString()); | + | |
| - | } | + | |
| - | } | + | |
| - | + | ||
| - | /** | + | |
| - | * Replace " | + | |
| - | * functions provided by this plugin. | + | |
| - | * | + | |
| - | */ | + | |
| - | @Override | + | |
| - | public String getPluginID() { | + | |
| - | return " | + | |
| - | } | + | |
| - | + | ||
| - | /** | + | |
| - | * This function returns the plugin description/ | + | |
| - | */ | + | |
| - | @Override | + | |
| - | public String getPluginDesc() { | + | |
| - | return " | + | |
| - | } | + | |
| - | + | ||
| - | /** | + | |
| - | * | + | |
| - | * This function returns the type of plugin: DATA or AUT. Set it to " | + | |
| - | * 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, | + | |
| - | * | + | |
| - | */ | + | |
| - | @Override | + | |
| - | public String getCatCode() { | + | |
| - | return " | + | |
| - | } | + | |
| - | + | ||
| - | /** | + | |
| - | * 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, | + | |
| - | 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 < | + | |
| - | * 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' | + | |
| - | * | + | |
| - | * 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, | + | |
| - | + | ||
| - | // generate screenshot and save it to screeshotFile. | + | |
| - | screenshotFile = null; // return null if this plugin does not support snapScreen() | + | |
| - | + | ||
| - | return screenshotFile; | + | |
| - | } | + | |
| - | + | ||
| - | } | + | |