Exemple #1
0
def check_element_parent(tag: HtmlTag) -> HtmlTag:
    """
    Check the image parent tags have keywords in attributes and in url
    Args:
        tag (HtmlTag): html tag

    Returns:
        HtmlTag: scored or not scored tag
    """
    parent_tag = tag.get_parent_tag()
    while parent_tag.get_name() != TAG_NAME_BODY:
        for attr in IMPORTANT_ATTRIBUTES:
            attribute_value = parent_tag.get_attribute_value(attr)
            if not attribute_value:
                continue
            if any(word in attribute_value for word in LOGO_KEYWORDS):
                tag.add_score(PRIORITY_HIGH)
            if attr == ATTRIBUTE_NAME_HREF and any(
                    attribute_value.endswith(url) for url in IMPORTANT_URLS):
                tag.add_score(PRIORITY_HIGH)
        try:
            parent_tag = parent_tag.get_parent_tag()
        except:
            break
    return tag
Exemple #2
0
    def test_check_image_size_should_add_score_if_image_has_needed_size(self):
        mocker_tag = Mock()
        html_tag = HtmlTag(tag=mocker_tag, is_image=True)
        expected_score = html_tag.get_score() + PRIORITY_MINOR

        mocker_tag.size = {'width': 100, 'height': 100}
        scored_tag = check_image_size(html_tag)
        self.assertEqual(scored_tag.get_score(), expected_score)
Exemple #3
0
    def test_get_image_url_should_return_none_if_no_has_image_url(self):
        css_background_parameter = CSS_PROPERTY_VALUE_NONE

        self.mocked_tag.value_of_css_property.return_value = css_background_parameter

        self.html_tag = HtmlTag(tag=self.mocked_tag, is_image=False)

        self.assertIsNone(self.html_tag.get_image_url())
Exemple #4
0
    def test_get_image_url_should_return_style_image_url_if_it_no_image(self):
        css_background_parameter = 'url("{image_url}")'.format(
            image_url=self.test_image_url)

        self.mocked_tag.value_of_css_property.return_value = css_background_parameter

        self.html_tag = HtmlTag(tag=self.mocked_tag, is_image=False)

        self.assertEqual(self.html_tag.get_image_url(), self.test_image_url)
Exemple #5
0
    def test_check_element_coordinates_should_add_score_if_image_has_needed_coordinates_in_page(
            self):
        mocker_tag = Mock()
        html_tag = HtmlTag(tag=mocker_tag, is_image=True)
        expected_score = html_tag.get_score() + PRIORITY_MEDIUM

        mocker_tag.location = {'x': 100, 'y': 100}
        scored_tag = check_element_coordinates(html_tag)
        self.assertEqual(scored_tag.get_score(), expected_score)
Exemple #6
0
    def test_check_image_extension_should_add_score_if_image_url_contains_image_extesions(
            self):
        mocker_tag = Mock()
        html_tag = HtmlTag(tag=mocker_tag, is_image=True)
        expected_score = html_tag.get_score() + PRIORITY_LOW

        mocker_tag.get_attribute.return_value = 'image.png'
        scored_tag = check_image_extension(html_tag)
        self.assertEqual(scored_tag.get_score(), expected_score)
Exemple #7
0
    def test_check_image_url_should_add_score_if_image_url_contains_keywords(
            self):
        mocker_tag = Mock()
        html_tag = HtmlTag(tag=mocker_tag, is_image=True)
        expected_score = html_tag.get_score() + PRIORITY_HIGH

        mocker_tag.get_attribute.return_value = 'logo'
        scored_tag = check_image_url(html_tag)
        self.assertEqual(scored_tag.get_score(), expected_score)
Exemple #8
0
 def test_check_attribute_should_add_point_if_parent_is_link_and_contains_keywords(
         self):
     mocker_tag = Mock()
     mocker_tag.get_attribute.return_value = 'index.html'
     html_tag = HtmlTag(tag=mocker_tag)
     old_score = html_tag.get_score()
     expected_score = old_score + PRIORITY_MAJOR
     scored_tag = check_attribute(html_tag)
     self.assertEqual(scored_tag.get_score(), expected_score)
Exemple #9
0
 def test_check_attribute_should_add_point_if_attributes_contains_keywords_in_any_case(
         self):
     mocker_tag = Mock()
     mocker_tag.get_attribute.return_value = 'LOGO'
     html_tag = HtmlTag(tag=mocker_tag)
     old_score = html_tag.get_score()
     expected_score = old_score + 20
     scored_tag = check_attribute(html_tag)
     self.assertEqual(scored_tag.get_score(), expected_score)
Exemple #10
0
    def test_check_element_visibility_should_add_score_if_tag_visible_to_user(
            self):
        mocker_tag = Mock()
        html_tag = HtmlTag(tag=mocker_tag, is_image=True)
        expected_score = html_tag.get_score() + PRIORITY_MEDIUM

        mocker_tag.value_of_css_property.return_value = None

        scored_tag = check_element_visibility(html_tag)
        self.assertEqual(scored_tag.get_score(), expected_score)
Exemple #11
0
def check_element_visibility(tag: HtmlTag) -> HtmlTag:
    """
    Check the tag is visible for user
    Args:
        tag (HtmlTag): html tag

    Returns:
        HtmlTag: scored or not scored tag
    """
    if tag.is_visible():
        tag.add_score(PRIORITY_MEDIUM)
    return tag
Exemple #12
0
def check_image_extension(tag: HtmlTag) -> HtmlTag:
    """
    Check the image name has extensions
    Args:
        tag (HtmlTag): html tag

    Returns:
        HtmlTag: scored or not scored tag
    """
    if any(tag.get_image_url().endswith(extension)
           for extension in IMPORTANT_FILE_EXTENSIONS):
        tag.add_score(PRIORITY_LOW)
    return tag
Exemple #13
0
def check_element_coordinates(tag: HtmlTag) -> HtmlTag:
    """
    Check the image placement in top of page
    Args:
        tag (HtmlTag): html tag

    Returns:
        HtmlTag: scored or not scored tag
    """
    coordinates = tag.get_coordinates()
    x, y = coordinates[COORDINATE_X], coordinates[COORDINATE_Y]
    if COORDINATE_Y_MIN < y < COORDINATE_Y_MAX and x > COORDINATE_X_MIN:
        tag.add_score(PRIORITY_MEDIUM)
    return tag
Exemple #14
0
def check_image_size(tag: HtmlTag) -> HtmlTag:
    """
    Check the image size has acceptable size
    Args:
        tag (HtmlTag): html tag

    Returns:
        HtmlTag: scored or not scored tag
    """
    tag_size = tag.get_size()
    width, height = tag_size[SIZE_KEY_WIDTH], tag_size[SIZE_KEY_HEIGHT]

    if LOGO_MAX_WIDTH > width > LOGO_MIN_WIDTH and LOGO_MAX_HEIGHT > height > LOGO_MIN_HEIGHT:
        tag.add_score(PRIORITY_MINOR)
    return tag
Exemple #15
0
def check_image_url(tag: HtmlTag) -> HtmlTag:
    """
    Check the image url path has keywords
    Args:
        tag (HtmlTag): html tag

    Returns:
        HtmlTag: scored or not scored tag
    """
    tag_image_url = tag.get_image_url()
    if not tag_image_url:
        tag.exclude()
        return tag
    if any(word in tag_image_url for word in LOGO_KEYWORDS):
        tag.add_score(PRIORITY_HIGH)
    return tag
Exemple #16
0
    def test_check_attribute_should_pass_add_point_if_not_contains_attributes(
            self):
        mocker_tag = Mock()
        mocker_tag.get_attribute.return_value = None
        html_tag = HtmlTag(tag=mocker_tag)

        self.assertEqual(check_attribute(html_tag), html_tag)
Exemple #17
0
    def test_check_element_parent_should_pass_if_parent_tags_no_have_attributes(
            self):
        mocker_tag = Mock()
        html_tag = HtmlTag(tag=mocker_tag, is_image=True)
        expected_score = html_tag.get_score()

        body_element = Mock()
        body_element.tag_name = 'body'

        parent_element = Mock()
        parent_element.tag_name = 'a'
        parent_element.get_attribute.return_value = None
        parent_element.find_element_by_xpath.return_value = body_element

        mocker_tag.find_element_by_xpath.return_value = parent_element

        scored_tag = check_element_parent(html_tag)
        self.assertEqual(scored_tag.get_score(), expected_score)
Exemple #18
0
    def test_check_element_parent_should_add_score_if_parent_link_attributes_have_keywords_urls(
            self):
        mocker_tag = Mock()
        html_tag = HtmlTag(tag=mocker_tag, is_image=True)
        expected_score = html_tag.get_score() + PRIORITY_HIGH

        body_element = Mock()
        body_element.tag_name = 'body'

        parent_element = Mock()
        parent_element.tag_name = 'a'
        parent_element.get_attribute.return_value = 'index.html'
        parent_element.find_element_by_xpath.return_value = body_element

        mocker_tag.find_element_by_xpath.return_value = parent_element

        scored_tag = check_element_parent(html_tag)
        self.assertEqual(scored_tag.get_score(), expected_score)
Exemple #19
0
def check_attribute(tag: HtmlTag) -> HtmlTag:
    """
    Check the tag attributes (class, id, etc.) has keywords
    Args:
        tag (HtmlTag): html tag

    Returns:
        HtmlTag: scored or not scored tag
    """
    for attr in IMPORTANT_ATTRIBUTES:
        attribute_value = tag.get_attribute_value(attr)
        if not attribute_value:
            continue
        attribute_value = attribute_value.lower()
        if any(word in attribute_value for word in LOGO_KEYWORDS):
            tag.add_score(PRIORITY_HIGH)

        if attr == ATTRIBUTE_NAME_HREF and any(word in attribute_value
                                               for word in IMPORTANT_URLS):
            tag.add_score(PRIORITY_MAJOR)
    return tag
Exemple #20
0
class HtmlTagTestCase(TestCase):
    def setUp(self):
        self.mocked_tag = Mock()
        self.test_image_url = 'http://example.com/img.png'
        self.html_tag = HtmlTag(tag=self.mocked_tag, is_image=True)

    def test_exclude_should_set_true_for_exclude_parameter(self):
        self.html_tag.exclude()

        self.assertEqual(self.html_tag.is_excluded(), True)

    def test_get_score_should_return_tag_score(self):
        self.assertEqual(self.html_tag.get_score(), 1)

    def test_get_score_should_return_zero_if_tag_excluded(self):
        expected_score = 0
        self.html_tag.exclude()
        self.assertEqual(self.html_tag.get_score(), expected_score)

    def test_add_score_should_add_value_to_score_parameter(self):
        expected_score = 6
        self.html_tag.add_score(PRIORITY_HIGH)
        self.assertEqual(self.html_tag.get_score(), expected_score)

    def test_get_image_url_should_return_attribute_src_if_it_image(self):
        expected_image_url = self.test_image_url
        self.mocked_tag.get_attribute.return_value = expected_image_url
        self.assertEqual(self.html_tag.get_image_url(), expected_image_url)

    def test_get_image_url_should_return_style_image_url_if_it_no_image(self):
        css_background_parameter = 'url("{image_url}")'.format(
            image_url=self.test_image_url)

        self.mocked_tag.value_of_css_property.return_value = css_background_parameter

        self.html_tag = HtmlTag(tag=self.mocked_tag, is_image=False)

        self.assertEqual(self.html_tag.get_image_url(), self.test_image_url)

    def test_get_image_url_should_return_none_if_no_has_image_url(self):
        css_background_parameter = CSS_PROPERTY_VALUE_NONE

        self.mocked_tag.value_of_css_property.return_value = css_background_parameter

        self.html_tag = HtmlTag(tag=self.mocked_tag, is_image=False)

        self.assertIsNone(self.html_tag.get_image_url())

    def test_is_visible_should_return_false_if_tag_has_display_none_style(
            self):
        self.mocked_tag.value_of_css_property.return_value = CSS_PROPERTY_VALUE_NONE

        self.assertFalse(self.html_tag.is_visible())

    def test_is_visible_should_return_false_if_tag_has_hidden_style(self):
        self.mocked_tag.value_of_css_property.return_value = CSS_PROPERTY_VALUE_HIDDEN

        self.assertFalse(self.html_tag.is_visible())

    def test_is_visible_should_return_true_if_tag_no_has_hide_styles(self):
        self.mocked_tag.value_of_css_property.return_value = None

        self.assertTrue(self.html_tag.is_visible())

    def test_get_size_should_return_tags_width_and_height_value(self):
        expected_tag_size = (100, 100)
        self.mocked_tag.size = expected_tag_size

        self.assertEqual(self.html_tag.get_size(), expected_tag_size)

    def test_get_coordinates_should_return_tags_coordinate_parameters(self):
        expected_tag_coordinates = {COORDINATE_X: 100, COORDINATE_Y: 100}
        self.mocked_tag.location = expected_tag_coordinates

        self.assertEqual(self.html_tag.get_coordinates(),
                         expected_tag_coordinates)

    def test_get_parent_tag_should_return_tags_parent_element(self):
        expected_element = 'Parent element'
        self.mocked_tag.find_element_by_xpath.return_value = expected_element

        self.assertIsInstance(self.html_tag.get_parent_tag(), HtmlTag)

    def test_get_name_should_return_tags_name(self):
        expected_element_name = TAG_NAME_BODY
        self.mocked_tag.tag_name = expected_element_name

        self.assertEqual(self.html_tag.get_name(), expected_element_name)

    def test_get_attribute_value_should_return_tags_attribute_value(self):
        expected_attribute_value = self.test_image_url
        self.mocked_tag.get_attribute.return_value = expected_attribute_value

        self.assertEqual(self.html_tag.get_attribute_value(ATTRIBUTE_NAME_SRC),
                         expected_attribute_value)
Exemple #21
0
 def test_check_image_url_should_exclude_tag_if_no_has_image_url(self):
     mocker_tag = Mock()
     mocker_tag.get_attribute.return_value = None
     html_tag = HtmlTag(tag=mocker_tag, is_image=True)
     scored_tag = check_image_url(html_tag)
     self.assertTrue(scored_tag.is_excluded())
Exemple #22
0
 def setUp(self):
     self.mocked_tag = Mock()
     self.test_image_url = 'http://example.com/img.png'
     self.html_tag = HtmlTag(tag=self.mocked_tag, is_image=True)