Example #1
0
int PhpWebView::click2(QWebElement elem, bool samewnd)
{
	if (elem.isNull())
		return 0;

	if (samewnd)
		elem.removeAttribute("target");

	QEventLoop loop;
	isNewViewCreated = false;
	isNewViewBegin = false;
	
	QString js = "var e = document.createEvent('MouseEvents');";
	//js += "e.initEvent( 'click', true, true );";
    js += "e.initMouseEvent( 'click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);";
    js += "this.dispatchEvent(e);";
	elem.evaluateJavaScript(js);
	
	QTimer::singleShot(1000, &loop, SLOT(quit()));
	loop.exec();
	
	while (browser && isNewViewBegin && !isNewViewCreated)
		loop.processEvents();

	return 1;
}
Example #2
0
void FormExtractor::submit()
{
    QWebFrame *frame = ui.webView->page()->mainFrame();

    QWebElement firstName = frame->findFirstElement("#firstname");
    QWebElement lastName = frame->findFirstElement("#lastname");
    QWebElement maleGender = frame->findFirstElement("#genderMale");
    QWebElement femaleGender = frame->findFirstElement("#genderFemale");
    QWebElement updates = frame->findFirstElement("#updates");

    ui.firstNameEdit->setText(firstName.evaluateJavaScript("this.value").toString());
    ui.lastNameEdit->setText(lastName.evaluateJavaScript("this.value").toString());

    if (maleGender.evaluateJavaScript("this.checked").toBool())
        ui.genderEdit->setText(maleGender.evaluateJavaScript("this.value").toString());
    else if (femaleGender.evaluateJavaScript("this.checked").toBool())
        ui.genderEdit->setText(femaleGender.evaluateJavaScript("this.value").toString());

    if (updates.evaluateJavaScript("this.checked").toBool())
        ui.updatesEdit->setText("Yes");
    else
        ui.updatesEdit->setText("No");

    // In mobile devices, change the tab when the data has been submitted
    #if defined Q_OS_SYMBIAN || defined Q_WS_HILDON || defined Q_WS_MAEMO_5 || defined Q_WS_SIMULATOR
        ui.tabWidget->setCurrentWidget(ui.tabData);
    #endif
}
Example #3
0
    foreach (QWebElement selectField, selectFields) {
        QString name = selectField.attribute(QLatin1String("name"));
        int selectedIndex = selectField.evaluateJavaScript(QLatin1String("this.selectedIndex")).toInt();
        if (selectedIndex == -1)
            continue;

        QWebElementCollection options = selectField.findAll(QLatin1String("option"));
        QString value = options.at(selectedIndex).toPlainText();
        searchUrl.addQueryItem(name, value);
    }
Example #4
0
void Browser::loadFinished(bool ok) {
    QUrl current_url(page->currentFrame()->baseUrl());

    // events trottling
    if (current_url == this->last_url)
    {
        qDebug() << this << QString("%1, loadFinished trottled.").arg(QDateTime::currentDateTime().toTime_t());
        return;
    }

    this->last_url = current_url;

    qDebug() << this << QString("%1, '%2', %3 ms.").arg(QDateTime::currentDateTime().toTime_t()).arg(current_url.toString()).arg(page_load_time.elapsed());

    if (!ok) {
        emit(errorHappened(page_load_time.elapsed(), page->currentFrame()->baseUrl()));
        restartTest(QString("its not ok, something wrong happened! (possibly 500) URL: %1").arg(page->currentFrame()->baseUrl().toString()));
        return;
     }

    QWebElement document= page->currentFrame()->documentElement();

    if (page->currentFrame()->baseUrl().host() != base_url.host())
    {
       restartTest("We've got outside the test site. Restarting.");
       return;
    }


    QWebElementCollection collection = document.findAll("a[href]");
    QWebElement link; QString link_href;

    if (collection.count() == 0)
    {
        restartTest("No links found on page. Restarting.");
        return;
    }

    // Ignoring javascript and empty links
    do
    {
       link = collection.at(qrand()%collection.count());
       link_href = link.attribute("href");
    }
    while ((link_href.count() == 0) || (link_href.at(0) == '#') || (link_href.contains("javascript:;")));

    emit(pageLoaded(page_load_time.elapsed(), page->currentFrame()->baseUrl()));

    link.evaluateJavaScript("var evObj = document.createEvent('MouseEvents');evObj.initEvent( 'click', true, true );this.dispatchEvent(evObj);");

    page_load_time.start();
    timeout_countdown->start();
}
Example #5
0
QList<QWebElement> PhpWebView::getAllElementsByXPath2(const QString & xpath, QWebElement node)
{
	QString js = "var result = document.evaluate('"+xpath+"', this, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null); ";
	js += "WebElementBridge.clear(); ";
	js += "var node = result.iterateNext(); ";
    js += "while (node) { ";
    js += " 	WebElementBridge.passElement(node); ";
    js += " 	node = result.iterateNext(); ";
    js += "} ";
	node.evaluateJavaScript(js);

	QList<QWebElement> coll = webridge.list;
	return coll;
}
Example #6
0
static bool isEditableElement(QWebPage* page)
{
    const QWebFrame* frame = (page ? page->currentFrame() : 0);
    QWebElement element = (frame ? frame->findFirstElement(QL1S(":focus")) : QWebElement());
    if (!element.isNull()) {
        const QString tagName(element.tagName());
        if (tagName.compare(QL1S("textarea"), Qt::CaseInsensitive) == 0) {
            return true;
        }
        const QString type(element.attribute(QL1S("type")).toLower());
        if (tagName.compare(QL1S("input"), Qt::CaseInsensitive) == 0
            && (type.isEmpty() || type == QL1S("text") || type == QL1S("password"))) {
            return true;
        }
        if (element.evaluateJavaScript("this.isContentEditable").toBool()) {
            return true;
        }
    }
    return false;
}
Example #7
0
bool qPBReaderDocView::GetWordAt(QPoint iScreenPt,
                                 QString & osSelectedWord,
                                 QRect & oScreenRect)
{
   TRSCOPE(view, "qPBReaderDocView::GetWordAt");

   osSelectedWord.clear();
   oScreenRect = QRect();

   QWebHitTestResult hit = _pDoc->mainFrame()->hitTestContent(iScreenPt);
   QWebElement e = hit.enclosingBlockElement();
   TRACE << ENC(e.localName()) << endl;
   QString js = "qPBReaderFindClickedWord(this, " + QString::number(
                   iScreenPt.x()) + "," + QString::number(iScreenPt.y()) + ")";
   TRACE << ENC(js) << endl;

   QList<QVariant> lv = e.evaluateJavaScript(js).toList();
   bool b = lv.size() == 5;
   TRACE << "Javascript word search = " << b << endl;

   if (b)
   {
      osSelectedWord = lv[0].toString();
      oScreenRect = QRect(QPoint(lv[1].toInt(), lv[2].toInt()), QPoint(lv[3].toInt(),
                          lv[4].toInt()));
      TRACE << ENC(osSelectedWord)
            << " rect (" << oScreenRect.left() << ", "
            << oScreenRect.top() << ", "
            << oScreenRect.right() << ", "
            << oScreenRect.bottom() << ")" << endl;
   }

   TRACE << TRANAME << " ended with " << b << endl;

   return b;
}
Example #8
0
void Phantom::setFormInputFile(QWebElement el, const QString &fileTag)
{
    m_page.m_nextFileTag = fileTag;
    el.evaluateJavaScript(JS_MOUSEEVENT_CLICK_WEBELEMENT);
}
Example #9
0
int PhpWebView::click(const QString & xpath, bool samewnd)
{
	QWebElement elem = getElementByXPath(QString(xpath));
	
	if (elem.isNull())
		return 0;

	elem.setFocus();

	if (samewnd)
		elem.removeAttribute("target");

	QString js = "var node = this; var x = node.offsetLeft; var y = node.offsetTop; ";
	js += "var w = node.offsetWidth/2; ";
	js += "var h = node.offsetHeight/2; ";
	js += "while (node.offsetParent != null) { ";
	js += "   node = node.offsetParent; ";
	js += "   x += node.offsetLeft; ";
	js += "   y += node.offsetTop; ";
	js += "} ";
	js += "[x+w, y+h]; ";
	QList<QVariant> vlist = elem.evaluateJavaScript(js).toList();
	QPoint point;
	point.setX(vlist.at(0).toInt());
	point.setY(vlist.at(1).toInt());

	QRect elGeom = elem.geometry();
	QPoint elPoint = elGeom.center();
	int elX = point.x(); //elPoint.x();
	int elY = point.y(); //elPoint.y();
	int webWidth = width();
	int webHeight = height();
	int pixelsToScrollRight=0;
	int pixelsToScrollDown=0;
	if (elX > webWidth)
		pixelsToScrollRight = elX-webWidth+elGeom.width()/2+50; //the +10 part if for the page to scroll a bit further
	if (elY > webHeight)
		pixelsToScrollDown = elY-webHeight+elGeom.height()/2+50; //the +10 part if for the page to scroll a bit further
	
	/*pixelsToScrollRight = elX-elGeom.width()/2-50;
	pixelsToScrollDown = elY-elGeom.height()/2-50;

	if (pixelsToScrollRight < 0)
		pixelsToScrollRight = 0;
	if (pixelsToScrollRight > page()->mainFrame()->scrollBarMaximum(Qt::Horizontal))
		pixelsToScrollRight = page()->mainFrame()->scrollBarMaximum(Qt::Horizontal);
	if (pixelsToScrollDown < 0)
		pixelsToScrollDown = 0;
	if (pixelsToScrollDown > page()->mainFrame()->scrollBarMaximum(Qt::Vertical))
		pixelsToScrollDown = page()->mainFrame()->scrollBarMaximum(Qt::Vertical);*/

	int oldHoriz = page()->mainFrame()->scrollBarValue(Qt::Horizontal);
	int oldVert = page()->mainFrame()->scrollBarValue(Qt::Vertical);

	page()->mainFrame()->setScrollBarValue(Qt::Horizontal, pixelsToScrollRight);
	page()->mainFrame()->setScrollBarValue(Qt::Vertical, pixelsToScrollDown);
	QPoint pointToClick(elX-pixelsToScrollRight, elY-pixelsToScrollDown);

	QEventLoop loop;
	isNewViewCreated = false;
	isNewViewBegin = false;
	
	//QMouseEvent *pressEvent = new QMouseEvent(QMouseEvent::MouseButtonPress, pointToClick, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
	//QApplication::postEvent(browser, pressEvent);
	//QApplication::processEvents();

	//QMouseEvent releaseEvent(QMouseEvent::MouseButtonRelease,pointToClick,Qt::LeftButton,Qt::LeftButton,Qt::NoModifier);
	//QApplication::sendEvent(browser, &releaseEvent);

	QString js2 = "var e = document.createEvent('MouseEvents');";
    //js2 += "e.initEvent( 'click', true, true );";
	//js2 += "e.initMouseEvent('click', true, true, window, 0, 0, 0, "+QString::number(elX)+", "+QString::number(elY)+", false, false, false, false, 0, null);";
	js2 += "e.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);";
    js2 += "this.dispatchEvent(e);";
	elem.evaluateJavaScript(js2);
	
	QTimer::singleShot(1000, &loop, SLOT(quit()));
	loop.exec();
	
	while (browser && isNewViewBegin && !isNewViewCreated)
		loop.processEvents();

	//page()->mainFrame()->setScrollBarValue(Qt::Horizontal, oldHoriz);
	//page()->mainFrame()->setScrollBarValue(Qt::Vertical, oldVert);
	return 1;
}