我正在编写代码,我发现错误"无法找到包trycatch"。
package require trycatch
namespace import trycatch
package require tcom
package require html
package require Thread
package require tdom
package require SQL
try {
global configFileValues
global File
set logFolder "D:/Work/Smart Test/Logs"
set windowTextOrWebUrl [lindex $argv 0]
puts "WindowText: $windowTextOrWebUrl"
# regsub -all {\[} "$windowTextOrWebUrl" {\[} windowTextOrWebUrl
# puts "WindowText: $windowTextOrWebUrl"
proc CreateSqlConnection {} {
global odbcHandle
set odbcHandle [SQL::SQLDriverConnect "Driver={MySQL ODBC 5.2a Driver};server=localhost;database=smarttest;user=root;password=pass"]
return $odbcHandle
}
proc ReleaseSqlConnection {} {
global odbcHandle
set disconnectVal [$odbcHandle SQLDisconnect]
# puts $disconnectVal
}
proc LogMaintenance {} {
global logFolder
global File
set Time [clock seconds]
set startTime [clock format $Time -format "%Y-%m-%d"]
# puts "$logFolder"
if {[file isdirectory $logFolder] == 0} {
file mkdir $logFolder
}
set fileName "Logs"
set extension ".txt"
set logFile [concat $logFolder/$startTime$extension]
set File [open $logFile a]
return $File
}
proc GetWebControls {url} {
global odbcHandle
set iapp [tcom::ref createobj "InternetExplorer.Application"]
$iapp Visible 1
$iapp Navigate $url
while {[$iapp Busy]} { after 500 }
after 500
set doc [$iapp Document]
set body [$doc body]
set elements [$body all]
for {set i 0} {$i < [$elements length]} {incr i} {
set controlType "NULL"
set controlId "NULL"
set controlName "NULL"
set controlCaption "NULL"
set controlDefaultValue "NULL"
set controlType [[$elements item $i] getAttribute type]
puts "ControlType: $controlType"
switch $controlType {
"button" {
puts "Button"
}
"text" {
# puts "Text"
set controlId [[$elements item $i] getAttribute id]
set controlCaption [[$elements item $i] getAttribute name]
set controlDefaultValue [[$elements item $i] getAttribute value]
set sqlQuery "Insert into t_controls (cntType, cntIndex, cntName, cntDefaultValue, cntCaption, cntScrId, modifiedBy, modifiedOn, createdBy,createdOn)
Values ('$controlType',NULL,'$controlId','$controlDefaultValue','$controlCaption',1,'BalGovind',NOW(),'BalGovind',NOW())"
# puts $sqlQuery
set queryResult [$odbcHandle SQLExecDirect $sqlQuery]
puts "Query result in WebControl(): $queryResult"
}
"submit" {
# puts "submit"
set controlId [[$elements item $i] getAttribute id]
set controlName [[$elements item $i] getAttribute name]
set controlCaption [[$elements item $i] getAttribute value]
set sqlQuery "Insert into t_controls (cntType, cntIndex, cntName, cntDefaultValue, cntCaption, cntScrId, modifiedBy, modifiedOn, createdBy,createdOn)
Values ('$controlType',NULL,'$controlId',NULL,'$controlCaption',1,'BalGovind',NOW(),'BalGovind',NOW())"
# puts $sqlQuery
set queryResult [$odbcHandle SQLExecDirect $sqlQuery]
puts "Query result in WebControl(): $queryResult"
}
"password" {
# puts "password"
set controlId [[$elements item $i] getAttribute id]
set controlCaption [[$elements item $i] getAttribute name]
set controlDefaultValue [[$elements item $i] getAttribute value]
set sqlQuery "Insert into t_controls (cntType, cntIndex, cntName, cntDefaultValue, cntCaption, cntScrId, modifiedBy, modifiedOn, createdBy,createdOn)
Values ('$controlType',NULL,'$controlId','$controlDefaultValue','$controlCaption',1,'BalGovind',NOW(),'BalGovind',NOW())"
# puts $sqlQuery
set queryResult [$odbcHandle SQLExecDirect $sqlQuery]
puts "Query result in WebControl(): $queryResult"
}
"label" {
# puts "label"
set controlId [[$elements item $i] getAttribute id]
set controlCaption [[$elements item $i] getAttribute value]
set sqlQuery "Insert into t_controls (cntType, cntIndex, cntName, cntDefaultValue, cntCaption, cntScrId, modifiedBy, modifiedOn, createdBy,createdOn)
Values ('$controlType',NULL,'$controlId','$controlDefaultValue','$controlCaption',1,'BalGovind',NOW(),'BalGovind',NOW())"
# puts $sqlQuery
set queryResult [$odbcHandle SQLExecDirect $sqlQuery]
puts "Query result in WebControl(): $queryResult"
}
}
unset controlType
unset controlId
unset controlName
unset controlCaption
unset controlDefaultValue
}
}
proc GetWindowControls {windowText} {
set app [::tcom::ref createobj "ManagedSpy.Utility.Utility"]
set bool [$app FetchControls $windowText]
if {$bool == 0} {
exit
}
}
proc GetControls {screenName} {
global windowTextOrWebUrl
set screenName $windowTextOrWebUrl
if {([regexp "www." "$screenName" matchedString]) || ([regexp "http" "$screenName" matchedString])} {
puts "Web Application."
GetWebControls $screenName
} else {
puts "Window Application."
GetWindowControls $screenName
}
}
proc Main {} {
puts "Executing Main Procedure!!!"
global File
global odbcHandle
global windowTextOrWebUrl
set File [LogMaintenance]
set currentTime [clock format [clock seconds] -format "%H-%M-%S-%p"]
puts $File "$currentTime :: Script started."
set odbcHandle [CreateSqlConnection]
GetControls $windowTextOrWebUrl
ReleaseSqlConnection
puts $File "[clock format [clock seconds] -format "%H-%M-%S-%p"] :: Script ends.\n\n######################################### \n"
close $File
}
}
catch -msg msg -code code -info info {
puts "catch"
puts "$msg $info"
puts $File "[clock format [clock seconds] -format "%H-%M-%S-%p"] :: Exception - $msg \n $code \n $info \n"
puts $File "[clock format [clock seconds] -format "%H-%M-%S-%p"] :: Script is ended.\n\n################################################## \n"
close $File
}
答案 0 :(得分:1)
trycatch
软件包的文档为online,我猜测来源为here。但是,您可能应该考虑切换到内置equivalent functionality的Tcl 8.6。
您的代码似乎也有其他问题。很少需要在错误捕获的上下文中声明过程;把调用放到......里面的那些程序更常见。