Getting Current Test Name

Applies to TestComplete 14.30, last modified on November 21, 2019

During the test run, you may need to get the name of the test that is currently running. This can be useful when exporting test results to an external file, or sending them to an issue-tracking or test management system, or using custom procedures or third-party tools to parse the test results.

Get Test Case Name

From Scripts

You can get the name of the current test case by using the aqTestCase.CurrentTestCase.Name property. The example below demonstrates this (to execute it, run the Main routine):

JavaScript, JScript

// To execute the example, run this routine
function Main()
{
  var categoryName = "Furniture";
  var b = OpenWebStore();
  SelectCategory(b, categoryName);
  CloseBrowser();
}

// Export test case results to an MHT file
function ExportTestResults(name)
{
  if (name != null)
  {
    var logName = Project.Path + "Log\\" + name + ".mht";
    Log.SaveResultsAs(logName, lsMHT);
  }
}

function OpenWebStore()
{
  var url = "http://services.smartbear.com/samples/TestComplete14/smartstore/";
  Browsers.Item(btIExplorer).Run(url);
  return Sys.WaitBrowser("iexplore", 3000);
}

function CloseBrowser()
{
  Sys.WaitBrowser("iexplore").Close();
}

function SelectCategory(browser, category)
{
  aqTestCase.Begin("Select category")
  try{
    if (browser != null)
    {
      var page = browser.WaitPage("*", 3000);
      if (page.Exists)
      {
        var c = page.FindChild(new Array("ObjectType", "attributes.title"), new Array("Link", category), 100);
        if (c.Exists)
        {
          c.Click();
        }
        else
        {
          Log.Warning("Cannot find the " + category + " category.");
        }
      }
    }
  }
  catch(e)
  {
    …
  }
  finally{
    ExportTestResults(aqTestCase.CurrentTestCase.Name);
    aqTestCase.End();
  }

}

Python

# To execute the example, run this routine
def Main():
  categoryName = "Furniture"
  b = OpenWebStore()
  SelectCategory(b, categoryName)
  CloseBrowser()


# Export test case results to an MHT file
def ExportTestResults(name):
  if (name != ""): 
    logName = Project.Path + "Log\\" + name + ".mht"
    Log.SaveResultsAs(logName, lsMHT);


def OpenWebStore():
  url = "http://services.smartbear.com/samples/TestComplete14/smartstore/"
  Browsers.Item[btIExplorer].Run(url)
  return Sys.WaitBrowser("iexplore", 3000)


def CloseBrowser():
  Sys.WaitBrowser("iexplore").Close();

def SelectCategory(browser, category):
  aqTestCase.Begin("Select category")
  try:
    if (browser != None):
      page = browser.WaitPage("*", 3000)
      if (page.Exists):
        c = page.FindChild(["ObjectType", "attributes.title"], ["Link", category], 100)
        if (c.Exists):
          c.Click()
        else:
          Log.Warning("Cannot find the " + category + " category.")
  except Exception as e:
    Log.Warning("Error")
    #…
  finally:
    ExportTestResults(aqTestCase.CurrentTestCase.Name)
    aqTestCase.End()

VBScript

' To execute the example, run this routine
Sub Main
  categoryName = "Furniture"
  Set b = OpenWebStore
  Call SelectCategory(b, categoryName)
  CloseBrowser
End Sub

' Export test case results to an MHT file
Sub ExportTestResults(name)
  If name <> "" Then
    logName = Project.Path & "Log\" & name & ".mht"
    Call Log.SaveResultsAs(logName, lsMHT)
  End if
End Sub

Function OpenWebStore()
  url = "http://services.smartbear.com/samples/TestComplete14/smartstore/"
  Browsers.Item(btIExplorer).Run(url)
  Set OpenWebStore = Sys.WaitBrowser("iexplore", 3000)
End Function

Sub CloseBrowser()
  Sys.WaitBrowser("iexplore").Close
End Sub

Sub SelectCategory(browser, category)
  aqTestCase.Begin("Select category")
  On Error Resume Next
    If Not browser Is Nothing Then
      Set page = browser.WaitPage("*", 3000)
      If page.Exists Then
        Set c = page.FindChild(Array("ObjectType", "attributes.title"), Array("Link", category), 100)
        If c.Exists Then
          c.Click
        Else
          Log.Warning("Cannot find the " + category + " category.")
        End If
      End If
    End If
  If Err.Number <> 0 Then
    …
  Else
    ExportTestResults(aqTestCase.CurrentTestCase.Name)
    aqTestCase.End
  End If

End Sub

DelphiScript

// Export test case results to an MHT file
procedure ExportTestResults(name);
var logName;
begin
  if name <> '' then
  begin
    logName := Project.Path + 'Log\' + name + '.mht';
    Log.SaveResultsAs(logName, lsMHT);
  end;
end;

function OpenWebStore();
var url;
begin
  url := 'http://services.smartbear.com/samples/TestComplete14/smartstore/';
  Browsers.Item(btIExplorer).Run(url);
  result := Sys.WaitBrowser('iexplore', 3000);
end;

procedure CloseBrowser();
begin
  Sys.WaitBrowser('iexplore').Close();
end;

procedure SelectCategory(browser, category);
var page, c;
begin
  aqTestCase.Begin('Select category');
  try
  begin
    if browser <> nil then
    begin
      page := browser.WaitPage('*', 3000);
      if page.Exists then
      begin
        c := page.FindChild(['ObjectType', 'attributes.title'], ['Link', category], 100);
        if c.Exists then
          c.Click()
        else
          Log.Warning('Cannot find the ' + category + ' category.');
      end;
    end;
  end;
  finally
  begin
    ExportTestResults(aqTestCase.CurrentTestCase.Name);
    aqTestCase.End();
  end;
  end;
end;

// To execute the example, run this routine
procedure Main();
var categoryName, b;
begin
  categoryName := 'Furniture';
  b := OpenWebStore();
  SelectCategory(b, categoryName);
  CloseBrowser();
end;

C++Script, C#Script

// To execute the example, run this routine
function Main()
{
  var categoryName = "Furniture";
  var b = OpenWebStore();
  SelectCategory(b, categoryName);
  CloseBrowser();
}

// Export test case results to an MHT file
function ExportTestResults(name)
{
  if (name != null)
  {
    var logName = Project["Path"] + "Log\\" + name + ".mht";
    Log["SaveResultsAs"](logName, lsMHT);
  }
}

function OpenWebStore()
{
  var url = "http://services.smartbear.com/samples/TestComplete14/smartstore/";
  Browsers["Item"](btIExplorer)["Run"](url);
  return Sys["WaitBrowser"]("iexplore", 3000);
}

function CloseBrowser()
{
  Sys["WaitBrowser"]("iexplore")["Close"]();
}

function SelectCategory(browser, category)
{
  aqTestCase["Begin"]("Select category")
  try{
    if (browser != null)
    {
      var page = browser["WaitPage"]("*", 3000);
      if (page["Exists"])
      {
        var c = page["FindChild"](new Array("ObjectType", "attributes.title"), new Array("Link", category), 100);
        if (c["Exists"])
        {
          c["Click"]();
        }
        else
        {
          Log["Warning"]("Cannot find the " + category + " category.");
        }
      }
    }
  }
  catch(e)
  {
    …
  }
  finally{
    ExportTestResults(aqTestCase["CurrentTestCase"]["Name"]);
    aqTestCase["End"]();
  }

}

The property returns the test case name regardless of how your test case has been run. It can be:

  • A test item marked as a test case on the Test Items page of your project (be default, all test items are treated as test cases). The test case name will correspond to the value in the Name column of the test item on the Test Items page.

    To get the name of such test cases, your tests must run as part of the project’s test item sequence. You can run them either from the project’s Test Items page or by running the entire project (for example, from the command line). See To Run a Test Item or Group.
  • A BDD scenario. The test case name corresponds to the scenario description.

  • An arbitrary code fragment marked as a test case with the aqTestCase.Begin and aqTestCase.End methods. The test case name is specified by the aqTestCase.Begin method.

  • A test selected for execution by its tag. The test case name will correspond to the test name. For keyword tests, it is the name you can see in the Project Explorer under the Keyword Tests node. For script tests, it is the routine name or a custom name specified by using tags.

If no test case is running (this can happen if your tests are not marked as test cases on the Test Items page, or you run individual script routines or keyword tests from the TestComplete IDE), the aqTestCase.CurrentTestCase property will return an empty value.

From Keyword Tests

Get the property value described above by using the Call Object Method, Run Code Snippet, or Run Script Routine operation.

Get Arbitrary Test Item Name

To be able to get test item names, you must run your tests as part of the test item sequence specified on the Test Items page of your project. You can do this either from the Test Items page or by running the entire project.

From Scripts

On the Test Items page of your TestComplete project, you specify a sequence of tests to run when you execute the project. To get the name of the test item that is currently running, use the Project.TestItems.Current.Name property. It is the name specified in the Name column of the test item on the Test Items page.

The example below shows how to get the name of the current test item. In order for the example to run successfully:

  1. Add the Main routine to the Test Items page of a TestComplete project.

  2. Add the Events collection to the TestComplete project, add an event control to it, and then add a handler for the OnStopTest event. For the handler, use the EventControl1_OnStopTest routine specified below. To learn more about creating event handlers, see Creating Event Handlers for TestComplete Events.

  3. Run the added test item that will execute the Main routine.

JavaScript, JScript

// Add this routine to the Test Items page and then run it
function Main()
{
  var categoryName = "Furniture";
  var url = "http://services.smartbear.com/samples/TestComplete14/smartstore/";
  Browsers.Item(btIExplorer).Run(url);
  var browser = Sys.WaitBrowser("iexplore", 3000);
  if (browser.Exists)
  {
    var page = browser.WaitPage("*", 3000);
    if (page.Exists)
    {
      var c = page.FindChild(new Array("ObjectType", "attributes.title"), new Array("Link", categoryName), 100);
      if (c.Exists)
      {
        c.Click();
      }
    }
    browser.Close();
  }
}

function EventControl1_OnStopTest(Sender)
{
  if (Project.TestItems.Current != null)
  {
    var logName = Project.Path + "Log\\" + Project.TestItems.Current.Name + ".mht";
    Log.SaveResultsAs(logName, lsMHT);
  }
}

Python

# Add this routine to the Test Items page and then run it
def Main():
  categoryName = "Furniture"
  url = "http://services.smartbear.com/samples/TestComplete14/smartstore/"
  Browsers.Item[btIExplorer].Run(url)
  browser = Sys.WaitBrowser("iexplore", 3000)
  if browser.Exists:
    page = browser.WaitPage("*", 3000)
    if page.Exists:
      c = page.FindChild(["ObjectType", "attributes.title"], ["Link", categoryName], 100)
      if c.Exists:
        c.Click()
    browser.Close()

def EventControl1_OnStopTest(Sender):
  if Project.TestItems.Current != None:
    logName = Project.Path + "Log\\" + Project.TestItems.Current.Name + ".mht"
    Log.SaveResultsAs(logName, lsMHT)

VBScript

' Add this routine to the Test Items page and then run it
Sub Main
  categoryName = "Furniture"
  url = "http://services.smartbear.com/samples/TestComplete14/smartstore/"
  Browsers.Item(btIExplorer).Run(url)
  Set browser = Sys.WaitBrowser("iexplore", 3000)
  If browser.Exists Then
    Set page = browser.WaitPage("*", 3000)
    If page.Exists Then
      Set c = page.FindChild(Array("ObjectType", "attributes.title"), Array("Link", categoryName), 100)
      If c.Exists Then
        c.Click
      End If
    End If
    browser.Close
  End If
End Sub

Sub EventControl1_OnStopTest(Sender)
  If Not Project.TestItems.Current is Nothing Then
    logName = Project.Path & "Log\\" & Project.TestItems.Current.Name & ".mht"
    Call Log.SaveResultsAs(logName, lsMHT)
  End If
End Sub

DelphiScript

// Add this routine to the Test Items page and then run it
procedure Main();
var categoryName, url, browser, page, c;
begin
  categoryName := 'Furniture';
  url := 'http://services.smartbear.com/samples/TestComplete14/smartstore/';
  Browsers.Item(btIExplorer).Run(url);
  browser := Sys.WaitBrowser('iexplore', 3000);
  if browser.Exists then
  begin
    page := browser.WaitPage('*', 3000);
    if page.Exists then
    begin
      c := page.FindChild(['ObjectType', 'attributes.title'], ['Link', categoryName], 100);
      if c.Exists then
        c.Click;
    end;
    browser.Close;
  end;
end;

procedure EventControl1_OnStopTest(Sender);
var logName;
begin
  if Project.TestItems.Current <> nil then
  begin
    logName := Project.Path + 'Log\' + Project.TestItems.Current.Name + '.mht';
    Log.SaveResultsAs(logName, lsMHT);
  end;
end;

C++Script, C#Script

// Add this routine to the Test Items page and then run it
function Main()
{
  var categoryName = "Furniture";
  var url = "http://services.smartbear.com/samples/TestComplete14/smartstore/";
  Browsers["Item"](btIExplorer)["Run"](url);
  var browser = Sys["WaitBrowser"]("iexplore", 3000);
  if (browser["Exists"])
  {
    var page = browser["WaitPage"]("*", 3000);
    if (page["Exists"])
    {
      var c = page["FindChild"](new Array("ObjectType", "attributes.title"), new Array("Link", categoryName), 100);
      if (c["Exists"])
      {
        c["Click"]();
      }
    }
    browser["Close"]();
  }
}

function EventControl1_OnStopTest(Sender)
{
  if (Project["TestItems"]["Current"] != null)
  {
    var logName = Project["Path"] + "Log\\" + Project["TestItems"]["Current"]["Name"] + ".mht";
    Log["SaveResultsAs"](logName, lsMHT);
  }
}

From Keyword Tests

Get the property value described above by using the Call Object Method, Run Code Snippet, or Run Script Routine operation.

See Also

Running Tests
Tests, Test Items, and Test Cases
Test Items Page (Project Editor)
aqTestCase Object
Project.TestItems.Current

Highlight search results