PDA

View Full Version : QWebView vs. JavaScript controls



Lucky Text
10th February 2015, 18:21
Hello!
I've got a desktop application. The QWebView is a part of it, and it's necessary to automate activity on the page, that contains JavaScript functions. These functions create dialogs and message boxes. I'd like to force my app fill in data on these fields and push message boxes' buttons.
Could you please give an advice, how to manage this task?

anda_skoa
10th February 2015, 19:18
Retrieve the view's web page, then the main frame of the page.

QWebFrame allows you to evaluate JavaScript in the context of the web content.
http://doc.qt.io/qt-5/qwebframe.html#evaluateJavaScript

Cheers,
_

Lucky Text
11th February 2015, 04:42
Retrieve the view's web page, then the main frame of the page.

QWebFrame allows you to evaluate JavaScript in the context of the web content.
http://doc.qt.io/qt-5/qwebframe.html#evaluateJavaScript

Cheers,
_

Thanks a lot!
Is it possible to close window.alert(), that was executed by another function, to fill window.prompt() with the application's text. Worst of all I have no idea how to find out are these modal controls active or not.

anda_skoa
11th February 2015, 09:06
You'll have to ask on a web programming forum.

Cheers,
_

Lucky Text
17th February 2015, 13:56
You'll have to ask on a web programming forum.

Cheers,
_

Thanks.
Actually, I found the Qt solution. These QWebView JS windows effects the same way as QMessageBox alerts - they're modal and blocks the whole GUI. The solution is not complicated.
QWebView->page() returns a QWebPage class. If you want to suspend these windows, you have to create derived class (MyWebPage for example) and redefine virtual functions.


class MyWebPage : public QWebPage
{
Q_OBJECT

public:
QStatusBar *sb;
QWebView *wv;

MyWebPage(QWebView *wvptr, QStatusBar *sbptr) : QWebPage(0)
{
sb = sbptr;
wv = wvptr;
}
~MyWebPage()
{
sb = NULL;
wv = NULL;
}

virtual void javaScriptAlert(QWebFrame *frame, const QString &msg)
{
if(wv->url().toString().contains("http://site.you.need"))
sb->showMessage(tr("SITE.YOU.NEED sent an alert: '%1', it was supressed.").arg(msg), 5000);
else
QWebPage::javaScriptAlert(frame, msg);
}

virtual bool javaScriptConfirm(QWebFrame *frame, const QString &msg)
{
if(wv->url().toString().contains("http://site.you.need"))
{
sb->showMessage(tr("SITE.YOU.NEED activated javaScriptConfirm: '%1'. TRUE sended.").arg(msg), 5000);
return true;
}
else
return QWebPage::javaScriptConfirm(frame, msg);
}

virtual bool javaScriptPrompt(QWebFrame * frame, const QString & msg, const QString & defaultValue, QString * result)
{
if(wv->url().toString().contains("http://SITE.YOU.NEED"))
{
sb->showMessage(tr("SITE.YOU.NEED activated javaScriptPrompt: Title '%1', default value is '%2'. Sending '%3'.").arg(msg).arg(defaultValue).arg(QGuiApplication:: clipboard()->text()), 5000);
result->clear();
result->append(QGuiApplication::clipboard()->text()); // some text from a clipboard

return true;
}
else
return QWebPage::javaScriptPrompt(frame, msg, defaultValue, result);
}
};

.... in a program ...

MyWebPage *page = new MyWebPage(ui->webView, ui->statusBar);
ui->webView->setPage(MyWebPage);