• Support
  • Articles
  • Resources
  • Products

Test for a string on the visible screen

Moderator: boris

2 postsPage 1 of 1

Vhati
Newbie

Posts:
3
Joined: Wed Mar 09, 2011 7:59 am
by Vhati » Wed Mar 09, 2011 9:46 am
;   ANSI/VT100 allows regions to be partially typed-over (eg curses menus).
;   So wait commands watching the raw stream see the edits, not the revised lines.

I like to do sanity checks before sending keystrokes, so this was irritating. It's why I gave up writing an automated client in C. I nearly rigged RealTerm with AutoHotKey. :oops: Luckily TeraTerm provides terminal emulation and slightly-kludgy screencap on top of some other features that made my life easier. :D

; waitForScreen and strOnScreen
;   Waits for a short string, pauses half a second, flushes the
;   recv buffer, then scans the visible screen for a string in context.

With these subroutines, you can delay until a menu choice is available, stall a little to let painting settle down, and then confirm the choice is in a context you were expecting. For example, when I traverse menus on a ProCurve switch I've got, the names of choices in submenus are written over the earlier menu's names. But the numbers next to the choices are left on the screen from before. So I have to do a screen dump to check that the choice I want has the number I'm about to send.

This screen dump doesn't touch a file, and it handles a corner case when the string you're looking for is partly in one 255-char buffer and partly in another.

; isScreenBlank

The switch normally starts out blank and unresponsive except for pressing enter twice. So I know to abort if I send an arrow key and the screen's no longer blank.

Code: Select all

  1. ;;;
  2. ; waitForScreen
  3. ;   Waits for a short string, pauses half a second, flushes the
  4. ;   recv buffer, then scans the visible screen for a string in context.
  5. ;   Global Arg Vars:
  6. ;     waitStr - a string to search for in the raw stream.
  7. ;     screenStr - a string to search for on the screen.
  8. ;   Clobbered Vars:
  9. ;     [See strOnScreen]
  10. ;   Return Vars:
  11. ;     result - 0 waiting timed out.
  12. ;              1 waitStr found but not screenStr.
  13. ;              2 both waitStr and screenStr found.
  14. ;
  15. :waitForScreen
  16.   wait waitStr
  17.   if (result == 1) then
  18.     mpause 500
  19.     flushrecv
  20.     call strOnScreen
  21.     result = result + 1
  22.   endif
  23. return
  24.  
  25. ;;;
  26. ; strOnScreen
  27. ;   Scans the visible screen for a substring.
  28. ;   ANSI/VT100 allows regions to be partially typed-over (eg curses menus).
  29. ;   So wait commands watching the raw stream see the edits, not the revised lines.
  30. ;   You may want to mpause and flushrecv beforehand to finish painting.
  31. ;   Global Arg Vars:
  32. ;     screenStr - a string to search for.
  33. ;   Clobbered Vars:
  34. ;     screenStrLen,oldBuf,midBuf,newBuf,offset,tmp,keepLooping,screenStrFound,result
  35. ;   Return Vars:
  36. ;     result - 1 if screenStr found, 0 otherwise
  37. ;
  38. :strOnScreen
  39.   screenStrFound = 0
  40.   strlen screenStr
  41.   screenStrLen = result
  42.   if (screenStrLen != 0) then
  43.     callmenu 50280  ; select screen
  44.     callmenu 50210  ; copy to clipboard
  45.     callmenu 50270  ; reset selection
  46.     mpause 500
  47.  
  48.     oldBuf = ""
  49.     midBuf = ""
  50.     newBuf = ""
  51.     offset = 0
  52.     do
  53.       keepLooping = 0
  54.       clipb2var newBuf offset
  55.       if (result > 0) then
  56.         keepLooping = 1
  57.         if (offset > 0) then
  58.           ; Make a tiny intermediate buffer in case
  59.           ;   the screenStr straddles the 255 char string limit
  60.  
  61.           ; strcopy doesn't freak if an index is <0 or >length,
  62.           ;   so I don't bother checking
  63.           strcopy oldBuf (255-screenStrLen) screenStrLen midBuf
  64.           strcopy newBuf 1 screenStrLen tmp
  65.           strconcat midBuf tmp
  66.           strscan midBuf screenStr
  67.           if (result != 0) then
  68.             screenStrFound = 1
  69.             break
  70.           endif
  71.         endif
  72.  
  73.         strscan newBuf screenStr
  74.         if (result != 0) then
  75.           screenStrFound = 1
  76.           break
  77.         endif
  78.         oldBuf = newBuf
  79.       endif
  80.       offset = offset + 1
  81.     loop while (keepLooping == 1)
  82.  
  83.     var2clipb ""
  84.   endif
  85.  
  86.   result = screenStrFound
  87. return
  88.  
  89. ;;;
  90. ; isScreenBlank
  91. ;   Tests if the visible screen is blank (entirely linebreaks).
  92. ;   You may want to mpause and flushrecv beforehand to finish painting.
  93. ;   Global Arg Vars:
  94. ;     None
  95. ;   Clobbered Vars:
  96. ;     newBuf,offset,keepLooping,isBlank,result
  97. ;   Return Vars:
  98. ;     result - 1 if blank, 0 otherwise
  99. ;
  100. :isScreenBlank
  101.   callmenu 50280  ; select screen
  102.   callmenu 50210  ; copy to clipboard
  103.   callmenu 50270  ; reset selection
  104.   mpause 500
  105.  
  106.   isBlank = 1
  107.   newBuf = ""
  108.   offset = 0
  109.   do
  110.     keepLooping = 0
  111.     clipb2var newBuf offset
  112.     if (result > 0) then
  113.       keepLooping = 1
  114.       strreplace newBuf 1 "[\r\n]+" ""
  115.       strlen newBuf
  116.       if (result > 0) then
  117.         isBlank = 0
  118.         break
  119.       endif
  120.     endif
  121.     offset = offset + 1
  122.   loop while (keepLooping == 1)
  123.  
  124.   var2clipb ""
  125.   result = isBlank
  126. return



Example waitForScreen Usage:

Code: Select all

  1. ; ...
  2. setsync 1
  3. timeout=3
  4. ; ...
  5. waitStr = "Command Prompt"
  6. screenStr = "4. Command Prompt"
  7. call waitForScreen
  8. if (result != 2) then
  9.   if (result == 0) then
  10.     messagebox "Error: Missed the Cmd Prompt choice." "ProCurve Config"
  11.   elseif (result == 1) then
  12.     messagebox "Error: Cmd Prompt choice wasn't 4." "ProCurve Config"
  13.   endif
  14.   goto die
  15. endif
  16. send "4"  ; Okay, it's really there. Pick it.
  17.  
  18. ; ... more code ...
  19.  
  20. :die
  21. end


Example isScreenBlank Usage:

Code: Select all

  1. ; ...
  2. setsync 1
  3. ; ...
  4. ; Send a down-cursor keystroke
  5. ; Nothing should appear on the screen if this is a new session
  6. sendkcode 336 1
  7. mpause 2500
  8. flushrecv
  9. call isScreenBlank
  10. if (result == 0) then
  11.   messagebox "Error: Console in-use." "ProCurve Config"
  12.   goto die
  13. endif
  14. goto die
  15. sendln
  16.  
LuannD
Newbie

Posts:
1
Joined: Wed Jul 18, 2012 1:59 pm
by LuannD » Wed Jul 18, 2012 2:03 pm
Thanks for the scripts and codes shared here)
I'm really interested in them and this info was right for me.
Glad to have found this forum today)
Display posts from previous:
Sort by:

2 postsPage 1 of 1

Users browsing this forum: No registered users