Sunday, March 25, 2012

Robot Framework - TypeKey Library

Implemented a library to simulate keyboard events. It can be used for upload and download operations along with Selenium.

TypeKeyLib - Library

# Author: Palani Selvam
#
# Purpose: Test library to simulate keyboard actions.
#

import time
import sys,os
import win32api, win32con
from win32com.client import Dispatch

from robot.errors import DataError
from robot import utils

__version__ = '0.1' # Developed by Palani

class TypeKeyLib:
ROBOT_LIBRARY_SCOPE = 'GLOBAL'
ROBOT_LIBRARY_VERSION = __version__

def __init__(self):
""" TypeKeyLib Library can be imported with optional arguments
"""
self._result='Init'
# To do

def TypeKeys(self,sKey):
""" TypeKeys - used to simulate keyboard keys. Parameter sKey can be STRING or LIST
For Key representation, see at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/wsmthsendkeys.asp

"""
try:
self._result='TypeKeys method'
shell=Dispatch("WScript.Shell")

stype = type(sKey).__name__
#print 'Given type: ', stype

if (stype == 'list'):
# Execute multiple times
for key1 in sKey:
shell.SendKeys(key1)
time.sleep (0.5)
else:
# Execute only one time
shell.SendKeys (sKey)
time.sleep(1)
#shell=Nothing
self._result='pass'
except:
self._result='fail'
print '*INFO* Exception occurs: ', sys.exc_type , ":", sys.exc_value
raise
finally:
self._result+= ' done'
return self._result

def TypeKeys_Space(self):
""" TypeKeys_Space - used to simulate SPACE.
"""
self.TypeKeys (" ")

NOTE: It will work only with Windows OS...

2 comments:

Премьер said...

Please tell me how can I install or set up this library ?

Palani Selvam said...

Hi,
You need to copy this python file into site-packages folder or pythonpath folder.

YOu may need to install ActiveState-Python OR Win32 module..