Пример #1
0
def test_bad_url_4():
    try:
        get_url.parse_site('')
        assert False
    except Exception:
        assert True
Пример #2
0
    def register_address(self,
                         url,
                         font_size=18.0,
                         title_font_size=19.0,
                         back_forward_call=False):
        # Clear the elements related to the previous website
        self.clear_layout(self.website_layout)
        self.clear_layout(self.scroll_layout)
        self.tab_title = url  # resets the tab title to the url (later changes if an actual title is found)

        # If the url is empty, just return
        if url.strip() == "":
            return

        try:
            # Retrieve the data from the url and return the resulting info from parsing the data
            website_elements = parse_site(url)

            #Luke Douville's edit
            fl = open("website_elements.txt", "w")
            fl.write(str(website_elements))
            fl.close()
            global website_elements_refresh
            website_elements_refresh = website_elements

            if website_elements:
                for (htmlType, web_element) in website_elements:
                    new_label = QLabel()
                    print("\n\nhtmlType = ", htmlType)
                    print("\n\n")
                    # Links
                    if (htmlType == 'a'):
                        if str(web_element[0]).strip() == "":
                            new_label.setText(str(web_element[1]))
                        else:
                            # Get the first element of the tuple which is the text to display for the link
                            new_label.setText(str(web_element[0]))

                        new_label.setStyleSheet(
                            'color: blue')  # Links are set to have blue text
                        new_label.setFont(
                            QFont("Times", self.font_size,
                                  QFont.Normal))  # changes the font size

                        new_label.mousePressEvent = functools.partial(
                            self.link_clicked,
                            source_object=new_label,
                            url_text=web_element[1])
                    # Titles
                    elif (htmlType == 'title'):
                        self.tab_title = str(
                            web_element)  # Resets the title of the web page
                        new_label.setText(str(web_element))
                        title_font = QFont("Times", self.title_font_size,
                                           QFont.Bold)
                        new_label.setFont(title_font)
                    # Images
                    elif (htmlType == 'img'):
                        # web_elements contains (img_url)
                        new_img = self.store_img(str(url), str(web_element[0]))
                        if new_img == None:
                            new_label.setText(web_element[1])
                        else:
                            pixmap = QPixmap("Images/" + new_img)
                            new_label.setPixmap(pixmap)
                    # CSS
                    elif (htmlType == 'css'):
                        #display the contents of the page in accordance with CSS properties
                        print(
                            "\n\n--------------------------\nCSS found!\n-------------------------------------"
                        )

                        #If the CSS says anything about the display
                        #Massive for loop?
                        #                        for i in range(0, len(web_element)):
                        #                        if web_element.startswith("background-color", i, len(web_element)):
                        if "background-color:" in web_element:
                            index = web_element.find(
                                "background-color:"
                            ) + 17  #place the index right after the text
                            color = ""
                            tempString = ""
                            for n in range(index, len(web_element)):
                                if n < web_element.find(
                                        " ", index, len(web_element)):
                                    tempString += web_element[n]
                            color += re.findall(
                                '[a-z]',
                                web_element[index:web_element.
                                            find(";", index,
                                                 len(web_element) - 1)])
                            #                            color += tempString
                            print("\n\n----------------------------\nColor: ",
                                  color)
                            print("----------------------------------")
                            for n in range(0, len(color)):
                                if color[n] == "#":
                                    continue
                                if color[n] == "f" or color[n] == "F":
                                    #f is max value, so deepest red/green/blue possible
                                    #fff is pure black
                                    #000 is pure white
                                    if n == 0:
                                        red = 255
                                    elif n == 1:
                                        green = 255
                                    elif n == 2:
                                        blue = 255
#                        continue
                    elif (htmlType == 'style'):
                        #display the contents of the page in accordance with CSS properties
                        print(
                            "\n\n--------------------------\nstyle found!\n-----------------------------------"
                        )
                        continue
                    else:
                        new_label.setText(str(web_element))

                    # Add the resulting element to the GUI
                    self.scroll_layout.addWidget(new_label)

        except Exception as e:
            # Display an error if there's an error while trying to access the website
            self.display_error("This site can't be reached.")

        # Add the URL to the global history list
        self.parent.history_list.append(url)
        # Call method to add urls in history to toolbar
        self.parent.create_history_action(url)

        # Call a method to update the tab title from the BrowserWindow class
        self.parent.update_tab_title()
        # Resets the address bar text to match the current url
        self.address_bar.setText(str(url))

        # Changes the image of the bookmark button based on whether the url is bookmarked
        if url in self.parent.bookmark_list:
            self.bookmarkIcon.addPixmap(
                QPixmap("./Resources/bookmarkFilledIcon_1600x1600.PNG"))
            self.bookmark_button.setIcon(self.bookmarkIcon)
        else:
            self.bookmarkIcon.addPixmap(
                QPixmap("./Resources/bookmarkIcon_1600x1600.PNG"))
            self.bookmark_button.setIcon(self.bookmarkIcon)

        # Only append to the back button stack if register_address isn't being called by the back button
        if not back_forward_call:
            # Empties the forward button stack
            self.forward_history_stack.clear()

            # If there was a previous url loaded by the tab, append it to the back button stack
            if not self.current_url.strip() == "":
                self.back_history_stack.append(self.current_url)

        # Resets the current url variable to be the url just loaded
        # (NOTE: keep below if not back_forward_call to avoid bug with back button)
        self.current_url = url
Пример #3
0
def test_bad_url_3():
    try:
        get_url.parse_site('http://google')
        assert False
    except Exception:
        assert True
Пример #4
0
def test_bad_url():
    try:
        get_url.parse_site('I_like_WAFFles')
        assert False
    except Exception:
        assert True
Пример #5
0
def test_url_suceed_without_full_url():
    try:
        get_url.parse_site('google.com')
        assert True
    except Exception:
        assert False
Пример #6
0
def test_url_suceed_2():
    try:
        get_url.parse_site('https://google.com')
        assert True
    except Exception:
        assert False
Пример #7
0
def test_url_suceed():
    try:
        get_url.parse_site('http://pythonprogramming.net')
        assert True
    except Exception:
        assert False