[32] | 1 | #include "StdAfx.h"
|
---|
| 2 | #include "LogParser.h"
|
---|
| 3 |
|
---|
| 4 | #include <iostream>
|
---|
| 5 |
|
---|
| 6 | #include "WindowFinder.h"
|
---|
| 7 |
|
---|
[105] | 8 | LogParser::LogParser(_TCHAR* runCommand, unsigned int startupTime, TestResults * results, bool useDefaultDelay) : runCommand(runCommand), startupTime(startupTime), results(results), useDefaultDelay(useDefaultDelay)
|
---|
[32] | 9 | {
|
---|
| 10 |
|
---|
| 11 | }
|
---|
| 12 |
|
---|
| 13 | LogParser::~LogParser(void)
|
---|
| 14 | {
|
---|
| 15 |
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 |
|
---|
[154] | 19 |
|
---|
[32] | 20 | HRESULT STDMETHODCALLTYPE LogParser::startElement(
|
---|
| 21 | wchar_t __RPC_FAR *pwchNamespaceUri,
|
---|
| 22 | int cchNamespaceUri,
|
---|
| 23 | wchar_t __RPC_FAR *pwchLocalName,
|
---|
| 24 | int cchLocalName,
|
---|
| 25 | wchar_t __RPC_FAR *pwchRawName,
|
---|
| 26 | int cchRawName,
|
---|
| 27 | MSXML2::ISAXAttributes __RPC_FAR *pAttributes)
|
---|
| 28 | {
|
---|
| 29 | std::wstring localName(pwchLocalName);
|
---|
| 30 | if( localName.compare(L"session")==0 ) {
|
---|
[105] | 31 | sessionId = GetAttributeValue(pAttributes, L"id", L"");
|
---|
[104] | 32 | std::wcout << L"================================================" << std::endl;
|
---|
| 33 | std::wcout << L"starting session " << sessionId << std::endl;
|
---|
[105] | 34 | result.sessionPass = true;
|
---|
| 35 | result.errorMessage = L"";
|
---|
| 36 | result.msgNumber = 0;
|
---|
| 37 | currentMessage = 0;
|
---|
[104] | 38 | std::wcout << L"executing " << runCommand << std::endl;
|
---|
| 39 | PROCESS_INFORMATION pi;
|
---|
| 40 | STARTUPINFO si;
|
---|
| 41 | ZeroMemory(&pi, sizeof(pi));
|
---|
| 42 | ZeroMemory(&si, sizeof(si));
|
---|
[154] | 43 |
|
---|
| 44 | //divide runCommand into path to executable and parameters
|
---|
[159] | 45 | /*wchar_t *pdest = _tcschr(runCommand, ' ');
|
---|
[154] | 46 | int positionSpace = (int)(pdest - runCommand + 1);
|
---|
| 47 | _TCHAR *executable = new _TCHAR[512];
|
---|
| 48 | for(int i=0; i<positionSpace; i++) executable[i] = runCommand[i];
|
---|
[159] | 49 | executable[positionSpace] = '\0';*/
|
---|
[154] | 50 |
|
---|
[159] | 51 | //CreateProcess(executable, L" c:\\programme\\mahr\\marwin\\commands/pause.dll", NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
|
---|
| 52 | CreateProcess(NULL, runCommand, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
|
---|
[104] | 53 | hProcess = pi.hProcess;
|
---|
| 54 | CloseHandle(pi.hThread);
|
---|
[154] | 55 |
|
---|
[104] | 56 | std::wcout << L"waiting " << startupTime << L" ms for application under test to intialize" << std::endl;
|
---|
| 57 | Sleep(startupTime);
|
---|
| 58 | std::wcout << L"replay starting..." << std::endl;
|
---|
[32] | 59 | }
|
---|
| 60 | else if( localName.compare(L"msg")==0 ) {
|
---|
| 61 | std::wstring type = GetAttributeValue(pAttributes, L"type", L"");
|
---|
| 62 | msgType = _wtoi(type.c_str());
|
---|
| 63 | std::wstring lParamStr = GetAttributeValue(pAttributes, L"LPARAM", L"");
|
---|
| 64 | lParam = _wtoi(lParamStr.c_str());
|
---|
| 65 | std::wstring wParamStr = GetAttributeValue(pAttributes, L"WPARAM", L"");
|
---|
| 66 | wParam = _wtoi(wParamStr.c_str());
|
---|
| 67 | std::wstring delayStr = GetAttributeValue(pAttributes, L"delay", L"");
|
---|
| 68 | delay = _wtoi(delayStr.c_str());
|
---|
| 69 | currentWindow = NULL;
|
---|
| 70 | currentParent = NULL;
|
---|
| 71 | }
|
---|
| 72 | else if( localName.compare(L"window")==0 ) {
|
---|
| 73 | WindowData * winData = new WindowData;
|
---|
| 74 | winData->name = GetAttributeValue(pAttributes, L"name", L"");
|
---|
| 75 | winData->className = GetAttributeValue(pAttributes, L"class", L"");
|
---|
| 76 | std::wstring resourceIdStr = GetAttributeValue(pAttributes, L"resourceId", L"");
|
---|
| 77 | winData->resourceId = _wtoi(resourceIdStr.c_str());
|
---|
| 78 | std::wstring isModalStr = GetAttributeValue(pAttributes, L"isModal", L"");
|
---|
| 79 | if( isModalStr.compare(L"true")==0 ) {
|
---|
| 80 | winData->isModal = true;
|
---|
| 81 | } else {
|
---|
| 82 | winData->isModal = false;
|
---|
| 83 | }
|
---|
| 84 | winData->child = NULL;
|
---|
| 85 | if( currentWindow==NULL ) {
|
---|
| 86 | currentWindow = winData;
|
---|
| 87 | } else {
|
---|
| 88 | currentParent->child = winData;
|
---|
| 89 | }
|
---|
| 90 | currentParent = winData;
|
---|
[143] | 91 | }
|
---|
| 92 | else if( localName.compare(L"textEquals")==0 ) {
|
---|
| 93 | expectedValue = GetAttributeValue(pAttributes, L"expectedValue", L"");
|
---|
| 94 | }
|
---|
| 95 | else if( localName.compare(L"fileEquals")==0) {
|
---|
| 96 | actualFile = GetAttributeValue(pAttributes, L"actualFile", L"");
|
---|
| 97 | expectedFile = GetAttributeValue(pAttributes, L"expectedFile", L"");
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[32] | 100 | return S_OK;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 |
|
---|
| 104 | HRESULT STDMETHODCALLTYPE LogParser::endElement(
|
---|
| 105 | wchar_t __RPC_FAR *pwchNamespaceUri,
|
---|
| 106 | int cchNamespaceUri,
|
---|
| 107 | wchar_t __RPC_FAR *pwchLocalName,
|
---|
| 108 | int cchLocalName,
|
---|
| 109 | wchar_t __RPC_FAR *pwchRawName,
|
---|
| 110 | int cchRawName)
|
---|
| 111 | {
|
---|
| 112 | std::wstring localName(pwchLocalName);
|
---|
| 113 | if( localName.compare(L"session")==0 ) {
|
---|
[104] | 114 | std::wcout << L"session completed" << std::endl;
|
---|
[105] | 115 | results->addResult(sessionId, result);
|
---|
[104] | 116 | BOOL retVal = TerminateProcess(hProcess, 0);
|
---|
| 117 | if( retVal!=0 ) {
|
---|
| 118 | std::wcout << L"application terminated" << std::endl;
|
---|
| 119 | }
|
---|
| 120 | CloseHandle(hProcess);
|
---|
[32] | 121 | }
|
---|
| 122 | else if( localName.compare(L"msg")==0 ) {
|
---|
[105] | 123 | currentMessage++;
|
---|
[32] | 124 | WindowFinder finder;
|
---|
| 125 | HWND hwnd = finder.find(currentWindow);
|
---|
[105] | 126 | // check if window was found, if not test has failed
|
---|
| 127 | if( result.sessionPass ) {
|
---|
| 128 | result.sessionPass = false;
|
---|
| 129 | result.errorMessage = finder.getErrorMessage();
|
---|
| 130 | result.msgNumber = currentMessage;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[32] | 133 | sendMessage(hwnd);
|
---|
| 134 | deleteWindowData(currentWindow);
|
---|
| 135 | currentWindow = NULL;
|
---|
| 136 | }
|
---|
| 137 | else if( localName.compare(L"LPARAM")==0 ) {
|
---|
| 138 | WindowFinder finder;
|
---|
| 139 | HWND hwnd = finder.find(currentWindow);
|
---|
| 140 | lParam = (LPARAM) hwnd;
|
---|
| 141 | deleteWindowData(currentWindow);
|
---|
| 142 | currentWindow = NULL;
|
---|
| 143 | }
|
---|
| 144 | else if( localName.compare(L"WPARAM")==0 ) {
|
---|
| 145 | WindowFinder finder;
|
---|
| 146 | HWND hwnd = finder.find(currentWindow);
|
---|
| 147 | wParam = (WPARAM) hwnd;
|
---|
| 148 | deleteWindowData(currentWindow);
|
---|
| 149 | currentWindow = NULL;
|
---|
| 150 | }
|
---|
[143] | 151 | else if( localName.compare(L"textEquals")==0 ) {
|
---|
| 152 | WindowFinder finder;
|
---|
| 153 | HWND hwnd = finder.find(currentWindow);
|
---|
| 154 |
|
---|
| 155 | wchar_t* wstr = new wchar_t[256];
|
---|
| 156 | wstr[0] = '\0';
|
---|
| 157 |
|
---|
| 158 | //GetWindowText is not working properly for EditControls in other applications -> send WM_GETTEXT instead
|
---|
| 159 | SendMessage(hwnd, WM_GETTEXT, 255, (LPARAM)wstr);
|
---|
| 160 | std::wstring windowText = wstr;
|
---|
| 161 |
|
---|
| 162 | if(expectedValue == windowText) std::wcout << std::endl << L"textEquals passed (expected value: " << expectedValue.c_str() << ")" << std::endl << std::endl;
|
---|
| 163 | else std::wcout << std::endl << L"textEquals failed (expected value: " << expectedValue.c_str() << ", windowText: " << windowText << ")" << std::endl << std::endl;
|
---|
| 164 | deleteWindowData(currentWindow);
|
---|
| 165 | currentWindow = NULL;
|
---|
| 166 | }
|
---|
| 167 | else if( localName.compare(L"fileEquals")==0) {
|
---|
| 168 | std::ifstream f1(actualFile.c_str(), std::ios_base::in | std::ios_base::binary);
|
---|
| 169 | if(f1 == NULL) {
|
---|
| 170 |
|
---|
| 171 | std::wcout << std::endl << L"fileEquals failed because " << actualFile << " is not available!"<< std::endl << std::endl;
|
---|
| 172 | return S_OK;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | std::ifstream f2(expectedFile.c_str(), std::ios_base::in | std::ios_base::binary);
|
---|
| 176 | if(f2 == NULL) {
|
---|
| 177 | std::wcout << std::endl << L"fileEquals failed because " << expectedFile << " is not available!" << std::endl << std::endl;
|
---|
| 178 | return S_OK;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | f1.seekg(0, std::ios_base::end);
|
---|
| 182 | f2.seekg(0, std::ios_base::end);
|
---|
| 183 | std::streamsize length1 = f1.tellg();
|
---|
| 184 | std::streamsize length2 = f2.tellg();
|
---|
| 185 |
|
---|
| 186 | bool passed = true;
|
---|
| 187 |
|
---|
| 188 | if (length1 != length2) { // Non equal length -> files differ
|
---|
| 189 | passed = false;
|
---|
| 190 | }
|
---|
| 191 | else {
|
---|
| 192 | f1.seekg(0);
|
---|
| 193 | f2.seekg(0);
|
---|
| 194 |
|
---|
| 195 | const std::streamsize blocksize = 4096;
|
---|
| 196 | char block1[blocksize], block2[blocksize];
|
---|
| 197 |
|
---|
| 198 | for (std::streamsize counter=length1; counter > 0; counter -= blocksize)
|
---|
| 199 | {
|
---|
| 200 | f1.read(block1, blocksize);
|
---|
| 201 | f2.read(block2, blocksize);
|
---|
| 202 |
|
---|
| 203 | if(memcmp(block1, block2, blocksize) != 0) { //block are non equal -> files differ
|
---|
| 204 | passed = false;
|
---|
| 205 | break;
|
---|
| 206 | }
|
---|
| 207 | }
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | if(passed) {
|
---|
| 211 | std::wcout << std::endl << L"FileEquals passed" << std::endl << std::endl;
|
---|
| 212 | }
|
---|
| 213 | else {
|
---|
| 214 | std::wcout << std::endl << L"fileEquals failed (expectedFile: " << expectedFile.c_str() << ", actualFile: " << actualFile << ")" << std::endl << std::endl;
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | f1.close();
|
---|
| 218 | f2.close();
|
---|
| 219 | }
|
---|
| 220 |
|
---|
[32] | 221 | return S_OK;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | std::wstring LogParser::GetAttributeValue(MSXML2::ISAXAttributes __RPC_FAR *pAttributes,
|
---|
| 225 | std::wstring name, std::wstring defvalue)
|
---|
| 226 | {
|
---|
| 227 | // get the number of attributes
|
---|
| 228 | int length = 0;
|
---|
| 229 | pAttributes->getLength(&length);
|
---|
| 230 |
|
---|
| 231 | // enumerate over all attributes
|
---|
| 232 | for ( int i=0; i<length; i++ )
|
---|
| 233 | {
|
---|
| 234 | wchar_t *attrname = NULL, * attrvalue = NULL;
|
---|
| 235 | int namelen = 0, valuelen = 0;
|
---|
| 236 |
|
---|
| 237 | // get the local name of the current attribute
|
---|
| 238 | pAttributes->getLocalName(i,&attrname,&namelen);
|
---|
| 239 | // get the value of the current attribute
|
---|
| 240 | pAttributes->getValue(i,&attrvalue,&valuelen);
|
---|
| 241 | // if current attribute is the one needed return its value
|
---|
| 242 | if(name.compare(std::wstring(attrname,namelen)) == 0)
|
---|
| 243 | return std::wstring(attrvalue, valuelen);
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | // attribute not found; return the default value
|
---|
| 247 | return defvalue;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | void LogParser::sendMessage(HWND hwnd) {
|
---|
[104] | 251 | std::wcout << L" Sending " << msgType << L" to " << hwnd << "L - LPARAM: " << lParam << L" - WPARAM: " << wParam << std::endl;
|
---|
[32] | 252 | PostMessage(hwnd, msgType, wParam, lParam);
|
---|
[104] | 253 | if( useDefaultDelay ) {
|
---|
| 254 | Sleep(defaultMsgDelay);
|
---|
| 255 | } else {
|
---|
| 256 | Sleep(delay);
|
---|
| 257 | }
|
---|
[32] | 258 | } |
---|