# MIT License
#
# Copyright (c) 2015-2020 Iakiv Kramarenko
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import os

import pytest

from selene import have
from selene.core.exceptions import TimeoutException

start_page = 'file://' + os.path.abspath(os.path.dirname(__file__)) + '/../resources/start_page.html'


def test_have_url(session_browser):
    session_browser.open(start_page)
    session_browser.should(have.url(session_browser.driver.current_url))
    session_browser.should(have.no.url(session_browser.driver.current_url[:-1]))


def test_have_url_containing(session_browser):
    session_browser.open(start_page)
    session_browser.should(have.url_containing('start_page.html'))
    session_browser.should(have.no.url_containing('start_page.xhtml'))


def test_fails_on_timeout_during_waiting_for_exact_url(session_browser):
    browser = session_browser.with_(timeout=0.1)

    browser.open(start_page)

    with pytest.raises(TimeoutException):
        browser.should(have.url('xttp:/'))
        # TODO: check message too


def test_fails_on_timeout_during_waiting_for_part_of_url(session_browser):
    browser = session_browser.with_(timeout=0.1)

    browser.open(start_page)

    with pytest.raises(TimeoutException):
        browser.should(have.url_containing('xttp:/'))
        # TODO: check message too

[((35, 27, 35, 71), 'selene.have.url', 'have.url', ({(35, 36, 35, 70): 'session_browser.driver.current_url'}, {}), '(session_browser.driver.current_url)', False, 'from selene import have\n'), ((36, 27, 36, 79), 'selene.have.no.url', 'have.no.url', ({(36, 39, 36, 78): 'session_browser.driver.current_url[:-1]'}, {}), '(session_browser.driver.current_url[:-1])', False, 'from selene import have\n'), ((41, 27, 41, 65), 'selene.have.url_containing', 'have.url_containing', ({(41, 47, 41, 64): '"""start_page.html"""'}, {}), "('start_page.html')", False, 'from selene import have\n'), ((42, 27, 42, 69), 'selene.have.no.url_containing', 'have.no.url_containing', ({(42, 50, 42, 68): '"""start_page.xhtml"""'}, {}), "('start_page.xhtml')", False, 'from selene import have\n'), ((50, 9, 50, 40), 'pytest.raises', 'pytest.raises', ({(50, 23, 50, 39): 'TimeoutException'}, {}), '(TimeoutException)', False, 'import pytest\n'), ((60, 9, 60, 40), 'pytest.raises', 'pytest.raises', ({(60, 23, 60, 39): 'TimeoutException'}, {}), '(TimeoutException)', False, 'import pytest\n'), ((30, 41, 30, 66), 'os.path.dirname', 'os.path.dirname', ({(30, 57, 30, 65): '__file__'}, {}), '(__file__)', False, 'import os\n'), ((51, 23, 51, 41), 'selene.have.url', 'have.url', ({(51, 32, 51, 40): '"""xttp:/"""'}, {}), "('xttp:/')", False, 'from selene import have\n'), ((61, 23, 61, 52), 'selene.have.url_containing', 'have.url_containing', ({(61, 43, 61, 51): '"""xttp:/"""'}, {}), "('xttp:/')", False, 'from selene import have\n')]