blob: 5c54f10960f640b80a6410e1a677b940a39660ea [file] [log] [blame]
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
part of webdriver.core;
class WebElement extends _WebDriverBase implements SearchContext {
final String id;
/// The context from which this element was found.
final SearchContext context;
/// How the element was located from the context.
final dynamic /* String | Finder */ locator;
/// The index of this element in the set of element founds. If the method
/// used to find this element always returns one element, then this is null.
final int index;
WebElement(driver, this.id, [this.context, this.locator, this.index])
: super(driver, 'element/$id');
/// Click on this element.
Future click() async {
await _post('click');
}
/// Submit this element if it is part of a form.
Future submit() async {
await _post('submit');
}
/// Send [keysToSend] to this element.
Future sendKeys(String keysToSend) async {
await _post('value', {
'value': [keysToSend]
});
}
/// Clear the content of a text element.
Future clear() async {
await _post('clear');
}
/// Is this radio button/checkbox selected?
Future<bool> get selected => _get<bool>('selected');
/// Is this form element enabled?
Future<bool> get enabled => _get<bool>('enabled');
/// Is this element visible in the page?
Future<bool> get displayed => _get<bool>('displayed');
/// The location within the document of this element.
Future<Point> get location async {
var point = await _get('location');
return new Point<int>(point['x'].toInt(), point['y'].toInt());
}
/// The size of this element.
Future<Rectangle<int>> get size async {
var size = await _get('size');
return new Rectangle<int>(
0, 0, size['width'].toInt(), size['height'].toInt());
}
/// The tag name for this element.
Future<String> get name => _get<String>('name');
/// Visible text within this element.
Future<String> get text => _get<String>('text');
///Find an element nested within this element.
///
/// Throws [NoSuchElementException] if matching element is not found.
@override
Future<WebElement> findElement(By by) async {
var element = await _post('element', by);
return new WebElement(driver, element[_element], this, by);
}
/// Find multiple elements nested within this element.
@override
Stream<WebElement> findElements(By by) async* {
var elements = await _post('elements', by);
int i = 0;
for (var element in elements) {
yield new WebElement(driver, element[_element], this, by, i);
i++;
}
}
/// Access to the HTML attributes of this tag.
///
/// TODO(DrMarcII): consider special handling of boolean attributes.
Attributes get attributes => new Attributes._(driver, '$_prefix/attribute');
/// Access to the cssProperties of this element.
///
/// TODO(DrMarcII): consider special handling of color and possibly other
/// properties.
Attributes get cssProperties => new Attributes._(driver, '$_prefix/css');
/// Does this element represent the same element as another element?
/// Not the same as ==
Future<bool> equals(WebElement other) => _get<bool>('equals/${other.id}');
Map<String, String> toJson() => {_element: id};
@override
int get hashCode => driver.hashCode * 3 + id.hashCode;
@override
bool operator ==(other) =>
other is WebElement && other.driver == this.driver && other.id == this.id;
@override
String toString() {
var out = new StringBuffer()..write(context);
if (locator is By) {
if (index == null) {
out..write('.findElement(');
} else {
out..write('.findElements(');
}
out..write(locator)..write(')');
} else {
out..write('.')..write(locator);
}
if (index != null) {
out..write('[')..write(index)..write(']');
}
return out.toString();
}
}