Forum Discussion

MOTOROLA_Suppor_1's avatar
14 years ago

How to access 'context' object from mockRunListener events

I try to access context object inside the mock events : such as onMockRunnerStart, onMockResult but got error. What is the proper way to access context object inside mock events callback script ?

thanks
sheena



For example for onMockRunnerStart event, I want to perform:
.....
context.setProperty("myLogFile", filehandler)

for onMockResult event, I want to perform:
context.myLogFile

thanks
sheena

8 Replies

  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hello,

    Try this:

    runner.mockContext.setProperty("myLogFile", "hello world")


    Regards,
    Dain
    eviware.com
  • Dain,

    thanks for your response but I am not sure I have complete solution as you suggested. How do I get this property from onMockResult() Do I have access to runner object ?
    In onMockResult, I have result object but not sure how to get runner object



    sheena
  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hello Sheena,

    Sorry, I missed that part of your original question. You can access the context from the result object by calling:


    def context = result.mockRequest.context

    log.info context.myLogFile


    Regards,
    Dain
    eviware.com
  • Hi, Dain,
    I copied the my script here, not sure what is wrong but I could not get the mock request logged into my file. I could not even find file in the expected directory.

    thanks
    sheena

    ===MockRunListener.moMockRunnerStart===========
    def logfile = "D:/XML-Testing/WebNBI/SoapUIPro/TestResultR7.3/MyMockLogFile.log";
    log.info(logfile);
    def File myMockFile = new File(logfile);
    myMockFile.append("this is a test");

    log.info(myMockFile);
    if ( myMockFile.exists()) {
    myMockFile.delete()
    myMockFile = new File(logfile);

    }
    runner.mockContext.setProperty("MyMockLogFile", myMockFile);


    ===MockRunListener.onMockResult
    if ( result ) {

    def mockReq = result.getMockRequest()
    def context = result.mockRequest.context;


    if ( context.MyMockLogFile) {
    def filehandle = context.myMockLogFile;
    filehandle.append ( mockReq.getRequestContent())
    log.info("Has Mock Log file");
    }
    log.info(mockReq.getRequestContent());

    }
  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi!

    it seems you have a typo in your onResult script;

    def filehandle = context.myMockLogFile;

    should be

    def filehandle = context.MyMockLogFile;

    (capital M in the property name)

    Does that help?

    regards!

    /Ole
    eviware.com
  • Ole, Thank you so much. It does work now. Somehow, I just did not catch the typo and script language is not strong at type/syntax chechking. thanks again.

    sheena