qc.rb

code - Jing Li, 11/02/2010 12:48 AM

Download (3.13 KB)

 
1
require 'win32ole'
2
require 'rubygems'
3
require 'highline/import'
4

    
5
def get_interface_iid(interface_name)
6
        require 'win32/registry'
7
        interface_iid = ''
8
        reg_interface = Win32::Registry::HKEY_CLASSES_ROOT.open('Interface')
9
        reg_interface.each_key {|subkey, wtime| key = subkey.to_s
10
                reg_subkey = reg_interface.open(key)
11
                begin
12
                        interface_iid = key if reg_subkey.read_s('') == interface_name
13
                rescue
14
                        next
15
                end
16
        }
17
        reg_interface.close()
18
        return interface_iid
19
end
20

    
21
system 'cls'
22
tdcon = WIN32OLE.new('TDApiOle80.TDConnection')
23

    
24
puts 'Starting to login QualityCenter...'
25
url = 'https://qc10.xxx.com/qcbin'
26
login_domain = 'Domain'
27
login_project = 'Project'
28
wk_of_year = Time.now.strftime("%U").to_i
29

    
30
puts "\nConnect to server..."
31
tdcon.InitConnectionEx(url)
32
# puts "    Server Name = #{tdcon.ServerName}"
33
puts "    Connected = #{tdcon.Connected}"
34
puts "\nAuthenticate user..."
35
begin
36
        login_name = ask("    Please enter your user name: ")
37
        login_password = ask("    Please enter your password: ") {|q| q.echo = '*'}
38
        tdcon.Login(login_name, login_password)
39
rescue => exception
40
        regex_exception = /error code.*?\n.*?\n/
41
        exception = regex_exception.match(exception.to_s).to_s.gsub(/error code.*?\n/, '').chomp.gsub(/^\s+/, '')
42
        puts "        #{exception}"
43
        retry
44
end
45
puts "        LoggedIn = #{tdcon.LoggedIn}"
46
puts "\nLogin Project..."
47
tdcon.Connect(login_domain, login_project)
48
puts "    ConnectedToProject = #{tdcon.ProjectConnected}"
49

    
50
def check_node_and_add(td_connection, path, node)
51
        puts "\nFinding folder \"#{node}\"..."
52
        begin
53
                td_connection.TestSetTreeManager.NodeByPath(path).FindChildNode(node)
54
        rescue => exception
55
                regex_exception = /error code.*?\n.*?\n/
56
                exception = regex_exception.match(exception.to_s).to_s.gsub(/error code.*?\n/, '').chomp.gsub(/^\s+/, '')
57
                if exception == "Node not found."
58
                        puts "        #{exception}  Folder will be created."
59
                        puts "        Folder has been created successfully." if td_connection.TestSetTreeManager.NodeByPath(path).AddNode(node)
60
                end
61
        end
62
        puts "        Folder exists already." if exception == nil
63
end
64

    
65
path_1 = "Root\\Project1\\Test"
66
path_2 = "Root\\Project2\\Test"
67

    
68
check_node_and_add(tdcon, path_1, "wk#{wk_of_year}")
69
check_node_and_add(tdcon, "#{path_1}\\wk#{wk_of_year}", "Component")
70

    
71
iid_ISupportCopyPaste = get_interface_iid('ISupportCopyPaste')
72
copied_id = tdcon.TestSetTreeManager.NodeByPath("#{path_1}\\wk30\\Component").FindTestSets("BAT_2010_0808").Item(1).ID
73
pasted_id = tdcon.TestSetTreeManager.NodeByPath("#{path_1}\\wk#{wk_of_year}\\Component").NodeID
74
tstsf = tdcon.TestSetTreeManager.NodeByPath("#{path_1}\\wk30\\Component").TestSetFactory
75
iscp = tstsf.ole_query_interface(iid_ISupportCopyPaste)
76
clipboard = iscp.CopyToClipBoard(copied_id, 0, "")
77
iscp.PasteFromClipBoard(clipboard, pasted_id)
78
puts "\nTestSet has been copied to this week's test folder"
79

    
80
puts "\nDisconnect from project..."
81
tdcon.Disconnect()
82
puts "    ConnectedToProject = #{tdcon.ProjectConnected}"
83
puts "\nLogout..."
84
tdcon.Logout()
85
puts "    LoggedIn = #{tdcon.LoggedIn}"
86
puts "\nFinish connection..."
87
tdcon.ReleaseConnection()
88
puts "    Connected = #{tdcon.Connected}"