| <!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>example_test.html</title> |
| <script src="../test/test_bootstrap.js"></script> |
| <script> |
| goog.require('bot.ErrorCode'); |
| goog.require('goog.string'); |
| goog.require('goog.userAgent'); |
| goog.require('goog.userAgent.product'); |
| goog.require('webdriver.Builder'); |
| goog.require('webdriver.testing.Window'); |
| goog.require('webdriver.testing.jsunit'); |
| </script> |
| <script> |
| var IOS = goog.userAgent.product.IPAD || goog.userAgent.product.IOS; |
| |
| var testWindow; |
| |
| function tearDown() { |
| if (testWindow) { |
| testWindow.close(); |
| testWindow = null; |
| } |
| } |
| |
| // If this doesn't pass, none of the WebDriverJS tests will! |
| function testCanAcquireDriverInstanceForCurrentBrowser() { |
| new webdriver.Builder().build(); // Yup, this is all we need to do. |
| } |
| |
| function testCanHandleErrorsReturnedByTheServer() { |
| var called = 'neither'; |
| var driver = new webdriver.Builder().build(); |
| driver.findElement(By.id('not-there')). |
| then(function() { |
| called = 'callback'; |
| fail('Attempt to find element should have failed'); |
| }, function(e) { |
| called = 'errback'; |
| assertEquals('Wrong error code', bot.ErrorCode.NO_SUCH_ELEMENT, |
| e.code); |
| }); |
| |
| driver.call(function() { |
| assertEquals('Wrong callback function called', 'errback', called); |
| }); |
| } |
| |
| function testCanType() { |
| var driver = new webdriver.Builder().build(); |
| |
| if (!IOS) { |
| webdriver.testing.Window.focusOnWindow(driver, window); |
| } |
| |
| var inputElement = driver.findElement(By.tagName('input')); |
| driver.executeScript('arguments[0].value = "";', inputElement); |
| inputElement.sendKeys('foo'); |
| assertThat(inputElement.getAttribute('value'), equals('foo')); |
| } |
| |
| if (!IOS) { |
| var testShouldBeAbleToOpenAPage = function() { |
| var driver = new webdriver.Builder().build(); |
| |
| // Switch to our test window before navigating or we'll lose the page |
| // running this script. Using an iframe won't work. WebDriver's |
| // navigation commands always apply to window.top! |
| testWindow = webdriver.testing.Window.create(driver, null, 2000); |
| |
| driver.get(window.frames[0].location.href); |
| driver.wait(function() { |
| return driver.getTitle().then(function(title) { |
| return title === 'ajaxy_page'; |
| }); |
| }, 5000, 'Timed out waiting for new page to load.'); |
| }; |
| } |
| |
| function testShouldBeAbleToSwitchFrames() { |
| var driver = new webdriver.Builder().build(); |
| |
| // Make sure this window is selected. |
| if (!IOS) { |
| webdriver.testing.Window.focusOnWindow(driver, window); |
| } |
| |
| driver.switchTo().frame(0); |
| assertThat(driver.executeScript('return document.title'), |
| equals('ajaxy_page')); |
| } |
| |
| function testShouldBeAbleToLocateDomElements() { |
| var div = document.createElement('div'); |
| div.innerHTML = 'hello world'; |
| document.body.appendChild(div); |
| |
| var driver = new webdriver.Builder().build(); |
| |
| // Make sure this window is selected. |
| if (!IOS) { |
| webdriver.testing.Window.focusOnWindow(driver, window); |
| } |
| assertThat(driver.findElement(div).getInnerHtml(), equals('hello world')); |
| } |
| </script> |
| <iframe src="ajaxy_page.html" width="100%" height="300px"></iframe> |
| <label>Text input: <input type="text"/></label> |