When we do QTP scripting, sometimes we need emulate keyboard actions such as typing letters, moving directions (Up, Down, Left,Right) and hitting some function keys such as “Enter” and F1. WSH enables us to do this. WSH is an abbreviation of Windows Script Host. It provides scripting abilities and is used for a variety of purposes, including logon scripts, administration and general automation.

Some QTP Scripting Examples

Below is a sample for emulating typing the“Enter” key.

  • Set WshShell =CreateObject(“WScript.Shell”)
  • WshShell.SendKeys “{ENTER}”

Notice that we use {} to identify this function key. If we want to emulate other function keys we can also use this way using the same syntax. For example:

  • WshShell.SendKeys “{F1}”
  • WshShell.SendKeys “{Down}”

If we just want to input a word such as “happy”, we can direct use the script command

  • WshShell.SendKeys “happy” without {}.

If we want to emulate typing a key like “h” 100 times, we can use such script as:
WshShell.SendKeys “{h 100}”. Instead of typing “h”100 times, notice that when we add the number of times as a parameter and need to use {}.

There are 3 special function keys (Shift, Ctrl, Alt) that use special characters:

  • For Shift, it is WshShell.SendKeys “+”
  • For Ctrl, it is WshShell.SendKeys “^”
  • For Alt, it is WshShell.SendKeys “%”

Note that if we need to emulate typing “+”, “^” and “%”, we need to add {} as WshShell.SendKeys “{^}” to distinguish them.

These are just a few of the most common keyboard emulation functions available via WSH for QTP scripting. For more examples of common functions, we’ll be writing about them in future blogs or you can just contact us if you have a question.

If you want to dive into automation scripting, its best to develop a framework before you begin. Otherwise your QTP scripts may get a bit disorganized and be difficult to maintain. Our software test automation best practices gives some tips on developing a good automation framework.