blob: d80ab0dc6c5f434cda1e829d4913498fecb22e02 [file] [log] [blame]
<!DOCTYPE html>
<!--
Copyright 2011 Software Freedom Conservancy. 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.
-->
<title>webdriver_finddomelement_test.html</title>
<script src="test_bootstrap.js"></script>
<script>
goog.require('bot');
goog.require('bot.inject');
goog.require('goog.dom');
goog.require('goog.userAgent');
goog.require('goog.userAgent.product');
goog.require('goog.userAgent.product.isVersion');
goog.require('webdriver.WebDriver');
goog.require('webdriver.Session');
goog.require('webdriver.test.JsExecutor');
goog.require('webdriver.test.testutil');
goog.require('webdriver.testing.jsunit');
</script>
<body>
<script>
var DRIVER = new webdriver.WebDriver(
new webdriver.Session('test_session_id', {}),
new webdriver.test.JsExecutor);
var bugsBunnyElement, iframeElement, iframeDoc, daffyDuckElement;
function setUpPage() {
bugsBunnyElement = document.createElement('div');
bugsBunnyElement.innerHTML = 'bunny';
document.body.appendChild(bugsBunnyElement);
iframeElement = document.createElement('iframe');
iframeElement.src = 'javascript:void(0);';
document.body.appendChild(iframeElement);
iframeDoc = goog.dom.getFrameContentDocument(iframeElement);
iframeDoc.open();
iframeDoc.write('<body><div>duck</div></body>');
iframeDoc.close();
}
function setUp() {
// In IE, we need to initialize the JScript engine in our frame before
// continuing with the test.
if (goog.userAgent.IE) {
goog.dom.getFrameContentWindow(iframeElement).
execScript('1', 'JavaScript');
}
daffyDuckElement = iframeDoc.body.firstChild;
document[bot.inject.cache.CACHE_KEY_] = null;
iframeDoc[bot.inject.cache.CACHE_KEY_] = null;
bot.setWindow(window);
}
function testCanFindDomElementInCurrentDocument() {
var webElement = DRIVER.findElement(bugsBunnyElement);
assertThat(webElement.getInnerHtml(), equals('bunny'));
assertThat(DRIVER.isElementPresent(bugsBunnyElement), equals(true));
}
function testCanFindElementsNotAttachedToTheDom() {
var orphanNode = document.createElement('div');
assertEquals('sanity check', document, orphanNode.ownerDocument);
assertThat(DRIVER.isElementPresent(orphanNode), equals(true));
DRIVER.findElement(orphanNode).
getInnerHtml().
then(goog.partial(fail, 'Should not be able to use an orphaned node'),
function(e) {
assertEquals('Element should be considered stale!',
bot.ErrorCode.STALE_ELEMENT_REFERENCE, e.code);
});
}
function testGracefullyFailsToFindElementsBelongingToADifferentFrame() {
DRIVER.findElement(daffyDuckElement).
then(goog.partial(fail, 'Should have failed to find the element'),
function(e) {
assertEquals('Should report element not found',
bot.ErrorCode.NO_SUCH_ELEMENT, e.code);
});
assertThat(DRIVER.isElementPresent(daffyDuckElement), equals(false));
}
function testCanFindFramedElementsWhenFrameIsSelected() {
// Test fails in Safari4; bot.inject.executeScript does not properly
// recompile the script for the selected window.
if (goog.userAgent.product.SAFARI &&
goog.userAgent.product.isVersion(4)) {
return;
}
bot.setWindow(goog.dom.getFrameContentWindow(iframeElement));
var webElement = DRIVER.findElement(daffyDuckElement);
assertThat(webElement.getInnerHtml(), equals('duck'));
assertThat(DRIVER.isElementPresent(daffyDuckElement), equals(true));
}
</script>
</body>