Friday, April 22, 2011

Open Script : Create New Java Class and Access the Methods in it

Go to Developer Perspective in Open Script.
Go to Window > Show View >Package Explorer
under "default package". You will find the Open Script test script name as "script.java"
Right Click and Create a New Class with a name "sayHello"

The Content in the class file should look like this
class sayHello {
   
    public void sayHi(Object str)
    {
        System.out.println("Saying Hi to : "+str);
    }

}


Now go to the open script file and enter the following code in the run() method

public void run() throws Exception {

         // Creating and Using external Java Class
       
        sayHello s=new sayHello();
        // Access the say Hi Method in it
        s.sayHi("Ajith");
       
    }
Output:
Saying Hi to : Ajith

5 comments:

  1. Hi Ajith,
    At last some one told me how to write a custom class in OATS. Developers perspective is what I am looking for. This is the first site I came across on OATS... Keep posting more.

    -ksivapur

    ReplyDelete
  2. Hi Ajith,

    Currently the messages goes only into the console window.

    what would I have to do to put the output also into the summary column of my OATS results table?

    Adding a result row is easy, but which command would I need here?

    beginStep("[1] Custom Step with Result Output", 0);
    s.sayHi("Ajith"); <<- what has to be here?
    endStep();

    Thanks, Volker

    ReplyDelete
    Replies
    1. Hi Volker,

      Following are the various ways in which you can achieve the requirement stated.

      Open script provides a built in functions/API’s to add text into the “Summary” column of the results file.

      Example1: If you want to add comments to results use ( by default the status is “Passed”):
      info("information");

      Example2: If you want to add comments to results use:
      getStepResult().addComment("This is comment");

      Example3: If you want to comments with status as warning to results use:
      getStepResult().addWarning("This is Warning");

      Example4: If you want to add comments with status as error to results use:

      getStepResult().setError("This is error");

      Please let us know if you need more information.

      Thanks
      -POPS

      Delete
  3. This post is helpful... But i want to ask a question which does not matches these questions but it is important to us.
    We are trying to send email from oats after test schedule ... so far I am able to send the email the report from OTM but the issue is I want to send the report only when the test fails ...

    Or otherwise I would want to send the test result only like passed or failed on top of email so user can ignore it incase it is passed and look into it when it failes.
    Appreciate your response
    I hope you are working with oats ptherwise can you forward this question to the current expert of openscript

    ReplyDelete
  4. Hi,
    is it possible to add the self-written java-classes to a package? Whenever I create a new package and move my classes there I get a NoClassDefFoundError when executing it.

    ReplyDelete