| <!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>find_child_element_test</title> |
| <script src="../test/test_bootstrap.js"></script> |
| <script> |
| goog.require('bot.locators'); |
| goog.require('webdriver.Builder'); |
| goog.require('webdriver.testing.asserts'); |
| goog.require('webdriver.testing.jsunit'); |
| </script> |
| <script> |
| |
| var driver; |
| |
| function setUp() { |
| driver = new webdriver.Builder().build(); |
| } |
| |
| function testCanFindElementByClassName() { |
| var body = driver.findElement(By.tagName('body')); |
| var el = body.findElement(By.className('root')); |
| assertThat(el.getAttribute('id'), equals('test-dom')); |
| } |
| |
| function testCanFindElementById() { |
| var body = driver.findElement(By.tagName('body')); |
| var el = body.findElement(By.id('test-dom')); |
| assertThat(el.getAttribute('className'), equals('root')); |
| } |
| |
| function testCanFindElementByXPath() { |
| var root = driver.findElement(By.xpath('//div[@class="root"]')); |
| var el = root.findElement(By.xpath('./div[2]')); |
| assertThat(el.getInnerHtml(), equals('child 2')); |
| } |
| |
| function testCanFindMultipleTiersOfElementsByXpath() { |
| var child3 = driver.findElement(By.xpath('//div//div[3]')); |
| var nested = child3.findElement(By.xpath('.//div[@class="nested-div"][1]')); |
| assertThat(nested.getInnerHtml(), equals('descendant 3')); |
| } |
| |
| function testCanFindElementByCssSelector() { |
| var root = driver.findElement(By.css('div.root')); |
| var child = root.findElement(By.css('div[id="child-2"]')); |
| assertThat(child.getInnerHtml(), equals('child 2')); |
| } |
| </script> |
| <div id="test-dom" class="root"> |
| <div id="child-1"> |
| <div> |
| <div class="nested-div">descendant 1</div> |
| <div class="nested-div">descendant 2</div> |
| </div> |
| </div> |
| <div id="child-2">child 2</div> |
| <div id="child-3"> |
| <div> |
| <div class="nested-div">descendant 3</div> |
| <div class="nested-div">descendant 4</div> |
| </div> |
| </div> |
| </div> |