from urllib.parse import urljoin

import pytest
import requests
from selenium.webdriver.common.keys import Keys

from tests.selenium_tests.conftest import skip_selenium_tests


@skip_selenium_tests
@pytest.mark.parametrize("file_format", ['id_formats_1', 'id_formats_2',
                                         'id_formats_3', 'id_formats_4',
                                         'id_formats_5'])
def test_select_country(base_url, login, file_format, selenium, reload_until_condition):
    selenium.get('{0}/'.format(base_url))

    # go to country menu
    country_page = selenium.find_element_by_link_text('➽ Existing excerpt / country')
    country_page.click()

    # select monaco
    country = selenium.find_element_by_xpath("//option[contains(.,'Monaco')]")
    country.click()

    # choose the file format
    formats = selenium.find_element_by_id(file_format)
    formats.click()

    # submit
    create = selenium.find_element_by_name('submit')
    create.send_keys(Keys.RETURN)

    # wait until the download link appears
    selenium.find_element_by_xpath("/html/body/div/div/div[2]/div[2]/div[1]/div/div[1]/h3")
    first_a = "/html/body/div/div/div[2]/div[2]/div[1]/div/div[2]/div[1]/div[1]/div[2]/div/div[1]/p/a"
    element = reload_until_condition(selenium.find_element_by_xpath, first_a)

    # check if the download link is a valid link
    url = urljoin(base_url, element.get_attribute('href'))
    r = requests.head(url)
    assert r.status_code == requests.codes.ok

@@ -167,16 +167,57 @@
 um_tests
+, first_panel_on_excerpts_export_overview
 %0A%0A%0A@skip
@@ -1113,124 +1113,120 @@
 ath(
-%22/html/body/div/div/div%5B2%5D/div%5B2%5D/div%5B1%5D/div/div%5B1%5D/h3%22)%0A    first_a = %22/html/body/div/div/div%5B2%5D/div%5B2%5D/div%5B1%5D/div/
+first_panel_on_excerpts_export_overview + %22div%5B1%5D/h3%22)%0A    first_a = first_panel_on_excerpts_export_overview + %22
 div%5B

use constant to see more clearly what the xpath is all about