Академический Документы
Профессиональный Документы
Культура Документы
Term
Meaning
Courier
Courier font is used for text to be entered on a command line or in a program, or for text output from a device.
Contents
DEVICEANYWHERE AUTOMATIONTM ..................................................................................... 1 JAVA QUICK START GUIDE ..................................................................................................... 1 1. INTRODUCTION ............................................................................................................ 4 1.1. Prerequisites ....................................................................................................... 4 1.2. Glossary of Terms ............................................................................................... 5 1.3. Recommended Workflow .................................................................................... 6 SETTING UP THE TUTORIAL WORKSPACE................................................................ 6 2.1. Creating a Project in DA Studio ........................................................................... 6 2.2. Devices Used in this Tutorial ............................................................................... 8 2.3. Get Familiar with the Test Cases and Actions in Java Tutorial Project ................ 9 GETTING STARTED WITH ECLIPSE .......................................................................... 14 3.1. Creating a Project in Eclipse ............................................................................. 14 3.2. Creating Your First Java Test Case................................................................... 18 3.3. Executing Java Test Case in DA Studio ............................................................ 23 3.4. Executing Java Test Cases in Eclipse ............................................................... 25 3.5. Introduction to the Device API ........................................................................... 26 3.6. JavaDocs .......................................................................................................... 28 JAVA SCRIPTING MORE ADVANCED ........................................................................ 29 4.1. Passing Parameters to Action Calls .................................................................. 29 4.2. Reading Values from File Data Driven Testing ............................................... 32 4.3. Calling a Test Cases from a Java Test Case..................................................... 34 4.4. Process Results and save to XML ..................................................................... 36 4.5. Saving results to database of API executed Test Cases ................................... 47 4.6. Scheduling Runs ............................................................................................... 48
2.
3.
4.
1. Introduction
This guide is intended to help you get started using the using the Java scripting functionality in the DeviceAnywhere Automation (DA) product. This document assumes that you have basic familiarity with Visual Scripting in the DA Automation solution. The same concepts and best practices of Visual Scripting are applicable when you begin building Test Cases and Actions in Java. The added benefits you get when working with Java are: Ability to interact with other systems (databases, build machines, billing systems etc.) required in your end to end automated test cases. Extend your existing test management system or automation framework to automate testing of mobile applications Leverage 3rd party Java libraries Data driving test cases by reading in values from a text file or database Work in an industry-standard programming language
After completing this Guide, you will be able to: Implement Java Test Cases that call other Test Cases, Visual Actions and Java Actions Pass parameters to Test Cases, Visual Actions and Java Actions Implement Java Test Cases using the device API Edit Java Test Cases in Eclipse Execute Java Test Cases in Eclipse Use the javadocs to learn about the objects and methods that are available in the API Implement Java Test Cases that read values from a text file and database
1.1. Prerequisites
This document assumes that you are familiar with Visual Scripting in DA Automation. It also assumes you understand your way around DA Studio. What you should already know how to do before starting this tutorial: Setup a Project Implement Actions, States and Test Cases Execute Test Cases and Actions View Test Results in the Result Portal Acquire and interact with devices
If you dont know how to do the tasks listed above, you can go through the following documents to get up to speed before staring this Tutorial: DeviceAnywhere Test Center QuickStart Guide
DeviceAnywhere Test Automation Getting Started DeviceAnywhere 5.0 Test Automation Best Practices
Proofscreen capture or video of device taken during testing to validate that an expected test result was achieved. Proofs may be captured manually or via an automated script. Java State Java code that identifies a known condition. Typically used to identify a condition NOT on a device (e.g. to enable a test on an application that interacts with a device).
Step 2: Browse the file system to the location of the Java_Tutorial.zip file.
Step 3: Name the project Java Tutorial, use DeviceAnywhere Version Control and make the project viewable and editable by all users in your account.
Step 4: Open the project and you should see the Java Tutorial project.
2.3. Get Familiar with the Test Cases and Actions in Java Tutorial Project
In this section, we examine the Visual Actions that will be used for the remainder of the Tutorial. Step 1: Play the Apple iPhone 3G Action Implementation of Reset to Home. Step 2: Highlight the Action Send SMS and select Properties.
Step 3: Edit the existing parameter ZipCode and you will see the following dialog box. This is the definition of the ZipCode parameter and also where the Default value is set.
Step 4: Double click the Type Zip command in the Send SMS Action Implementation for the iPhone. The Text to Send is populated with %ZipCode%. When the Action is called or executed, this is how the parameter passed in is used within the Action Implementation. The percentage % before and after are used to mark this as a variable and the DA Automation engine inserts the passed in value at runtime.
Step 5: Play the Apple iPhone 3G Action Implementation of Send SMS. This Action has been implemented to send a zip code to the weather.com short code 42278. When you play this Action, a dialog box will appear allowing you to enter a zip code. The default value is 94402 but feel free change it.
Step 6: Open the Verify Result Action Implementation for the iPhone. Look at the Verify Correct Forecast command. It is a WaitText command that takes a parameter %City%. Because it
is parameterized, you can pass in values from Visual Test Cases and Java Test Cases. We will use this Action later in this Tutorial when covering passing parameters to Actions from Java Test Cases and also when demonstrating data driven testing in Java.
Step 7: Play the Verify Result Action Implementation for the iPhone. If you changed the default Zip Code in Step 5 above, you should update the City parameter to make the Action run successfully.
Step 9: View the Send Message ExecuteAction command. Notice that this command takes a parameter for %ZipCode%. When an ExecuteAction command is called from the Visual Test Case, the zip code is passed in. Also in the ExecuteAction command, you can specify the proof capture type for the Action.
Step 10: Play the Test Case. The Test Case should run through to completion. Step 11: Optional - Repeat steps 1-8 for Blackberry 8530 device. You have now finished setting up the environment you will use for the remainder of the tutorial.
Note: If this is the first time you are launching Eclipse, please ensure you DO NOT set your Eclipse workspace folder to the workspace folder inside DeviceAnywhere Studio. This will cause problems when trying to use DeviceAnywhere Studio, as well as Eclipse.
Step 2: Leave the project name blank, and choose Create project from existing source and click Browse
Step 3: Browse to the project folder on your local disk. For this document, we will be using a project called Java Tutorial under the login user1@daprotrial.com. The path for this project is as follows:
C:\Program Files\Mobile Complete\DeviceAnywhereStudio\workspaces\yourDAUsername\my_java_tutorial
Step 4: Once you have selected the folder, click OK and then choose Next and then Finish. This will import the project directory structure. You may notice that the structure is not the same as within DeviceAnywhere Studio.
Step 5: To view the directory structure in a similar fashion as DeviceAnywhere Studio, change the package presentation to Hierarchical
You have now successfully configured Eclipse for editing your java test cases within Eclipse.
You should know see the Java Test Case editor in DA Studio. If you are familiar with Java and the methods available in the device API, you can use DA Studio to edit test scripts. For ease of use, we recommend using Eclipse or some other IDE to develop Java Test Cases.
Step 2: In Eclipse, refresh your project. After the refresh, you will see My First Java Test Case in the project.
Step 4: Scroll to the bottom of the test case and put the cursor in the execute method.
Step 5: Start typing the first few characters of Reset to Home and press the CTRL key and the space bar simultaneously. This will bring up a menu of available objects. Because the Eclipse project uses the same workspace as DA Studio, the Actions, States and Test Cases, are all available in your Eclipse editor. Select the Reset to Home object.
Step 6: After inserting the Reset_to_Home, add the .instance.execute(device). This is the syntax to call a Visual Action from a Java Test Case. Repeat the steps for Send SMS and Verify Forecast. Once you are done, your code Java Test Case should look as follows:
@Override protected ScriptReturn execute(Device device, IScriptContext context) throws ScriptFailureException, InterruptedException, DeviceExecutionException { Reset_to_Home.instance.execute(device); Send_SMS.instance.execute(device); Verify_Forecast.instance.execute(device);
return SUCCESS(); }
At the top of the Java Test Case, you will see the import statement for the Reset_to_Home action is added to the Java Test Case automatically.
Step 7: Save your Java Test Case in Eclipse Step 8: Go back to DA Studio and you will see the lines that you added to the Java Test Case from the Eclipse editor and also displayed within DA Studio.
You can use an IDE such as Eclipse to edit your Java Test Cases by creating a project in Eclipse that shares the same project files as your DA Studio workspace. Actions that were created in DA Studio can be called from a Java Test Case. Using this approach you can leverage the easy to use Visual Scripting to rapidly implement Actions in DA Studio and the power of Java to enhance your end to end test scenarios. By creating Java Test Cases that call Actions that are created in DA Studio, your test cases can now be run on any device that has completed Action Implementations
Test cases developed in Eclipse can be executed in DA Studio Test results for Java Test Cases are the same as Visual Test Cases
Step 3: You will need to define 2 variables before Eclipse will successfully execute test cases. These can be defined in the Arguments section of the run configuration.
The program argument is the MCD of the device. For this example, we are using MCD 4616. You can find the MCD of a device inside DeviceAnywhere Studio by right-clicking on that device and clicking Device Info:
The MC_HOME VM argument passes the DeviceAnywhere Studio directory to the JVM, which allows DeviceAnywhere classes to find required resources at runtime:
-DMC_HOME=C:\Program Files\Mobile Complete\DeviceAnywhereStudio
Once these have been set, you will be able to run your test cases directly from Eclipse by choosing run as, and selecting the run configuration you have created. Step 4: Press the Run button in the Eclipse toolbar
Step 1: In DA Studio, create a Java Test Case called Intro to Device API Step 2: Open the test case in Eclipse Step 3: Type device.se in the Java Test Case in Eclipse to get a drop down of all the methods available in the Device object. Select the first instance of sendKeys.
Step 4: After selecting the first instance of sendKeys, add the keys and the mode to the code. When complete it should look as follows.
Step 5: After sending [End][End][End] to the device, the device should be on the Home Screen. From your Java Test Case, you can verify the device is at the Home Screen by using the WaitFor Method to call into your State definition for the Home Screen.
Step 6:
3.6. JavaDocs
The latest Javadocs can be found at the following location: http://dademo1.deviceanywhere.com/javadocs
In Java TestCases action call can be passed parameters using Action Helper. How it is done is shown below: Step 1: First create a Visual Action with parameters. To add parameter to Action right click on action name and then choose parameter.
Step 2: Now do your visual scripting in the created Action. Call Parameters in appropriate SendKey Commands. Its written in SendKeys in between %Zip%.
Step 3: Create object of type ActionHelper. ActionHelper myHelper = new ActionHelper(); Step 4: Set parameters using setParameter() method. myHelper.setParameter("Number",42278 ); myHelper.setParameter("Zip", 94402); myHelper.setPrimaryDevice(device);
The String Name in myHelper.setParameter should always be same as of Visual Action Parameter. Step 5: Execute Send_SMS Action in Testcase using ActionHelper object as argument. Send_SMS.instance.execute(myHelper);
Step 1: Read data from file which has to be passed to Action call. String inputFileName="D:/ZipCodes.csv";//Grabs ZIP codes from file s = new Scanner(new BufferedReader(new FileReader(inputFileName))); s.useDelimiter(",");
Step 2: Execuete Reset_to_Home Action. Reset_to_Home.instance.execute(device); Step 3: Create object of type ActionHelper. ActionHelper myHelper = new ActionHelper(); Step 4: Set ActionHelper object parameters using setParameter() method.
The String Name in myHelper.setParameter() should always be same as of Visual Action Parameter (in this case Send_SMS).
Step 3: Execute Send_SMS Action in Testcase using ActionHelper object as argument. Send_SMS.instance.execute(myHelper); Step 4: Execute Verify_Result Action in Testcase using ActionHelper object as argument. Verify_Result.instance.execute(myHelper);
Step 1: First create Java Test case which will be called in another Java Test case. Here in this example a Java Testcase is created in which Text is extracted from received SMS. This extracted text is saved to a String and this String is put in to Context, which is returned as ScriptReturn. In this way the extracted text can be used in Action/Testcase in which this test case is called.
Step 2: Calling Testcase in a Testcase First create an instance of called Testcase. Then execute called Testcase using execute() method. Step 3: Getting saved Context (extracted Text) from called Testcase and save extracted text to a String. Step 4: String manipulation searching for City name in extracted String. If City name is found in extracted string SUCCESS() is returned otherwise FAIL() is returned.
import import import import import import import import import import
java.awt.image.BufferedImage; java.io.ByteArrayInputStream; java.io.File; java.io.FileOutputStream; java.io.IOException; java.io.PrintWriter; java.io.StringWriter; java.net.InetAddress; java.net.UnknownHostException; java.util.Collection;
import java.util.LinkedList; import java.util.List; import javax.imageio.ImageIO; import import import import import import import import import import import import import import import import import import import import import org.apache.xerces.dom.DOMImplementationImpl; org.apache.xerces.dom.DocumentImpl; org.w3c.dom.Document; org.w3c.dom.Element; org.w3c.dom.Node; org.w3c.dom.ls.DOMImplementationLS; org.w3c.dom.ls.LSOutput; org.w3c.dom.ls.LSSerializer; com.mc.api.common.GlobalContext; com.mc.api.device.Device; com.mc.api.device.exception.DeviceExecutionException; com.mc.api.device.exception.NoSuchDeviceException; com.mc.api.script.ScriptReturn; com.mc.api.script.ScriptReturn.ScriptReturnCode; com.mc.api.script.exception.ScriptFailureException; com.mc.api.script.result.ResultManager; com.mc.api.script.result.ScriptResult; com.mc.api.script.result.ScriptResultProof; com.mc.api.script.result.ScriptResultStep; com.mc.api.script.result.ScriptResultProof.ProofType; com.mc.api.testcase.TestCase;
public class SampleCode { private static final String SCRIPT_RUN_KEY = "scriptRun"; private private private private private private private private private private private private private private private private static static static static static static static static static static static static static static static static final final final final final final final final final final final final final final final final String String String String String String String String String String String String String String String String SCRIPT_KEY = "script"; SCRIPT_NAME_KEY = "name"; SCRIPT_PATH_KEY = "path"; SCRIPT_DESCRIPTION_KEY = "description"; SCRIPT_STATUS_KEY = "status"; SCRIPT_LOCATION_KEY = "location"; SCRIPT_START_TIME_KEY = "start"; SCRIPT_STOP_TIME_KEY = "stop"; STEP_KEY = "step"; STEP_NAME_KEY = "name"; STEP_PATH_KEY = "path"; STEP_DESCRIPTION_KEY = "description"; STEP_STATUS_KEY = "status"; STEP_LINE_NUMBER_KEY = "lineNumber"; STEP_START_TIME_KEY = "start"; STEP_STOP_TIME_KEY = "stop";
{ executeTestCase(); } public static void executeTestCase() throws NoSuchDeviceException, ScriptFailureException { // get the test case and device // TODO: replace these references TestCase testCase = com.mobilecomplete.cl.test_cases.a.a.instance; Device device = Device.get(121); // lock the device if (device != null) device.lock(); try { // execute the test case ScriptResult result = testCase.execute(device); // print the result System.out.println(testCase.getClass().getName() + " Result is: " + result); // find failure step (if any) ScriptResultStep failedStep = findFailedStep(result); if (failedStep != null) { // the test case failed System.err.println("Test case " + testCase + " failed on step " + failedStep); // upload result to database ResultManager manager = new ResultManager(GlobalContext.getGlobalContext()); manager.saveToDatabase(result); // save the last images that were captured from the device before the failure saveFailedImages(device, result, failedStep); // generate result XML saveResultXML(result); } } finally { // unlock the device if (device != null) device.unlock(); } }
private static ScriptResultStep findFailedStep(ScriptResultStep result) { ScriptResultStep failedStep = null; // loop through all steps and find the last failed step for (ScriptResultStep step : result.getChildSteps()) { // see if we can find a nested failure ScriptResultStep nestedStep = findFailedStep(step); // if there was a nested failure, save it if (nestedStep != null) failedStep = nestedStep; // if no nested failure was found but the step failed, save this step else if (step.getScriptReturnCode() == ScriptReturnCode.FAIL) failedStep = step; } // if we found a nested failure, return it if (failedStep != null) return failedStep; // no nested failure found else if (result.getScriptReturnCode() == ScriptReturnCode.FAIL) return result; else return null; } private static void saveFailedImages(Device device, ScriptResult result, ScriptResultStep failedStep) { // print out information about the failed step System.out.println(result.getScriptPath() + " failed at " + failedStep.getStepPath() + " in script " + failedStep.getScriptPath()); // find the image proofs before and after the failure (if they exist) ScriptResultProof beforeProof = findLastImageBeforeStep(result, failedStep); ScriptResultProof afterProof = findNextImageAfterStep(result, failedStep); if (beforeProof != null && beforeProof.getData().length > 0) { // format the image String format = beforeProof.getFormat(); BufferedImage image = formatImage(beforeProof.getData()[0]); String file = "before." + format; System.out.println("found image before failed step " + failedStep.getStepPath() + "; saving to file " + file); // save it to disk try { ImageIO.write(image, format, new File(file)); } catch (Exception e) { e.printStackTrace(); }
} else System.out.println("unable to find image before failed step " + failedStep.getStepPath()); if (afterProof != null && afterProof.getData().length > 0) { // format the image String format = afterProof.getFormat(); BufferedImage image = formatImage(afterProof.getData()[afterProof.getData().length - 1]); String file = "after." + format; System.out.println("found image after failed step " + failedStep.getStepPath() + "; saving to file " + file); // save it to disk try { ImageIO.write(image, format, new File(file)); } catch (Exception e) { e.printStackTrace(); } } else { try { // capture the current image from the device String format = "jpeg"; BufferedImage image = device.getCurrentImage(); String file = "after." + format; System.out.println("captured latest image from device " + device.getID() + "; saving to file " + file); // save it to disk ImageIO.write(image, format, new File(file)); } catch (DeviceExecutionException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } private static ScriptResultProof findLastImageBeforeStep(ScriptResult result, ScriptResultStep currentStep) { ScriptResultProof lastProof = null; // first see if the current step contains before and after images. if so, we're done Collection<ScriptResultProof> proofs = currentStep.getProofs(); for (ScriptResultProof proof : proofs) { // if we found an image sequence capture, return the first one if (proof.getProofType() == ProofType.IMAGE_SEQUENCE && proof.getData().length > 1) return proof;
// if we found multiple image captures, return the first one else if (proof.getProofType() == ProofType.IMAGE && lastProof != null && lastProof.getProofType() == ProofType.IMAGE) return lastProof; // otherwise keep track of this image proof else if (proof.getProofType() == ProofType.IMAGE) lastProof = proof; } // if the current step does not have image captures, find the last step before it that does ScriptResultStep lastStepWithImageProof = findLastStepWithImageProof(result, currentStep, null); lastProof = null; if (lastStepWithImageProof != null) { // if we found a step with image proofs before the failure, get the last proof for (ScriptResultProof proof : lastStepWithImageProof.getProofs()) { if (proof.getProofType() == ProofType.IMAGE || proof.getProofType() == ProofType.IMAGE_SEQUENCE) lastProof = proof; } } // and return return lastProof; } private static ScriptResultStep findLastStepWithImageProof(ScriptResultStep currentStep, ScriptResultStep failedStep, ScriptResultStep lastStep) { if (currentStep == failedStep) return lastStep; // go through every step and get the captured images, until we reach the failed step for (ScriptResultStep step : currentStep.getChildSteps()) { // if this is a step that contains the failed step... if (contains(step, failedStep)) { // recursively find the last step and return lastStep = findLastStepWithImageProof(step, failedStep, lastStep); break; } else { // otherwise, just find the last step with image proofs Collection<ScriptResultProof> proofs = step.getProofs(); for (ScriptResultProof proof : proofs)
{ // if this step has image proofs, keep track of it if (proof.getProofType() == ProofType.IMAGE || proof.getProofType() == ProofType.IMAGE_SEQUENCE) { lastStep = step; break; } } lastStep = findLastStepWithImageProof(step, failedStep, lastStep); } } return lastStep; } private static ScriptResultProof findNextImageAfterStep(ScriptResult result, ScriptResultStep currentStep) { ScriptResultProof lastProof = null; // first see if the current step contains before and after images. if so, we're done Collection<ScriptResultProof> proofs = currentStep.getProofs(); for (ScriptResultProof proof : proofs) { // if we found an image capture, return the last one if (proof.getProofType() == ProofType.IMAGE || proof.getProofType() == ProofType.IMAGE_SEQUENCE) lastProof = proof; } if (lastProof == null) { // if the current step does not have image captures, find the next step after it that does ScriptResultStep nextStepWithImageProof = findNextStepWithImageProof(result, currentStep); if (nextStepWithImageProof != null) { // if we found a step with image proofs after the failure, get the first proof for (ScriptResultProof proof : nextStepWithImageProof.getProofs()) { // if we found an image capture, return the first one if (proof.getProofType() == ProofType.IMAGE || proof.getProofType() == ProofType.IMAGE_SEQUENCE) { lastProof = proof; break; }
} } } return lastProof; } private static ScriptResultStep findNextStepWithImageProof(ScriptResultStep currentStep, ScriptResultStep failedStep) { ScriptResultStep nextStep = null; boolean passedStep = false; for (ScriptResultStep step : currentStep.getChildSteps()) { // if the current step contains the failed step, find a step after the failed step with an image capture if (contains(step, failedStep)) { nextStep = findNextStepWithImageProof(step, failedStep); passedStep = true; } // ignore any steps before the failed step else if (passedStep) { nextStep = findNextStepWithImageProof(step, failedStep); // if we did not find a nested step with image proofs, see if this step has any if (nextStep == null) { Collection<ScriptResultProof> proofs = step.getProofs(); for (ScriptResultProof proof : proofs) { // if this step has image proofs, return it if (proof.getProofType() == ProofType.IMAGE || proof.getProofType() == ProofType.IMAGE_SEQUENCE) { nextStep = step; break; } } } } if (nextStep != null) break; } return nextStep;
} private static boolean contains(ScriptResultStep step, ScriptResultStep childStep) { if (step == childStep) return true; for (ScriptResultStep nestedStep : step.getChildSteps()) { if (contains(nestedStep, childStep)) return true; } return false; } private static BufferedImage formatImage(byte[] b) { try { return ImageIO.read(new ByteArrayInputStream(b)); } catch (IOException e) { e.printStackTrace(); return null; } } private static void saveResultXML(ScriptResult result) { // create XML elements Document document = new DocumentImpl(); Element root = document.createElement(SCRIPT_RUN_KEY); Element script = createScriptElement(result, document); // build XML structure root.appendChild(script); document.appendChild(root); // write document to file File file = new File("result.xml"); try { FileOutputStream stream = new FileOutputStream(file); DOMImplementationLS ls = new DOMImplementationImpl(); LSOutput output = ls.createLSOutput(); output.setByteStream(stream);
// TODO: format result LSSerializer serializer = ls.createLSSerializer(); serializer.getDomConfig().setParameter("format-pretty-print", true); serializer.write(script, output); stream.flush(); stream.close(); System.out.println("saved result to " + file.getAbsolutePath() ); } catch (IOException e) { e.printStackTrace(); } } private static Element createScriptElement(ScriptResult result, Document document) { // create XML elements Element scriptElement = document.createElement(SCRIPT_KEY); Element descriptionElement = document.createElement(SCRIPT_DESCRIPTION_KEY); Node description = document.createTextNode(SCRIPT_DESCRIPTION_KEY); // build XML structure descriptionElement.appendChild(description); scriptElement.appendChild(descriptionElement); // get hostname where test case was run String hostname = "Unknown Host"; try { hostname = InetAddress.getLocalHost().getHostName(); } catch (UnknownHostException e) { e.printStackTrace(); } // set script attributes scriptElement.setAttribute(SCRIPT_PATH_KEY, "" + result.getScriptPath()); scriptElement.setAttribute(SCRIPT_START_TIME_KEY, "" + result.getStartTimestamp()); scriptElement.setAttribute(SCRIPT_STOP_TIME_KEY, "" + result.getEndTimestamp()); scriptElement.setAttribute(SCRIPT_LOCATION_KEY, "" + hostname ); scriptElement.setAttribute(SCRIPT_NAME_KEY, result.getScriptPath()); scriptElement.setAttribute(SCRIPT_STATUS_KEY, result.getScriptReturnCode().toString()); // get description ScriptReturn scriptReturn = result.getReturn(); String strDescription = scriptReturn.getMessage(); if (!scriptReturn.isSuccess() && scriptReturn.getScriptException() != null) { // if the script threw an exception, save the stack trace in the description Exception e = scriptReturn.getScriptException();
StringWriter stringWriter = new StringWriter(); e.printStackTrace(new PrintWriter(stringWriter)); strDescription += "\n" + e.getMessage() + ":\n" + stringWriter.toString(); } // set description description.setNodeValue(strDescription); // create step elements for (Element step : createStepElements(result, document)) { scriptElement.appendChild(step); } return scriptElement; } private static List<Element> createStepElements(ScriptResultStep parentStep, Document document) { List<Element> stepElements = new LinkedList<Element>(); for (ScriptResultStep step : parentStep.getChildSteps()) { // create XML elements Element stepElement = document.createElement(STEP_KEY); Element descriptionElement = document.createElement(STEP_DESCRIPTION_KEY); Node description = document.createTextNode(STEP_DESCRIPTION_KEY); // build XML structure descriptionElement.appendChild(description); stepElement.appendChild(descriptionElement); // set step attributes stepElement.setAttribute(STEP_NAME_KEY, step.getStepPath()); stepElement.setAttribute(STEP_STATUS_KEY, "" + step.getScriptReturnCode()); stepElement.setAttribute(STEP_START_TIME_KEY, "" + step.getStartTimestamp()); stepElement.setAttribute(STEP_STOP_TIME_KEY, "" + step.getEndTimestamp()); stepElement.setAttribute(STEP_PATH_KEY, "" + step.getScriptPath()); stepElement.setAttribute(STEP_LINE_NUMBER_KEY, "" + step.getPosition()); // get description description.setNodeValue(step.getDescription()); for (Element childElement : createStepElements(step, document)) { stepElement.appendChild(childElement); }
package com.mobilecomplete.example.test_cases; import import import import import import import import import com.mc.api.device.Device; com.mc.api.device.exception.DeviceExecutionException; com.mc.api.script.exception.ScriptFailureException; com.mc.api.script.IScriptContext; com.mc.api.script.result.ScriptResult; com.mc.api.script.ScriptReturn; com.mc.api.testcase.TestCase; com.mc.api.common.GlobalContext; com.mc.api.script.result.ResultManager;
public class CallTestCaseWithResults extends TestCase { /*** GENERATED CODE -- DO NOT MODIFY (ANY CHANGES WILL BE OVERWRITTEN) ***/ public static final CallTestCaseWithResults instance = new CallTestCaseWithResults(); private CallTestCaseWithResults() { super(); } /** * @param args * @throws ScriptFailureException */ public static void main(String[] args) throws ScriptFailureException { // get the devices Device primaryDevice = getPrimaryDeviceFromArgs(args);
// lock all devices primaryDevice.lock(); TestCase testCase = CallTestCaseWithResults.instance; try { ScriptResult result = testCase.execute(primaryDevice); System.out.println(testCase.getClass().getName() + " Result is: " + result); ResultManager manager = new ResultManager(GlobalContext.getGlobalContext().getDataAccess()); manager.saveToDatabase(result); } finally { // unlock all devices primaryDevice.unlock(); } } /*** END GENERATED CODE ***/
@Override protected ScriptReturn execute(Device device, IScriptContext context) throws ScriptFailureException, InterruptedException, DeviceExecutionException { TestCase testCase = com.mobilecomplete.example.test_cases.tc1.tc1.instance; testCase.execute(device); return SUCCESS(); } }
// create a runnable to execute the script Runnable script = new Runnable() { @Override public void run() {
// TODO call script here } }; // create an executor service to schedule the script ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); // schedule the script to run every 10 minutes scheduler.schedule(script, 10, TimeUnit.MINUTES);