-
WinRunner基本函数扩展-1
2007-08-24 18:11:06
#########################################################
#
# This compiled module contains miscellanous auxiliary functions
#
# LIST of FUNCTIONS:
#
# General:
#
# min, max
#
# String functions:
#
# abs
# rindex
# ltrim, rtrim
# repl
# count_str
# isalpha, isdigit
# gen_str
#
# File Operations:
#
# gen_file
# dirname
# f_tmp_name
#
########################################################## -------------------------------------------------------------------------------
# DEscrīptION: The function returns the lesser of two numbers passed to
# the function as parameters.
#
# PARAMETERS: in a and in b - two numbers to get the lesser of them
#
# RETURN CODE: Lesser of the two parameters passed to the function .
#
public function min(in a, in b)
{
if (a > b)
return(b);
else
return(a);
}
# -------------------------------------------------------------------------------
# DEscrīptION: The function returns the greater of two numbers passed to
# the function as parameters.
#
# PARAMETERS: in a and in b - two numbers to get the greater of them
#
# RETURN CODE: Greater of the two parameters passed to the function .
#
public function max(in a, in b)
{
if (a > b)
return(a);
else
return(b);
}# -------------------------------------------------------------------------------
# DEscrīptION: This function performs an absolute value on a number.
#
# PARAMETERS: in number - a number.
#
# RETURN CODE: Returns the abs number.
#
public function abs(in number)
{
return number <0 ? -number : number;
}
# -------------------------------------------------------------------------------
# DEscrīptION: The function returns a position of the last occurrence of the
# string s2 in the string s1.
#
# PARAMETERS: in s1 and in s2 - character strings.
#
# RETURN CODE: Position of the last occurrence of the string s2 in the string s1.
#
public function rindex(in s1, in s2)
{
auto ptr, lpos, src;
auto s2_len = length(s2);src = s1;
lpos = 0;while ((ptr = index(src, s2)) != 0) {
lpos += ptr + (s2_len - 1);
src = substr(src, ptr + s2_len);
}if (lpos > 0)
lpos -= s2_len - 1;return(lpos);
}
# -------------------------------------------------------------------------------
# DEscrīptION: The function removes leading spaces and tabs in the string s.
#
# PARAMETERS: in s - character string.
#
# RETURN CODE: A string that is the copy of s where leading spaces and tabs are
# removed .
#
public function ltrim(in s)
{
auto i;
auto src = s;while ((index(src, " ") == 1) || (index(src, "\t") == 1))
src = substr(src, 2);return(src);
}
# -------------------------------------------------------------------------------
# DEscrīptION: The function removes trailing spaces and tabs in the string s.
#
# PARAMETERS: in s - character string.
#
# RETURN CODE: A string that is the copy of s where trailing spaces and tabs are
# removed.
#
public function rtrim(in s)
{
auto i;
auto src = s;while ((rindex(src, " ") == length(src)) || (rindex(src, "\t") == length(src))) {
src = substr(src, 1, length(src) - 1);
if (length(src) == 0)
break;
}return(src);
}
# -------------------------------------------------------------------------------
# DEscrīptION: The function replaces all occurrences of the substring sub1 in the
# string s by the substring sub2.
#
# PARAMETERS: in s, in sub1, in sub2 - character strings.
#
# RETURN CODE: A string that is the copy of s where all occurrences of the substring
# sub1 in the string s are replaced by the substring sub2.
#
public function repl_str(in s, in sub1, in sub2)
{
auto ptr;
auto src = s;
auto sub1_len = length(sub1);while (ptr = index(src, sub1))
src = substr(src, 1, ptr - 1) & sub2 & substr(src, ptr + sub1_len);return(src);
}
# -------------------------------------------------------------------------------
# DEscrīptION: The function returns the number of occurrences of the substring s2
# in the string s1.
#
# PARAMETERS: in s1, in s2 - character strings.
#
# RETURN CODE: A number of occurrences of the substring s2 in the string s1.
#
public function count_str(in s1, in s2)
{
auto src = s1;
auto s2_len = length(s2);
auto sub_cnt, ptr;for (sub_cnt = 0; ptr = index(src, s2); sub_cnt++)
src = substr(src, ptr + s2_len);return(sub_cnt);
}
# -------------------------------------------------------------------------------
# DEscrīptION: The function checks if c is a letter.
#
# PARAMETERS: in c - a character.
#
# RETURN CODE: 1 - if c is a letter, otherwise - 0.
#
public function isalpha(c)
{
auto ch = ascii(substr(c, 1, 1));if ((ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122))
return(1);
else
return(0);
}
# -------------------------------------------------------------------------------
# DEscrīptION: The function checks if c is a digit.
#
# PARAMETERS: in c - a character.
#
# RETURN CODE: 1 - if c is a digit, otherwise - 0.
#
public function isdigit(c)
{
auto ch = ascii(substr(c, 1, 1));if (ch >= 48 && ch <= 57)
return(1);
else
return(0);
}
# -------------------------------------------------------------------------------
# DEscrīptION: The function generates a random string (containing characters
# which ASCII is in the range 32 - 126).
#
# PARAMETERS: in slen - length of the string to generate,
# in max_wlen - maximal length of a word in the string (length of
# a word is random restricted from the top by
# max_wlen).
#
# RETURN CODE: The generated random string.
#
public function gen_str(in slen, in max_wlen)
{
auto rand_ch, wlen, word, i;
auto str = "";# If max_wlen is not defined use the default.
#
if (nargs() < 2)
max_wlen = min(slen, 7);srand();
# Generate random string.
#
while (length(str) < slen) {
# Generate random word.
#
word = "";
wlen = min(int(rand() * max_wlen) + 1, slen - length(str));for (i = 0; i < wlen; i++) {
# Generate random character.
rand_ch = sprintf("%c", int(rand() * 95) + 32);
word = word & rand_ch;
}str = str & word;
if (length(str) < slen)
str = str & " ";
}return(str);
}######## File Operations #####################
#
## -------------------------------------------------------------------------------
# DEscrīptION: The function generates a file consisting of a random string
# (containing characters which ASCII is in the range 32 - 126).
#
# PARAMETERS: in fname - the name of the file to generate,
# in slen - length of the string to generate,
# in scnt - number of lines in the file.
#
# RETURN CODE: None.
#
public function gen_file(in fname, in slen, in scnt)
{
auto i;file_open(fname, FO_MODE_WRITE);
for (i = 1; i <= scnt; i++)
file_printf(fname,"%s\r\n", gen_str(slen));file_close(fname);
}
# -------------------------------------------------------------------------------
# DEscrīptION: The function returns all but the last level of the pathname in fp.
#
# PARAMETERS: in fp - the pathname to proceed.
#
# RETURN CODE: Pathname without the last level.
#
public function dirname(in fp)
{
auto file_name, i, d, dirs[], st;if( nargs() < 1)
call_chain_get_attr("testname", 1, file_name);
else
file_name = fp;if (!index(file_name, FILE_SEP)) # If file_name is only name of a file.
if( file_name = "." )
return "..\\.";
else
return "";do
file_name = substr(file_name,1,length(file_name)-1);
while (substr(file_name,length(file_name)) != FILE_SEP);if (file_name == "." )
file_name = "..\\.";return file_name;
}# -------------------------------------------------------------------------------
# DEscrīptION: The function generates a random temporary file name.
#
# PARAMETERS:
#
# RETURN CODE: Returns random file name.
#
public function f_tmp_name() {
auto c_time;c_time = get_time();
return getenv("TMP") & "\\tst" & substr(c_time, length(c_time) - 4);
} -
通过api来实现等待某个窗体消失的功能
2007-06-29 17:48:23
在winrunner中,经常会碰到需要等待某个窗体消失的情况,winrunner提供了一个win_wait_info()函数来实现这个功能,但是这个函数在实际运用中,效率并不是很好。特别是如果等待的窗体的出现时间很短,以致于在还没运行win_wait_info()函数还没运行的时候这个窗体已经消失的时候,winrunner就会等待设定的时间并且报错。 这在实际运用中带来了很大的问题。因为win_wait_info()这个函数并不能很好的判断这个窗体如果不存在的情况。
既然winrunner支持windows api,那么可不可以通过api调用的方式来实现这个函数的功能呢?答案是肯定的,而且在实际的运用过程中,效果比win_wait_info()要好很多。下面是具体的代码供参考:
load_dll("user32.dll");
extern long IsWindowVisible(long);
extern long FindWindowA(string,string);
function ni_win_wait_disapear(winClass,winName)
{auto handle;
handle = FindWindowA(winClass,winName);
if(handle == 0) return 0;
else
{
while(IsWindowVisible(handle))
wait(1);
}}
其中winClass表示窗口的类名,winName表示窗口的名字;同时可以通过修改FindWindowA的声明,来定义只需要其中某个参数就可以实现。具体的大家可以看看windows api的参数说明