Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Copyright (c) 2011 Nicholas Okunew
   *  All rights reserved.
   *  
   *  This file is part of the com.atomicleopard.webelemental library
   *  
   *  The com.atomicleopard.webelemental library is free software: you 
   *  can redistribute it and/or modify it under the terms of the GNU
   *  Lesser General Public License as published by the Free Software Foundation, 
  *  either version 3 of the License, or (at your option) any later version.
  *  
  *  The com.atomicleopard.webelemental library is distributed in the hope
  *  that it will be useful, but WITHOUT ANY WARRANTY; without even
  *  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  *  See the GNU Lesser General Public License for more details.
  *  
  *  You should have received a copy of the GNU Lesser General Public License
  *  along with the com.atomicleopard.webelemental library.  If not, see 
  *  http://www.gnu.org/licenses/lgpl-3.0.html.
  */
 package com.atomicleopard.webelemental;
 
 import static com.atomicleopard.expressive.Expressive.*;
 
 import java.util.List;
 
 

Element wraps the WebElement of Selenium to provide a cleaner and easier to use interface.

The Element interface is designed to be similar to the actions available in the jQuery, leveraging both its familiarity to web developers and its simple but powerful interface.

An Element wraps zero or more WebElements unifying the interface with which we deal with elements within Selenium tests. The web elements contained within an Element are generally referred to as the selected, or matched, elements.

As a general rule, action methods (such as click(), val() etc) act on all selected elements, while query methods (such as text(), classes() act on the first selected element.

 
 public class Element implements Iterable<Element> {
 	private EList<WebElementelements;
 
 	public Element(WebElement element) {
 		this. = new EListImpl<WebElement>();
 		if (element != null) {
 			.add(element);
 		}
 	}
 
 	public Element(List<WebElementelements) {
 		this. = new EListImpl<WebElement>();
 		if (elements != null) {
 			this..addAll(elements);
 		}
 	}

Returns:
the id of the first selected element, null if no id exists, or null if no elements are selected
 
 	public String id() {
 		return attr("id");
 	}

Returns:
a list of the classes (as in CSS classes) on the first selected element, or an empty list if no elements are selected or no classes exist
 
 	public EList<Stringclasses() {
 		String classes = attr("class");
 		return classes == null ? Expressive.<Stringlist() : list(classes.split("\\s"));
 	}

Returns:
the value of the first selected element, null if no value exists, or null if no elements are selected
 
	public String val() {
		return attr("value");
	}

Returns the value of the named attribute on the first selected element.

Parameters:
name the name of the attribute to return
Returns:
the value of the named attribute on the first selected element, null if the attribute does not exist, or null if no elements are selected
	public String attr(String name) {
		return .isEmpty() ? null : .first().getAttribute(name);
	}

Sets the value of all selected elements to the given value

Parameters:
value the value to assign to all selected elements
Returns:
this Element, supporting a fluid API
	public Element val(CharSequence value) {
		for (WebElement element : ) {
			element.clear();
			element.sendKeys(value);
		}
		return this;
	}

Clicks all selected elements

Returns:
this Element, supporting a fluid API
	public Element click() {
		for (WebElement element : ) {
			element.click();
		}
		return this;
	}

Returns the html of the first selected element, or null if no elements are selected. This is usually synonymous with innerHtml.

Returns:
the html of the first selected element, or null if no elements are selected
See also:
org.openqa.selenium.WebElement.getText()
	public String html() {
		return .isEmpty() ? null : .first().getText();
	}

Returns the text of the first selected element, or null if no elements are selected.

Returns:
the text of the first selected element, or null if no elements are selected
See also:
org.openqa.selenium.WebElement.getText()
	public String text() {
		return .isEmpty() ? null : .first().getText();
	}

Sets the text of all selected elements to the given value

Parameters:
value the text value to write on all selected elements
Returns:
this Element, supporting a fluid API
	public Element text(CharSequence value) {
		for (WebElement element : ) {
			element.clear();
			element.sendKeys(value);
		}
		return this;
	}

Returns:
the number of selected elements
	public int size() {
		return .size();
	}

Returns:
true is there are no selected elements within this Element
	public boolean isEmpty() {
		return .isEmpty();
	}

Returns:
true if the first selected element is visible, false if no elements are selected or the first matching element is not visible
	public boolean isVisible() {
		return .isEmpty() ? false : .first().isDisplayed();
	}

Returns:
an Element containing the parent element of every selected element within this Element
	public Element parent() {
		for (WebElement element : ) {
			list.addAll(element.findElements(By.xpath("..")));
		}
		return new Element(list);
	}

Selects all elements matching the given CSS selector that are children of the selected elements within this Element.

Parameters:
selector specified as a CSS, or jQuery style, selector
Returns:
an Element containing all elements matching the given selector
	public Element find(String selector) {
		return find(By.cssSelector(selector));
	}

Selects all elements matching the given selector that are children of the selected elements within this Element.

Parameters:
selector
Returns:
an Element containing all elements matching the given selector
	public Element find(By selector) {
		for (WebElement webElement : ) {
			list.addAll(webElement.findElements(selector));
		}
		return new Element(list);
	}

Returns:
an java.util.Iterator over all selected Elements
	public Iterator<Elementiterator() {
		EList<Elementlist = new EListImpl<Element>();
		for (WebElement webElement : ) {
			list.add(new Element(webElement));
		}
		return list.iterator();
	}

Returns:
the first selected element, the returned Element will be empty if this Element has no selected elements
	public Element first() {
		return new Element(.first());
	}

Return the selected element at the given index. The returned Element will be empty if this Element has no selected element at the given index

Parameters:
index
Returns:
the selected element at the given index, returned Element will be empty if this Element has no selected element at the given index
	public Element get(int index) {
		return new Element(.at(index));
	}

Returns:
the last selected element, the returned Element will be empty if this Element has no selected elements
	public Element last() {
		return new Element(.last());
	}

Returns:
a random selected element from all the selected elements, the returned Element will be empty if this Element has no selected elements
	public Element any() {
		Collections.shuffle(duplicate);
		return new Element(duplicate.first());
	}

Performs the given transformation on all the selected elements within this Element.

This can be useful for extracting data from this Element, or performing a series of operations.
Example - return all the ids of the selected elements:

 
 EList<String> ids = element.each(new ETransformer<Element, String>() {
  public String to(Element from) {
   return from.id();
  }
 });
 
 
Example - set the value of all the selected elements to something unique:
 
 element.each(new ETransformer<Element, Void>() {
  private int unique = 1;
  public Void to(Element from) {
   from.val("Unique"+ (unique++));
   return null;
  }
 });
 
 
Example - advanced, extract data from a table:
 
 Element table = ...
 Element tableRows = table.find("tbody tr");
 EList<List<String>> tableContents = tableRows.each(new ETransformer<Element, List<String>>() {
  public List<String> to(Element from) {
   List<String> cellContent = from.find("td").each(ETransformers.<Element, String> toProperty("text", Element.class));
   return cellContent;
  }
 });
 
 
The com.atomicleopard.expressive.ETransformer interface enables powerful transformations, and is worth investigating. To help you write succinct code, many common operations are available statically using com.atomicleopard.expressive.transform.ETransformers

Parameters:
eTransformer the transformation to apply to all selected elements
Returns:
the result of applying the given transformation across each selected element
	public <T> EList<T> each(ETransformer<Element, T> eTransformer) {
		return new CollectionTransformer<Element, T>(eTransformer).to(this);
	}

Returns an ElementMatcher, which allows assertions and verifications to be applied to the set of selected elements using a fluid, declarative API.

Returns:
an ElementMatcher for all the selected elements
	public ElementMatcher verify() {
		return new ElementMatcher(this);
	}
	public String toString() {
		int size = .size();
		if (size > 1) {
			for (Element element : this) {
				sb.append(element);
				sb.append(", ");
			}
else if (size == 0) {
			sb.append("None");
else {
			sb.append("<");
			sb.append(String.format(" id='%s'"id()));
			sb.append(String.format(" class='%s'"attr("class")));
			sb.append("/>");
		}
		return sb.toString();
	}
New to GrepCode? Check out our FAQ X