接上篇文章,本章节主要讲解Webbrowser 如何模拟用户上传文件
(8)模拟上传文件
首先模拟用户点击上传按钮,调用ClickButton方法:
nRet = ClickButton(_T("load_file('/fileWtWindow.jsp','文件上传测试页面')"), _T("input"), _T("onclick"), _T("myframe"));
在URL加载完成消息中判断加载完成的URL,由于Webbrowser 与UI主线程公用同一条线程,因此判断完之后可以通过定时器调用浏览按钮:
BOOL OnUrlCompleteEvent(void* pParam)
{
std::wstring strURL = (wchar_t*)pParam;
DUI__Trace(_T("URL Finish:%s"), strURL.c_str());
//点击上传代理委托书
if (strURL.find(_T("/fileWtWindow.jsp")) != std::string::npos)
{
//启用文件上传接口
SetTimer(m_hWnd, 0x107, 5000,NULL);
}
else if (strURL.find(_T("/fileUpload_returnFileResult.xhtml")) != std::string::npos)
{
SetTimer(m_hWnd, 0x11E, 5000, NULL);
}
return TRUE;
}
定时器:
else if (wParam == 0x107)
{
KillTimer(m_hWnd, 0x107);
UploadFile( _T("fileWt"), _T("file_submit"), _T("G:\\trademark\\测试文件.jpg"), _T("myframe"), UPLOAD_FILE_TYPE_AGENT);
}
上传文件实现操作:先获取到上传界面的iFrame元素节点(下面代码的ifr_popup0为iFrame元素iD),然后获取对应的浏览按钮,模拟选择文件后,点击上传操作
xny::Integer UploadFile(LPCTSTR lpEditName, LPCTSTR lpFunName, LPCTSTR lpFilePath, LPCTSTR lpParentName, UPLOAD_FILE_TYPE type)
{
xny::Integer nRet = nsny::eProbeErr::NYERR_FAIL;
do
{
//先点击上传按钮
//通过模拟点击来设置
CComPtrpElement = NULL;
CComPtr pIFrame = (IHTMLIFrameElement*)m_pWebBrowserEx->GetElementByName(_T("ifr_popup0"), lpParentName, IID_IHTMLIFrameElement);
if (pIFrame)
{
pElement = (IHTMLInputFileElement *)m_pWebBrowserEx->GetElementByName(lpEditName, pIFrame, IID_IHTMLInputFileElement);
}
if (pElement == 0) break;
//然后设置路径
m_strPath = lpFilePath;
m_upLoadFileType = type;
//启动定时器,检测窗口调动
SetTimer(m_hWnd, 0x200, 2000, NULL);
VARIANT params[10];
VARIANT ret;
CWebBrowserEx::InvokeMethod(pElement, _T("click"), &ret, params, 0);
DUI__Trace(_T("调用上传文件接口"));
//相应上传操作
ClickButton(_T("laodBut"), NULL, NULL, lpParentName, _T("ifr_popup0"));
//
} while (0);
return nRet;
}
浏览文件窗口检测代码:
//查找对应的文件浏览界面
else if (wParam == 0x200)
{
KillTimer(m_hWnd, 0x200);
//
HWND hWnd = FindWindow(L"#32770", L"选择要加载的文件");
if (hWnd != NULL)
{
HWND hEdit = hWnd;
EnumChildWindows(hWnd, EnumEditProc, (LPARAM)m_strPath.c_str());
}
}
//模拟文件上传操作
BOOL CALLBACK EnumEditProc(
HWND hWnd, // handle to child window
LPARAM lParam // application-defined value
)
{
TCHAR szClassName[256] = { 0 };
std::wstring strText = (LPCTSTR)lParam;
do
{
GetClassName(hWnd, szClassName, 256);
std::wstring strClassName = szClassName;
if (strClassName == L"Edit")
{
::SetWindowText(hWnd, strText.c_str());
LPTSTR lpszProgress = new TCHAR[strText.size() + 1];
_tcscpy_s(lpszProgress, strText.size() + 1, strText.c_str());
HRESULT hRet = ::SendMessage(hWnd, WM_SETTEXT, NULL, (LPARAM)lpszProgress);
if (hRet == CB_ERRSPACE)
{
DUI__Trace(_T("SetText Faild"));
}
delete[] lpszProgress;
lpszProgress = NULL;
}
else if (strClassName == L"Button")
{
TCHAR szWndText[256] = { 0 };
GetWindowText(hWnd, szWndText, 256);
std::wstring strWndText = szWndText;
if (strWndText.find(L"打开") != -1 || strWndText.find(L"Open") != -1)
{
HRESULT hRet = ::SendMessage(hWnd, WM_LBUTTONDOWN, NULL, MAKELPARAM(50, 17));
hRet = ::SendMessage(hWnd, WM_LBUTTONUP, NULL, MAKELPARAM(50, 17));
if (hRet == 0)
{
DUI__Trace(_T("发送消息成功"));
}
}
}
} while (0);
return true;
}
(9)时间控件(未完待续)