Edit
Attach
Printable
topic end
<!-- * Set TOPICTITLE = #define private public - Pasting my own dogfood, part 2 (09 Apr 2006) --> <style type="text/css"> pre {background-color:#ffeecc;} </style> %STARTINCLUDE% <a name="09"></a> ---+++ [[BlogOnSoftware20060409][Pasting my own dogfood, part 2]] (09 Apr 2006) [[BlogOnSoftware20060409][Yesterday]], I introduced the problem of how to automatically test Windows clipboard code in applications. The idea is to move from manual and error-prone clickety-click style testing to an automatic process which produces reliable results. <img src="%ATTACHURL%/clipbrdviewer.png" alt="Clipboard viewer" align="right" /> Unbeknownst to many, Windows ships with a fairly interesting tool called the <nop>ClipBook Viewer (=clipbrd.exe=), which monitors what the clipboard contains, and will even display the formats it knows about. This is quite helpful while developing and debugging clipboard code. However, <nop>ClipBook Viewer can even help with test automation since it can save the current clipboard contents to =*.CLP= files and load them back into the clipboard later. Which, in fact, is almost all we need to thoroughly and reliably test clipboard code: We run some apps which produce a good variety of clipboard formats which our own application needs to deal with. We select some data, copy them to the clipboard, then save the clipboard contents as a =*.CLP= file from <nop>ClipBook Viewer. Once we have created a reasonably-sized clipboard file library, we run <nop>ClipBook viewer and load each one of those clipboard files in turn. After loading, we switch to our own app, paste the data and check whether the incoming data makes sense to us. Not bad at all! But alas, I could not find a way to automate the <nop>ClipBook Viewer via the command-line or COM interfaces. If someone knows about such interfaces, I'm certainly most interested to hear about them. <img src="%ATTACHURL%/clearclipboard.png" align="right" /> Luck would have it that only recently, I [[BlogOnSoftware20060303][blogged about poor man's automation via <tt>SendKeys</tt>]]. The idea is to write a small shell script which runs the target application (here: =clipbrd.exe=), and then simulate how a user presses keys to use the application. =clipbrd.exe= can be started with the name of a =*.CLP= file in its command line, and will then automatically load this file. However, before it pushes the contents of the file to clipboard, it will ask the user for confirmation in a message box. Well, in fact, first it will try to establish <nop>NetDDE connections, and will usually waste quite a bit of time for this. The following script tries to take this into account: <pre> Set WshShell = WScript.CreateObject("WScript.Shell") WshShell.Run("clipbrd.exe c:\temp\clip.CLP") WScript.Sleep 5000 ' Wait for "Clear clipboard (yes/no)?" message box WshShell.SendKeys "{ENTER}" </pre> Now we could add some more scripting glue code to control our own application, have it execute its "Paste" functionality and verify that the data arrives in all its expected glory. The above code is not quite that useful if we need to run a set of tests in a loop; the following modified version is better suited for that purpose. It assumes that all =*.CLP= files are stored in =c:\temp\clipfiles=. <pre> <font color="#a020f0">Set</font> WshShell <font color="#804040"><b>=</b></font> WScript<font color="#804040"><b>.</b></font><font color="#008080">CreateObject</font><font color="#804040"><b>(</b></font><font color="#ff00ff">"WScript.Shell"</font><font color="#804040"><b>)</b></font> WshShell<font color="#804040"><b>.</b></font>Run<font color="#804040"><b>(</b></font><font color="#ff00ff">"clipbrd.exe"</font><font color="#804040"><b>)</b></font> WScript<font color="#804040"><b>.</b></font>Sleep <font color="#ff00ff">20000</font> startFolder<font color="#804040"><b>=</b></font><font color="#ff00ff">"c:\temp\clipfiles"</font> <font color="#a020f0">set</font> folder<font color="#804040"><b>=</b></font><font color="#008080">CreateObject</font><font color="#804040"><b>(</b></font><font color="#ff00ff">"Scripting.FileSystemObject"</font><font color="#804040"><b>).</b></font>GetFolder<font color="#804040"><b>(</b></font>startFolder<font color="#804040"><b>)</b></font> <font color="#804040"><b>for</b></font> <font color="#804040"><b>each</b></font> file <font color="#804040"><b>in</b></font> folder<font color="#804040"><b>.</b></font><font color="#a020f0">files</font> WScript<font color="#804040"><b>.</b></font>Echo <font color="#ff00ff">"Now testing "</font> <font color="#804040"><b>&</b></font> file<font color="#804040"><b>.</b></font>Path OpenClipFile<font color="#804040"><b>(</b></font>file<font color="#804040"><b>.</b></font>Path<font color="#804040"><b>)</b></font> <font color="#0000ff">' Add here:</font> <font color="#0000ff">' - Activate application under test</font> <font color="#0000ff">' - Have it paste data from the clipboard</font> <font color="#0000ff">' - Verify that the data comes in as expected</font> <font color="#804040"><b>next</b></font> <font color="#0000ff">' Close clipbrd.exe</font> WshShell<font color="#804040"><b>.</b></font><font color="#804040"><b>AppActivate</b></font><font color="#804040"><b>(</b></font><font color="#ff00ff">"ClipBook Viewer"</font><font color="#804040"><b>)</b></font> WshShell<font color="#804040"><b>.</b></font><font color="#804040"><b>SendKeys</b></font> <font color="#ff00ff">"%F"</font> WScript<font color="#804040"><b>.</b></font>Sleep <font color="#ff00ff">1000</font> WshShell<font color="#804040"><b>.</b></font><font color="#804040"><b>SendKeys</b></font> <font color="#ff00ff">"x"</font> <font color="#804040"><b>Sub</b></font> OpenClipFile<font color="#804040"><b>(</b></font>filename<font color="#804040"><b>)</b></font> WshShell<font color="#804040"><b>.</b></font><font color="#804040"><b>AppActivate</b></font><font color="#804040"><b>(</b></font><font color="#ff00ff">"ClipBook Viewer"</font><font color="#804040"><b>)</b></font> WshShell<font color="#804040"><b>.</b></font><font color="#804040"><b>SendKeys</b></font> <font color="#ff00ff">"%W"</font> <font color="#0000ff">' ALT-W for Windows menu</font> WScript<font color="#804040"><b>.</b></font>Sleep <font color="#ff00ff">500</font> WshShell<font color="#804040"><b>.</b></font><font color="#804040"><b>SendKeys</b></font> <font color="#ff00ff">"1"</font> <font color="#0000ff">' Activate Clipboard window</font> WScript<font color="#804040"><b>.</b></font>Sleep <font color="#ff00ff">500</font> WshShell<font color="#804040"><b>.</b></font><font color="#804040"><b>SendKeys</b></font> <font color="#ff00ff">"%F"</font> <font color="#0000ff">' ALT-F for File menu</font> WScript<font color="#804040"><b>.</b></font>Sleep <font color="#ff00ff">1000</font> WshShell<font color="#804040"><b>.</b></font><font color="#804040"><b>SendKeys</b></font> <font color="#ff00ff">"O"</font> WScript<font color="#804040"><b>.</b></font>Sleep <font color="#ff00ff">1000</font> WshShell<font color="#804040"><b>.</b></font><font color="#804040"><b>SendKeys</b></font> filenam9e WScript<font color="#804040"><b>.</b></font>Sleep <font color="#ff00ff">1000</font> WshShell<font color="#804040"><b>.</b></font><font color="#804040"><b>SendKeys</b></font> <font color="#ff00ff">"{ENTER}"</font> WScript<font color="#804040"><b>.</b></font>Sleep <font color="#ff00ff">1000</font> <font color="#0000ff">' Wait for "Clear clipboard (yes/no)?"</font> WshShell<font color="#804040"><b>.</b></font><font color="#804040"><b>SendKeys</b></font> <font color="#ff00ff">"{ENTER}"</font> <font color="#804040"><b>End</b></font> <font color="#804040"><b>Sub</b></font> </pre> I'm sure a <nop>VBscript hacker could tidy this up considerably and use it to form a complete test suite. However, while this approach finally gives us some degree of automation, it is still lacking in several ways: * The format for the =*.CLP= file is undocumented, so we cannot add clipboard data of our own, unless we first copy it to the clipboard, then save it from there using <nop>ClipBook Viewer. * Automation via sending keys is a very brittle approach. For instance, the above code was written for the English version of =clipbrd.exe=. The German or French or Lithuanian versions of =clipbrd.exe= might have completely different keyboard shortcuts. * I shudder when looking at those magic delay time values which the code is ridden with - what if we run on a really slow system? On a system which has even more networking problems than the one which I tested the code with? * Any process (or user) stealing the window focus while the test is running will break the test. Hence, next time: Look Ma, no <tt>SendKeys</tt>! --- %STOPINCLUDE% %COMMENT{type="below" nonotify="on"}% ---
to top
End of topic
Skip to action links
|
Back to top
Edit
|
Attach image or document
|
Printable version
|
Raw text
|
Refresh
|
More topic actions
Revisions: | r1.9 |
>
|
r1.8
|
>
|
r1.7
|
Total page history
|
Backlinks
You are here:
Blog
>
BlogOnSoftware20060409
r1.9 - 14 Apr 2007 - 16:15 -
ClausBrod
to top
Blog
This site
2017
:
12
-
11
-
10
2016
:
10
-
7
-
3
2015
:
11
-
10
-
9
-
4
-
1
2014
:
5
2013
:
9
-
8
-
7
-
6
-
5
2012
:
2
-
10
2011
:
1
-
8
-
9
-
10
-
12
2010
:
11
-
10
-
9
-
4
2009
:
11
-
9
-
8
-
7
-
6
-
5
-
4
-
3
2008
:
5
-
4
-
3
-
1
2007:
12
-
8
-
7
-
6
-
5
-
4
-
3
-
1
2006:
4
-
3
-
2
-
1
2005:
12
-
6
-
5
-
4
2004:
12
-
11
-
10
C++
CoCreate Modeling
COM & .NET
Java
Mac
Lisp
OpenSource
Scripting
Windows
Stuff
Changes
Index
Search
Maintenance
Impressum
Datenschutzerklärung
Home
Webs
Atari
Blog
Claus
CoCreateModeling
Klassentreffen
Main
Sandbox
Sommelier
TWiki
Xplm
Jump:
Copyright © 1999-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki?
Send feedback