from net.grinder.script.Grinder import grinder
from net.grinder.plugin.http import HTTPPluginControl

def write_to_file(text, testId, error = False):
    '''Write the text into a file. The file name is composed using the testId (typically the test number), the grinder run number and the error flag'''
    filename = grinder.getFilenameFactory().createFilename(
        str(testId) + "-page" + ("-error" if error else "") , "-%d.html" % grinder.runNumber)

    file = open(filename, "w")
    print >> file, text

    file.close()

def write_response_to_file(testId):
    '''Write the result of the last request into a file. testId is typically the test number'''
    write_to_file(HTTPPluginControl.getHTTPUtilities().getLastResponse().getText(), testId)
