testing/web-platform/tests/webdriver/tests/is_element_selected/selected.py
author Henrik Skupin <mail@hskupin.info>
Tue, 24 Apr 2018 18:57:48 +0200
changeset 792546 91ef9aae32eb1e07de7c9dd7d8f51de0080e5ccc
parent 792391 a41eba4d76259bd54fec7d9b420beb4173dd9660
child 792381 8d2b5884a137b007f565d2134033eae377c8b437
permissions -rw-r--r--
Bug 1455568 - [wdspec] Use shared command function for each WebDriver command. Tests for a specific command have to use a shared function for each of the included tests of a module to avoid redefinition of the same code. In a follow-up patch those shared methods could even be separated out as fixtures for each of the sub folders. MozReview-Commit-ID: 11lAp7J8IoZ

# META: timeout=long

from tests.support.asserts import assert_error, assert_dialog_handled, assert_success
from tests.support.inline import inline
from tests.support.fixtures import create_dialog


alert_doc = inline("<script>window.alert()</script>")
check_doc = inline("<input id=checked type=checkbox checked/><input id=notChecked type=checkbox/>")
option_doc = inline("""<select>
                        <option id=notSelected>r-</option>
                        <option id=selected selected>r+</option>
                       </select>
                    """)


def is_element_selected(session, element_id):
    return session.transport.send(
        "GET", "session/{session_id}/element/{element_id}/selected".format(
            session_id=session.session_id,
            element_id=element_id))


def test_no_browsing_context(session, create_window):
    # 13.1 step 1
    session.window_handle = create_window()
    session.close()

    result = is_element_selected(session, "foo")
    assert_error(result, "no such window")


def test_handle_prompt_dismiss(new_session, add_browser_capabilites):
    # 13.1 step 2
    _, session = new_session({"capabilities": {"alwaysMatch": add_browser_capabilites({"unhandledPromptBehavior": "dismiss"})}})
    session.url = inline("<input id=foo>")
    element = session.find.css("#foo", all=False)

    create_dialog(session)("alert", text="dismiss #1", result_var="dismiss1")

    result = is_element_selected(session, element.id)
    assert_success(result, False)
    assert_dialog_handled(session, "dismiss #1")

    create_dialog(session)("confirm", text="dismiss #2", result_var="dismiss2")

    result = is_element_selected(session, element.id)
    assert_success(result, False)
    assert_dialog_handled(session, "dismiss #2")

    create_dialog(session)("prompt", text="dismiss #3", result_var="dismiss3")

    result = is_element_selected(session, element.id)
    assert_success(result, False)
    assert_dialog_handled(session, "dismiss #3")


def test_handle_prompt_accept(new_session, add_browser_capabilites):
    # 13.1 step 2
    _, session = new_session({"capabilities": {"alwaysMatch": add_browser_capabilites({"unhandledPromptBehavior": "accept"})}})
    session.url = inline("<input id=foo>")
    element = session.find.css("#foo", all=False)

    create_dialog(session)("alert", text="dismiss #1", result_var="dismiss1")

    result = is_element_selected(session, element.id)
    assert_success(result, False)
    assert_dialog_handled(session, "dismiss #1")

    create_dialog(session)("confirm", text="dismiss #2", result_var="dismiss2")

    result = is_element_selected(session, element.id)
    assert_success(result, False)
    assert_dialog_handled(session, "dismiss #2")

    create_dialog(session)("prompt", text="dismiss #3", result_var="dismiss3")

    result = is_element_selected(session, element.id)
    assert_success(result, False)
    assert_dialog_handled(session, "dismiss #3")


def test_handle_prompt_missing_value(session):
    # 13.1 step 2
    session.url = inline("<input id=foo>")
    element = session.find.css("#foo", all=False)

    create_dialog(session)("alert", text="dismiss #1", result_var="dismiss1")

    result = is_element_selected(session, element.id)
    assert_error(result, "unexpected alert open")
    assert_dialog_handled(session, "dismiss #1")

    create_dialog(session)("confirm", text="dismiss #2", result_var="dismiss2")

    result = is_element_selected(session, element.id)
    assert_error(result, "unexpected alert open")
    assert_dialog_handled(session, "dismiss #2")

    create_dialog(session)("prompt", text="dismiss #3", result_var="dismiss3")

    result = is_element_selected(session, element.id)
    assert_error(result, "unexpected alert open")
    assert_dialog_handled(session, "dismiss #3")


def test_element_stale(session):
    # 13.1 step 4
    session.url = check_doc
    element = session.find.css("#checked", all=False)
    session.refresh()

    result = is_element_selected(session, element.id)
    assert_error(result, "stale element reference")


def test_element_checked(session):
    # 13.1 step 5
    session.url = check_doc
    element = session.find.css("#checked", all=False)

    result = is_element_selected(session, element.id)
    assert_success(result, True)


def test_checkbox_not_selected(session):
    # 13.1 step 5
    session.url = check_doc
    element = session.find.css("#notChecked", all=False)

    result = is_element_selected(session, element.id)
    assert_success(result, False)


def test_element_selected(session):
    # 13.1 step 5
    session.url = option_doc
    element = session.find.css("#selected", all=False)

    result = is_element_selected(session, element.id)
    assert_success(result, True)


def test_element_not_selected(session):
    # 13.1 step 5
    session.url = option_doc
    element = session.find.css("#notSelected", all=False)

    result = is_element_selected(session, element.id)
    assert_success(result, False)