import pytest
from web_test_base import *

class TestIATIPreview(WebTestBase):
    requests_to_load = {
        'IATI Preview': {
            'url': 'http://preview.iatistandard.org/'
        }
    }

    def test_contains_links(self, loaded_request):
        """
        Test that each page contains links to the defined URLs.
        """
        result = utility.get_links_from_page(loaded_request)

        assert "http://www.iatistandard.org/" in result

    @pytest.mark.parametrize("target_request", ["IATI Preview"])
    def test_xml_web_address_form_presence(self, target_request):
        """
        Test that there is a form to enter a URL of a valid XML file on the Preview Tool.
        """
        req = self.loaded_request_from_test_name(target_request)
        form_xpath = '//*[@id="main"]/div/div/div[1]/div/form'
        form_action_xpath = '//*[@id="main"]/div/div/div[1]/div/form/@action'
        form_method_xpath = '//*[@id="main"]/div/div/div[1]/div/form/@method'
        input_xpath = '//*[@id="url"]'
        button_xpath = '//*[@id="main"]/div/div/div[1]/div/form/div/div/span/button'

        forms = utility.locate_xpath_result(req, form_xpath)
        form_action = utility.locate_xpath_result(req, form_action_xpath)
        form_method = utility.locate_xpath_result(req, form_method_xpath)
        form_inputs = utility.locate_xpath_result(req, input_xpath)
        form_buttons = utility.locate_xpath_result(req, button_xpath)

        assert len(forms) == 1
        assert form_action == ['index.php']
        assert form_method == ['get']
        assert len(form_inputs) == 1
        assert len(form_buttons) == 1

@@ -187,22 +187,452 @@
       %7D%0A
+        , 'Valid (Example) XML Input': %7B%0A            'url': 'http://preview.iatistandard.org/index.php?url=https%253A%252F%252Fraw.githubusercontent.com%252FIATI%252FIATI-Extra-Documentation%252Fversion-2.01%252Fen%252Factivity-standard%252Factivity-standard-example-annotated.xml'%0A        %7D%0A        , 'Non-XML Input': %7B%0A            'url': 'http://preview.iatistandard.org/index.php?url=http%253A%252F%252Fpreview.iatistandard.org%252Findex.php'%0A        %7D%0A
     %7D%0A
-
 %0A    def
@@ -964,12 +964,8 @@
 est_
-xml_
 web_
@@ -2022,32 +2022,32 @@
 rm_inputs) == 1%0A
-
         assert l
@@ -2060,16 +2060,1404 @@
 m_buttons) == 1%0A
+%0A    @pytest.mark.parametrize(%22target_request%22, %5B%22Valid (Example) XML Input%22%5D)%0A    def test_example_xml_detection(self, target_request):%0A        %22%22%22%0A        Test that valid XML is detected and parsed.%0A        %22%22%22%0A        req = self.loaded_request_from_test_name(target_request)%0A        activity_count_xpath = '//*%5B@id=%22main%22%5D/div/div/div%5B1%5D/div'%0A        policy_marker_2_xpath = '//*%5B@id=%22actinfo-AA-AAA-123456789-ABC123-0%22%5D/li%5B26%5D/div/span%5B2%5D'%0A%0A        activity_count_text = utility.get_text_from_xpath(req, activity_count_xpath)%0A        policy_marker_2_text = utility.get_text_from_xpath(req, policy_marker_2_xpath)%0A%0A        assert utility.substring_in_list('This file has 1 activity', activity_count_text)%0A        assert utility.substring_in_list(' %5Bvocabulary=%221%22%5Cxa0code=%221%22%5Cxa0significance=%224%22%5Cxa0%5D', policy_marker_2_text)%0A%0A    @pytest.mark.parametrize(%22target_request%22, %5B%22Non-XML Input%22%5D)%0A    def test_non_xml_detection(self, target_request):%0A        %22%22%22%0A        Test that a URL that is not XML is detected as such.%0A        %22%22%22%0A        req = self.loaded_request_from_test_name(target_request)%0A        failed_detection_xpath = '//*%5B@id=%22main%22%5D/div/div/div%5B1%5D'%0A%0A        failed_detection_text = utility.get_text_from_xpath(req, failed_detection_xpath)%0A%0A        assert utility.substring_in_list('Sorry, could not get IATI compliant data from the supplied file!', failed_detection_text)%0A

Add pass and fail tests for Preview Tool