@@ -94,7 +94,8 @@ Automation::Automation(const Logger& logger)
9494 : logger_(logger),
9595 build_no_ (0 ),
9696 isLoading(false ),
97- sessionId(0 )
97+ sessionId(0 ),
98+ jslogger()
9899{
99100}
100101
@@ -239,7 +240,13 @@ void Automation::Init(const BrowserOptions& options, int* build_no, Error** erro
239240 if (options.command .HasSwitch (switches::kStartMaximized ))
240241 pStartView->showMaximized ();
241242
242- if (SessionManager::GetInstance ()->is_wi_enabled ())
243+ // JS Console log redefinition
244+ connect (pStartView->page ()->mainFrame (), SIGNAL (javaScriptWindowObjectCleared ()), this , SLOT (loadJSLogObject ()));
245+ connect (pStartView, SIGNAL (loadFinished (bool )),this , SLOT (loadConsoleJS ()), Qt::QueuedConnection);
246+ loadJSLogObject (pStartView->page ()->mainFrame ());
247+ loadConsoleJS (pStartView);
248+
249+ if (SessionManager::GetInstance ()->is_wi_enabled ())
243250 {
244251 pStartView->page ()->setProperty (" _q_webInspectorServerPort" , SessionManager::GetInstance ()->get_wi_port ());
245252 }
@@ -2210,6 +2217,37 @@ void Automation::SetAlertPromptText(const WebViewId& view_id, const std::string
22102217 }
22112218}
22122219
2220+ void Automation::GetBrowserLog (base::ListValue **log)
2221+ {
2222+ *log = jslogger.getLog ();
2223+ }
2224+
2225+ void Automation::ResetBrowserCurrentView (const WebViewId& old_view_id, const WebViewId& new_view_id, Error **error)
2226+ {
2227+ if (checkView (old_view_id))
2228+ {
2229+ QWebView *view = qobject_cast<QWebView*>(old_view_id.GetView ());
2230+ disconnect (view->page ()->mainFrame (), SIGNAL (javaScriptWindowObjectCleared ()), this , SLOT (loadJSLogObject ()));
2231+ }
2232+
2233+ if (!checkView (new_view_id))
2234+ {
2235+ *error = new Error (kNoSuchWindow );
2236+ return ;
2237+ }
2238+
2239+ QWebView *view = qobject_cast<QWebView*>(new_view_id.GetView ());
2240+
2241+ if (view)
2242+ {
2243+ // JS Console log redefinition
2244+ connect (view->page ()->mainFrame (), SIGNAL (javaScriptWindowObjectCleared ()), this , SLOT (loadJSLogObject ()));
2245+ connect (view, SIGNAL (loadFinished (bool )),this , SLOT (loadConsoleJS ()), Qt::QueuedConnection);
2246+ loadJSLogObject (view->page ()->mainFrame ());
2247+ loadConsoleJS (view);
2248+ }
2249+ }
2250+
22132251QWebFrame* Automation::FindFrameByPath (QWebFrame* parent, const FramePath &frame_path)
22142252{
22152253 if (frame_path.value ().empty ())
@@ -2347,10 +2385,54 @@ void Automation::pageLoadStarted()
23472385}
23482386
23492387void Automation::pageLoadFinished ()
2350- {
2388+ {
23512389 isLoading = false ;
23522390}
23532391
2392+ void Automation::loadJSLogObject ()
2393+ {
2394+ QWebFrame *mainFrame = qobject_cast<QWebFrame*>(sender ());
2395+ if (mainFrame)
2396+ loadJSLogObject (mainFrame);
2397+ }
2398+
2399+ void Automation::loadJSLogObject (QWebFrame *frame)
2400+ {
2401+ frame->addToJavaScriptWindowObject (" wdconsole" , &jslogger);
2402+ }
2403+
2404+ void Automation::loadConsoleJS ()
2405+ {
2406+ QWebView* view = qobject_cast<QWebView*>(sender ());
2407+ if (view)
2408+ loadConsoleJS (view);
2409+ }
2410+
2411+ void Automation::loadConsoleJS (const QWebView *view)
2412+ {
2413+ QString jscript (" if ( _log == null ){"
2414+ " var _log = console.log,"
2415+ " _warn = console.warn,"
2416+ " _error = console.error;"
2417+ " };"
2418+
2419+ " console.log = function() {"
2420+ " wdconsole.log(arguments[0]);"
2421+ " return _log.apply(console, arguments);"
2422+ " };"
2423+
2424+ " console.warn = function() {"
2425+ " wdconsole.warn(arguments[0]);"
2426+ " return _warn.apply(console, arguments);"
2427+ " };"
2428+
2429+ " console.error = function() {"
2430+ " wdconsole.error(arguments[0]);"
2431+ " return _error.apply(console, arguments);"
2432+ " };" );
2433+ view->page ()->mainFrame ()->evaluateJavaScript (jscript);
2434+ }
2435+
23542436JSNotifier::JSNotifier ():
23552437 isCompleted (false )
23562438{
@@ -2373,6 +2455,33 @@ void JSNotifier::setResult(QVariant result)
23732455 emit completed ();
23742456}
23752457
2458+ JSLogger::JSLogger ()
2459+ {
2460+ browserLogger.AddHandler (&browserLog);
2461+ }
2462+
2463+ base::ListValue* JSLogger::getLog ()
2464+ {
2465+ base::ListValue* retVal = browserLog.entries_list ()->DeepCopy ();
2466+ browserLog.clear_entries_list ();
2467+ return retVal;
2468+ }
2469+
2470+ void JSLogger::log (QVariant message)
2471+ {
2472+ browserLogger.Log (kInfoLogLevel , message.toString ().toStdString ());
2473+ }
2474+
2475+ void JSLogger::warn (QVariant message)
2476+ {
2477+ browserLogger.Log (kWarningLogLevel , message.toString ().toStdString ());
2478+ }
2479+
2480+ void JSLogger::error (QVariant message)
2481+ {
2482+ browserLogger.Log (kSevereLogLevel , message.toString ().toStdString ());
2483+ }
2484+
23762485// bool Automation::eventFilter(QObject *obj, QEvent *event)
23772486// {
23782487// if (obj == pWeb) {
0 commit comments