1 | #include "StdAfx.h"
|
---|
2 | #include "LogParser.h"
|
---|
3 |
|
---|
4 | #include <iostream>
|
---|
5 |
|
---|
6 | #include "WindowFinder.h"
|
---|
7 |
|
---|
8 | LogParser::LogParser(_TCHAR* runCommand, unsigned int startupTime, bool useDefaultDelay) : runCommand(runCommand), startupTime(startupTime), useDefaultDelay(useDefaultDelay)
|
---|
9 | {
|
---|
10 |
|
---|
11 | }
|
---|
12 |
|
---|
13 | LogParser::~LogParser(void)
|
---|
14 | {
|
---|
15 |
|
---|
16 | }
|
---|
17 |
|
---|
18 |
|
---|
19 | HRESULT STDMETHODCALLTYPE LogParser::startElement(
|
---|
20 | wchar_t __RPC_FAR *pwchNamespaceUri,
|
---|
21 | int cchNamespaceUri,
|
---|
22 | wchar_t __RPC_FAR *pwchLocalName,
|
---|
23 | int cchLocalName,
|
---|
24 | wchar_t __RPC_FAR *pwchRawName,
|
---|
25 | int cchRawName,
|
---|
26 | MSXML2::ISAXAttributes __RPC_FAR *pAttributes)
|
---|
27 | {
|
---|
28 | std::wstring localName(pwchLocalName);
|
---|
29 | if( localName.compare(L"session")==0 ) {
|
---|
30 | std::wstring sessionId = GetAttributeValue(pAttributes, L"id", L"");
|
---|
31 | std::wcout << L"================================================" << std::endl;
|
---|
32 | std::wcout << L"starting session " << sessionId << std::endl;
|
---|
33 | std::wcout << L"executing " << runCommand << std::endl;
|
---|
34 | PROCESS_INFORMATION pi;
|
---|
35 | STARTUPINFO si;
|
---|
36 | ZeroMemory(&pi, sizeof(pi));
|
---|
37 | ZeroMemory(&si, sizeof(si));
|
---|
38 | CreateProcess(NULL, runCommand, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
|
---|
39 | hProcess = pi.hProcess;
|
---|
40 | CloseHandle(pi.hThread);
|
---|
41 | std::wcout << L"waiting " << startupTime << L" ms for application under test to intialize" << std::endl;
|
---|
42 | Sleep(startupTime);
|
---|
43 | std::wcout << L"replay starting..." << std::endl;
|
---|
44 | }
|
---|
45 | else if( localName.compare(L"msg")==0 ) {
|
---|
46 | std::wstring type = GetAttributeValue(pAttributes, L"type", L"");
|
---|
47 | msgType = _wtoi(type.c_str());
|
---|
48 | std::wstring lParamStr = GetAttributeValue(pAttributes, L"LPARAM", L"");
|
---|
49 | lParam = _wtoi(lParamStr.c_str());
|
---|
50 | std::wstring wParamStr = GetAttributeValue(pAttributes, L"WPARAM", L"");
|
---|
51 | wParam = _wtoi(wParamStr.c_str());
|
---|
52 | std::wstring delayStr = GetAttributeValue(pAttributes, L"delay", L"");
|
---|
53 | delay = _wtoi(delayStr.c_str());
|
---|
54 | currentWindow = NULL;
|
---|
55 | currentParent = NULL;
|
---|
56 | }
|
---|
57 | else if( localName.compare(L"window")==0 ) {
|
---|
58 | WindowData * winData = new WindowData;
|
---|
59 | winData->name = GetAttributeValue(pAttributes, L"name", L"");
|
---|
60 | winData->className = GetAttributeValue(pAttributes, L"class", L"");
|
---|
61 | std::wstring resourceIdStr = GetAttributeValue(pAttributes, L"resourceId", L"");
|
---|
62 | winData->resourceId = _wtoi(resourceIdStr.c_str());
|
---|
63 | std::wstring isModalStr = GetAttributeValue(pAttributes, L"isModal", L"");
|
---|
64 | if( isModalStr.compare(L"true")==0 ) {
|
---|
65 | winData->isModal = true;
|
---|
66 | } else {
|
---|
67 | winData->isModal = false;
|
---|
68 | }
|
---|
69 | winData->child = NULL;
|
---|
70 | if( currentWindow==NULL ) {
|
---|
71 | currentWindow = winData;
|
---|
72 | } else {
|
---|
73 | currentParent->child = winData;
|
---|
74 | }
|
---|
75 | currentParent = winData;
|
---|
76 | }
|
---|
77 | return S_OK;
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | HRESULT STDMETHODCALLTYPE LogParser::endElement(
|
---|
82 | wchar_t __RPC_FAR *pwchNamespaceUri,
|
---|
83 | int cchNamespaceUri,
|
---|
84 | wchar_t __RPC_FAR *pwchLocalName,
|
---|
85 | int cchLocalName,
|
---|
86 | wchar_t __RPC_FAR *pwchRawName,
|
---|
87 | int cchRawName)
|
---|
88 | {
|
---|
89 | std::wstring localName(pwchLocalName);
|
---|
90 | if( localName.compare(L"session")==0 ) {
|
---|
91 | std::wcout << L"session completed" << std::endl;
|
---|
92 | BOOL retVal = TerminateProcess(hProcess, 0);
|
---|
93 | if( retVal!=0 ) {
|
---|
94 | std::wcout << L"application terminated" << std::endl;
|
---|
95 | }
|
---|
96 | CloseHandle(hProcess);
|
---|
97 | }
|
---|
98 | else if( localName.compare(L"msg")==0 ) {
|
---|
99 | WindowFinder finder;
|
---|
100 | HWND hwnd = finder.find(currentWindow);
|
---|
101 | sendMessage(hwnd);
|
---|
102 | deleteWindowData(currentWindow);
|
---|
103 | currentWindow = NULL;
|
---|
104 | }
|
---|
105 | else if( localName.compare(L"LPARAM")==0 ) {
|
---|
106 | WindowFinder finder;
|
---|
107 | HWND hwnd = finder.find(currentWindow);
|
---|
108 | lParam = (LPARAM) hwnd;
|
---|
109 | deleteWindowData(currentWindow);
|
---|
110 | currentWindow = NULL;
|
---|
111 | }
|
---|
112 | else if( localName.compare(L"WPARAM")==0 ) {
|
---|
113 | WindowFinder finder;
|
---|
114 | HWND hwnd = finder.find(currentWindow);
|
---|
115 | wParam = (WPARAM) hwnd;
|
---|
116 | deleteWindowData(currentWindow);
|
---|
117 | currentWindow = NULL;
|
---|
118 | }
|
---|
119 | return S_OK;
|
---|
120 | }
|
---|
121 |
|
---|
122 | std::wstring LogParser::GetAttributeValue(MSXML2::ISAXAttributes __RPC_FAR *pAttributes,
|
---|
123 | std::wstring name, std::wstring defvalue)
|
---|
124 | {
|
---|
125 | // get the number of attributes
|
---|
126 | int length = 0;
|
---|
127 | pAttributes->getLength(&length);
|
---|
128 |
|
---|
129 | // enumerate over all attributes
|
---|
130 | for ( int i=0; i<length; i++ )
|
---|
131 | {
|
---|
132 | wchar_t *attrname = NULL, * attrvalue = NULL;
|
---|
133 | int namelen = 0, valuelen = 0;
|
---|
134 |
|
---|
135 | // get the local name of the current attribute
|
---|
136 | pAttributes->getLocalName(i,&attrname,&namelen);
|
---|
137 | // get the value of the current attribute
|
---|
138 | pAttributes->getValue(i,&attrvalue,&valuelen);
|
---|
139 | // if current attribute is the one needed return its value
|
---|
140 | if(name.compare(std::wstring(attrname,namelen)) == 0)
|
---|
141 | return std::wstring(attrvalue, valuelen);
|
---|
142 | }
|
---|
143 |
|
---|
144 | // attribute not found; return the default value
|
---|
145 | return defvalue;
|
---|
146 | }
|
---|
147 |
|
---|
148 | void LogParser::sendMessage(HWND hwnd) {
|
---|
149 | std::wcout << L" Sending " << msgType << L" to " << hwnd << "L - LPARAM: " << lParam << L" - WPARAM: " << wParam << std::endl;
|
---|
150 | PostMessage(hwnd, msgType, wParam, lParam);
|
---|
151 | if( useDefaultDelay ) {
|
---|
152 | Sleep(defaultMsgDelay);
|
---|
153 | } else {
|
---|
154 | Sleep(delay);
|
---|
155 | }
|
---|
156 | } |
---|