<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<!--
Copyright 2004 ThoughtWorks, Inc

 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.
-->
<html>
  <head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>BrowserBot tests</title>
    <link rel="stylesheet" type="text/css" href="../../jsunit/css/jsUnitStyle.css">
<script language="JavaScript" type="text/javascript" src="../../jsunit/app/jsUnitCore.js"></script>
<script language="JavaScript" type="text/javascript" src="../jsmock/mock.js"></script>
<script language="JavaScript" type="text/javascript" src="../dummy-logging.js"></script>


<script language="JavaScript" type="text/javascript" src="../../core/lib/prototype.js"></script>
<script language="JavaScript" type="text/javascript" src="../../core/scripts/selenium-browserdetect.js"></script>
<script language="JavaScript" type="text/javascript" src="../../core/scripts/htmlutils.js"></script>
<script language="JavaScript" type="text/javascript" src="../../core/scripts/selenium-executionloop.js"></script>
<script language="JavaScript" type="text/javascript" src="../../core/scripts/selenium-api.js"></script>
<script language="JavaScript" type="text/javascript">
function setUp() {
    mockPageBot = mockBrowserBot = new Mock();

    selenium = new Selenium(mockBrowserBot);
}

function verifyMocks() {
    mockBrowserBot.verify();
    mockPageBot.verify();
}

// Tests for Element actions
function testClickElementWithoutCallback() {
    mockPageBot.expects("findElement", "id").returns("elementToClick");
    mockPageBot.expects("clickElement", "elementToClick");

    selenium.doClick("id");
    verifyMocks();
}

function testType() {
    mockPageBot.expects("findElement", "id").returns("elementToType");
    mockPageBot.expects("replaceText", "elementToType", "new text");

    selenium.doType("id", "new text");
    verifyMocks();
}

function testSelect() {
    var mockOptionLocatorFactory = new Mock();
    selenium.optionLocatorFactory = mockOptionLocatorFactory;
    var mockSelect = new Mock();
    // The doSelect() method checks the options property exists as a way
    // of ensuring that the element is a Select element.  Hence the following expectation.
    mockSelect.expectsProperty("options").returns("some options");
    mockPageBot.expects("findElement", "id").returns(mockSelect);
    var mockOptionLocator = new Mock();
    mockOptionLocatorFactory.expects("fromLocatorString", "Option One").returns(mockOptionLocator);
    var option = new Object();
    mockOptionLocator.expects("findOption", mockSelect).returns(option);
    mockPageBot.expects("selectOption", mockSelect, option);

    selenium.doSelect("id", "Option One");
    mockOptionLocatorFactory.verify();
    mockOptionLocator.verify();
    mockSelect.verify();
    verifyMocks();
}

// Browser actions
function testOpen() {
    mockBrowserBot.expects("openLocation", "new/location");

    selenium.doOpen("new/location");
    verifyMocks();
}

function testSelectWindow() {
    mockBrowserBot.expects("selectWindow", "windowName");

    selenium.doSelectWindow("windowName");
    verifyMocks();
}

function testGetLocation() {
    var mockWindow = new Object();
    mockWindow.location = "http://page/path?foo=bar";
    mockPageBot.getCurrentWindow = function() {
        return mockWindow;
    }

    assertTrue(selenium.getLocation().indexOf("path") > 0);
    assertTrue(selenium.getLocation().indexOf("page/path") > 0);
    assertTrue(selenium.getLocation().indexOf("http://page/path?foo=bar") > -1);

}

function testGetTitleReturnsTheTitle() {
    mockPageBot.expects("getTitle").returns("foo");
    assertEquals("foo", selenium.getTitle());
    verifyMocks();
}

function testGetValueOfText() {
    var mockTextControl = Object();
    mockTextControl.type = "TEXT";
    mockTextControl.value = "the value";
    mockPageBot.expects("findElement", "id").returns(mockTextControl);

    assertEquals("the value", selenium.getValue("id"));
    verifyMocks();
}

function testGetValueOfCheckbox() {
    var mockControl = Object();
    mockControl.type = "CHECKBOX";
    mockControl.value = "the value";
    mockControl.checked = true;
    mockPageBot.expects("findElement", "id").returns(mockControl);

    assertEquals("on", selenium.getValue("id"));
    verifyMocks();
}

function testGetText() {
    var element = document.getElementById("testGetText")
    mockPageBot.expects("findElement", "id").returns(element);
    assertEquals("foo", selenium.getText("id"));
    verifyMocks();
}

function textCell(val) {
   var cell = new Object();
   cell.textContent = val;
   return cell;
}

function testGetTableSuccess() {
    mockPageBot.expects("findElement", "table").returns(getMockTable());
    assertEquals("buz", selenium.getTable("table.1.1"));
    verifyMocks();
}

function testGetTableInvalidLocator() {
    assertCallErrors("VerifyTable should have failed for invalid locator",
                    function() {selenium.getTable("foo");},
                    "Invalid target format. Correct format is tableName.rowNum.columnNum");
    verifyMocks();
}

function testGetTableNoSuchRow() {
    var mockTable = getMockTable();
    mockPageBot.expects("findElement", "table").returns(mockTable);
    assertCallFails("VerifyTable should have failed for no such row",
                    function() {selenium.getTable("table.11.0", "bar");},
                    "Cannot access row 11 - table has 2 rows");
    verifyMocks();
}

function testGetTableNoSuchColumn() {
    var mockTable = getMockTable();
    mockPageBot.expects("findElement", "table").returns(mockTable);
    assertCallFails("VerifyTable should have failed for no such column",
                    function() {selenium.getTable("table.0.11", "bar");},
                    "Cannot access column 11 - table row has 2 columns");
    verifyMocks();
}

function getMockTable() {
    return document.getElementById("test_table")
}

function testTextPresent() {
    mockPageBot.expects("bodyText").returns("this is some foo text");
    mockPageBot.expects("bodyText").returns("this is some foo text");
    assertTrue(selenium.isTextPresent("foo"));
    assertFalse(selenium.isTextPresent("bar"));
    verifyMocks();
}

function testElementPresent() {
    mockPageBot.expects("findElementOrNull", "id").returns("foo");
    assertTrue(selenium.isElementPresent("id"));
    verifyMocks();
}

function testElementNotPresent() {
    mockPageBot.expects("findElementOrNull", "id").returns(null);
    assertFalse(selenium.isElementPresent("id"));
    verifyMocks();
}

function testGetAllButtonsShouldCallPageBot() {
    mockPageBot.expects("getAllButtons").returns("foo");
    selenium.getAllButtons();
    verifyMocks();
}

function testGetAllFieldsShouldCallPageBot() {
    mockPageBot.expects("getAllFields").returns("foo");
    selenium.getAllFields();
    verifyMocks();
}

function testGetAllLinksShouldCallPageBot() {
    mockPageBot.expects("getAllLinks").returns("foo");
    selenium.getAllLinks();
    verifyMocks();
}

function testShouldFailIfTryToGetAlertWhenThereAreNone() {
    mockBrowserBot.expects("hasAlerts").returns(false);

    assertCallFails("getAlert should have failed",
                    function() {selenium.getAlert(); },
                    "There were no alerts");

    verifyMocks();
}

function testGetAlertReturnsNextAlert() {
    mockBrowserBot.expects("hasAlerts").returns(true);
    mockBrowserBot.expects("getNextAlert").returns("The real alert");

    assertEquals("The real alert", selenium.getAlert());

    verifyMocks();
}

function testShouldFailIfTryToVerifyConfirmationWhenThereAreNone() {
    mockBrowserBot.expects("hasConfirmations").returns(false);

    assertCallFails("verifyConfirmation should have failed",
                    function() {selenium.getConfirmation();},
                    "There were no confirmations");

    verifyMocks();
}

function testGetConfirmationReturnsNextConfirmation() {
      mockBrowserBot.expects("hasConfirmations").returns(true);
      mockBrowserBot.expects("getNextConfirmation").returns("The real confirmation");

     assertEquals("The real confirmation", selenium.getConfirmation());

     verifyMocks();
}

function testShouldTellBroswerBotIfAskedToCancelNextConfirmation() {
     mockBrowserBot.expects("cancelNextConfirmation", false);
     selenium.doChooseCancelOnNextConfirmation();
     verifyMocks();
}

function testIsSelectedSuccess() {
    var mockTextControl = Object();
    mockTextControl.selectedIndex = 1;

    mockTextControl.options = [{text: "option0"},{text: "option1", selected:true},{text: "option2"}];
    mockPageBot.expects("findElement", "id=option1").returns(mockTextControl);
    assertTrue(selenium.isSomethingSelected("id=option1"));
    verifyMocks();

    mockPageBot.expects("findElement", "id=option2").returns({});
    try {
        selenium.isSomethingSelected("id=option2");
        fail();
    }
    catch (e)
    {
        // pass
    }
    verifyMocks();

}

function testGetSelectOptions() {
    var mockTextControl = Object();
    mockTextControl.options = [{text: "option0"},{text: "option1"},{text: "option2"}];
    mockPageBot.expects("findElement", "id").returns(mockTextControl);

    assertObjectEquals(["option0","option1","option2"], selenium.getSelectOptions("id"));
    verifyMocks();
}

function testGetSelectOptionsWithCommasEscaped() {
    var mockTextControl = Object();
    mockTextControl.options = [{text: "option,0"},{text: "option.1"}];
    mockPageBot.expects("findElement", "id").returns(mockTextControl);

    assertObjectEquals(["option,0","option.1"], selenium.getSelectOptions("id"));
    verifyMocks();
}

function testGetAttributeWithId() {
    mockPageBot.expects("findAttribute", "id@attribute").returns("foo");

    assertEquals("foo", selenium.getAttribute("id@attribute"));
    verifyMocks();
}

function assertCallFails(message, theCall, expectedFailureMessage) {
    try {
        theCall();
    } catch (e) {
        if (!e.isAssertionFailedError) {
            throw e;
        }
        if (expectedFailureMessage) {
            assertEquals(expectedFailureMessage, e.failureMessage);
        }
        return;
    }
    fail(message);
}
function assertCallErrors(message, theCall, expectedFailureMessage) {
    try {
        theCall();
    } catch (e) {
        if (expectedFailureMessage) {
            assertEquals(expectedFailureMessage, e.message);
        }
        return;
    }
    fail(message);
}

function testEnsureNoUnhandledPopupsThrowsExceptionIfAlertPresent() {

     mockBrowserBot.expects("hasAlerts").returns(true);
     mockBrowserBot.expects("getNextAlert").returns("The Alert");

     try {
        selenium.ensureNoUnhandledPopups();
        fail("exception expected");
     } catch (e) {
        assertTrue(e.isSeleniumError);
        assertEquals("There was an unexpected Alert! [The Alert]", e.message);
     }

     mockBrowserBot.verify();
}

function testShouldFailActionCommandsIfConfirmPresent() {

     mockBrowserBot.expects("hasAlerts").returns(false);
     mockBrowserBot.expects("hasConfirmations").returns(true);
     mockBrowserBot.expects("getNextConfirmation").returns("The Confirmation");

     try {
        selenium.ensureNoUnhandledPopups();
        fail("exception expected");
     } catch (e) {
        assertTrue(e.isSeleniumError);
        assertEquals("There was an unexpected Confirmation! [The Confirmation]", e.message);
     }

     mockBrowserBot.verify();
}
</script>
  </head>
  <body>Selenium API Tests
  <span id="testGetText"> foo </span>

  <table id="test_table">
    <tr><td>foo</td><td>bar</td></tr>
    <tr><td>fuz</td><td>buz</td></tr>
  </table>
  </body>
</html>