linux - #include 在 Expect 脚本中

标签 linux unix scripting automation expect

所以我在使用期望脚本制作测试用例时遇到了麻烦,我有大约 10 个测试用例,它们都以相同的“功能”开始和结束,例如登录和注销或关闭一些标志,是否有可能包含它们或执行它们从我的脚本远程操作,例如 spawn login.exp 或者甚至更好地将它们放入函数中?

TC01.exp

#!/usr/bin/expect -f
set timeout 5

#example of getting arguments passed from command line..
#not necessarily the best practice for passwords though...
set server [lindex $argv 0]
set user [lindex $argv 1]
set pass [lindex $argv 2]
set no [lindex $argv 3]
set counter 0

# connect to server via ssh, login, and su to root
send_user "connecting to $server\n"
spawn ssh $user@$server

#login handles cases:
#   login with keys (no user/pass)
#   user/pass
#   login with keys (first time verification)
expect {
  "> " { }
  "$ " { }

  "assword: " { 
        send "$pass\n" 
        expect {
          "> " { }
          "$ " { }
          "assword: " { 
                    send_user "\nLogin failed\n" 
                                    incr counter 1
                    exit 5

                }
        }
  }
  "(yes/no)? " { 
        send "yes\n"
        expect {
          "> " { }
          "$ " { }
        }
  }
 default {
        send_user "Login failed\n"
        incr counter 1
        exit
  }
}


#TEST CASE HERE


#login out
send "exit\n"

expect {
    "> " {}
    default {}
}


if { $counter > 0 } {
    send_user "\nTestCase finished with some errors!\nFAILED!!!\nERRORS $counter\n";
    exit 4;
}

send_user "\nTestCase finished with SUCCESS!\nERRORS: $counter\n";

所以我希望将 login 和 count_error 作为函数,这样我就能够像这样创建我的测试用例:

TC01.exp

#!/usr/bin/expect -f
set timeout 5
set server [lindex $argv 0]
set user [lindex $argv 1]
set pass [lindex $argv 2]
set no [lindex $argv 3]
set counter 0


login($server, $user, $pass)

#TestCase

Errors($counter)
exit

最佳答案

Expect 实际上是 Tcl,其中包含一些与 pty 和 fork() 的绑定(bind)。所有这些都在 http://tcl.tk 上得到了很好的描述。 Tcl 中的函数是通过 proc 完成的(查看 here )例如:

lib.tcl

proc login {server user pass} {
  # Your expect code goes here
  return $errorCount
}
proc errors {errorCount} { 
  if {$errorCount > 0} {
    # Your error handling code here
  }
}

测试:

#!/usr/bin/env expect
source lib.tcl
set errors [login $server $user $pass]
# your test case here
errors $errors

关于linux - #include 在 Expect 脚本中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23735573/

相关文章:

linux - 如何在创建时设置pthread名称?

node.js - Node 集群 worker 内存使用情况

windows - 适用于 Windows 的命令行对话框工具

javascript - 将脚本添加到 html 页面的 firefox 扩展

android - Linux机器无法识别android设备

linux -- 确定哪个服务在关闭的端口上运行

linux - 我如何使用 bash 脚本在后台执行 grep 链命令

java - 在不同的目录中使用 Java 的 exec 运行 .sh 文件?

node.js - Nodejs 服务器创建未运行

php - 脚本语言如何使用套接字?