All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.crawljax.condition.XPathCondition Maven / Gradle / Ivy

Go to download

Crawling Ajax applications through dynamic analysis and reconstruction of the UI state changes. Crawljax is based on a method which dynamically builds a `state-flow graph' modeling the various navigation paths and states within an Ajax application.

The newest version!
package com.crawljax.condition;

import java.io.IOException;

import javax.xml.xpath.XPathExpressionException;

import net.jcip.annotations.Immutable;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.crawljax.browser.EmbeddedBrowser;
import com.crawljax.util.Helper;
import com.crawljax.util.XPathHelper;

/**
 * A condition which returns true if the XPath expression returns one or more elements. NOTE:
 * element names must be in upper case and attributes in lower case.
 * 
 * @author [email protected] (Danny Roest)
 * @version $Id$
 */
@Immutable
public class XPathCondition extends AbstractCondition {

	private final String expression;

	/**
	 * Construct xpath condition.
	 * 
	 * @param expression
	 *            The actual xpath expression.
	 */
	public XPathCondition(String expression) {
		this.expression = expression;
	}

	@Override
	public boolean check(EmbeddedBrowser browser) {
		return checkXPathExpression(browser);
	}

	private boolean checkXPathExpression(EmbeddedBrowser browser) {

		try {
			Document document = Helper.getDocument(browser.getDom());
			NodeList nodeList = XPathHelper.evaluateXpathExpression(document, expression);
			// TODO IF this can be removed, the ThreadLocal store can be removed and increasing
			// speed!
			this.setAffectedNodes(nodeList);
			return nodeList.getLength() > 0;
		} catch (XPathExpressionException e) {
			// Exception is catched, check failed so return false;
			return false;
		} catch (SAXException e) {
			// Exception is catched, check failed so return false;
			return false;
		} catch (IOException e) {
			// Exception is catched, check failed so return false;
			return false;
		}

	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy