cancel
Showing results for 
Search instead for 
Did you mean: 

Testing Destination of Windows Shortcut lnk in Start Menu Win7x64

SOLVED
Occasional Contributor

Testing Destination of Windows Shortcut lnk in Start Menu Win7x64

Hi,

 

I'm trying to get this JScript from https://support.smartbear.com/viewarticle/8967/ working in TestComplete 12.6 on Win7x64.

 

It's to verify the target value of the shortcut (what it's trying to run).

 

This script is throwing an error:

JavaScript runtime error

ReferenceError: ActiveXObject is not defined

Error location:

Unit "script unit name"

Line 14 Column 18



{
  if (!IsShortcutValid("Programs|Accessories|Paint", "|")) {
    Log.Error("Shortcut is not valid");
    return;
  }
  Log.Message("Shortcut is valid");
}
 
function IsShortcutValid(shortcutPath, separator)
{
  var WshShell, fso, startDir, allUsersStartDir, path, shortcut;
  
  WshShell = new ActiveXObject("WScript.Shell");
  startDir = WshShell.SpecialFolders("StartMenu") + "\\";
  allUsersStartDir = WshShell.SpecialFolders("AllUsersStartMenu") + "\\";
  path = shortcutPath.split(separator).join("\\") + ".lnk";
  fso = new ActiveXObject("Scripting.FileSystemObject");
  if (fso.FileExists(startDir + path)) {
    shortcut = WshShell.CreateShortcut(startDir + path);
    return fso.FileExists(shortcut.TargetPath);
  }
  if (fso.FileExists(allUsersStartDir + path)) {
    shortcut = WshShell.CreateShortcut(allUsersStartDir + path);
    return fso.FileExists(shortcut.TargetPath);
  }
  return false;
}

Any advice greatly appreciated.

 

Pete

1 ACCEPTED SOLUTION

Accepted Solutions
Occasional Contributor

Re: Testing Destination of Windows Shortcut lnk in Start Menu Win7x64

Thanks for the tips Robert.   This is what I ended up with in case anyone else is looking for this later, in javascript

function Test_SCExists_GetSCPath()
{
  if (!IsShortcutValid("Programs|Accessories|Paint", "|")) {
    Log.Error("Shortcut is not valid");
    return;
  }
  Log.Message("Shortcut is valid");
  
  
  var target = GetShortcutTarget("Programs|Accessories|Paint", "|");
  if (!target) {
    Log.Error("Shortcut doesn't exist");
    return;
  }
  Log.Message(target);
  
}
 
function IsShortcutValid(shortcutPath, separator)
{
  var StartDir, path, shortcut;
  StartDir = "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\";
  path = shortcutPath.split(separator).join("\\") + ".lnk";
  if (aqFileSystem.Exists(StartDir + path)) {
    shortcut = WshShell.CreateShortcut(StartDir + path);
    return aqFileSystem.Exists(shortcut.TargetPath);
  }
  return false;
}


function GetShortcutTarget(shortcutPath, separator)
{
  var StartDir, path, shortcut;
  StartDir = "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\";
  path = shortcutPath.split(separator).join("\\") + ".lnk";
  if (aqFileSystem.Exists(StartDir + path)) {
    shortcut = WshShell.CreateShortcut(StartDir + path);
    return shortcut.TargetPath;
  }
  return false;
}
2 REPLIES 2
Highlighted
Community Hero

Re: Testing Destination of Windows Shortcut lnk in Start Menu Win7x64

Both the WShell and the FileSystem object have analogs in TestComplete where you don't need to actually instantiate new objects.

 

https://support.smartbear.com/testcomplete/docs/reference/program-objects/wshshell/index.html

 

https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqfilesystem/index.html

 

I would use these objects instead of the "new ActiveXObject" calls that are in that article.


Robert Martin
[Community Expert Group]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Extensions available
Occasional Contributor

Re: Testing Destination of Windows Shortcut lnk in Start Menu Win7x64

Thanks for the tips Robert.   This is what I ended up with in case anyone else is looking for this later, in javascript

function Test_SCExists_GetSCPath()
{
  if (!IsShortcutValid("Programs|Accessories|Paint", "|")) {
    Log.Error("Shortcut is not valid");
    return;
  }
  Log.Message("Shortcut is valid");
  
  
  var target = GetShortcutTarget("Programs|Accessories|Paint", "|");
  if (!target) {
    Log.Error("Shortcut doesn't exist");
    return;
  }
  Log.Message(target);
  
}
 
function IsShortcutValid(shortcutPath, separator)
{
  var StartDir, path, shortcut;
  StartDir = "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\";
  path = shortcutPath.split(separator).join("\\") + ".lnk";
  if (aqFileSystem.Exists(StartDir + path)) {
    shortcut = WshShell.CreateShortcut(StartDir + path);
    return aqFileSystem.Exists(shortcut.TargetPath);
  }
  return false;
}


function GetShortcutTarget(shortcutPath, separator)
{
  var StartDir, path, shortcut;
  StartDir = "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\";
  path = shortcutPath.split(separator).join("\\") + ".lnk";
  if (aqFileSystem.Exists(StartDir + path)) {
    shortcut = WshShell.CreateShortcut(StartDir + path);
    return shortcut.TargetPath;
  }
  return false;
}
New Here?
Join us and watch the welcome video:
API Testing Mistake #2
APITestingMistake#2
Top Kudoed Authors