예제 #1
0
class ImagePage(Page):

    image = Locator("CSS", "#media_container img")
    input_field = Locator("XPATH", "//input[@class='q']")
    image_link = Locator("CSS", ".search_results .item>a")

    def download_image(self):
        print("Download image")
        self.get_element(self.input_field).send_keys("Sunny Fitness")
        self.get_element(self.input_field).send_keys(Keys.ENTER)
        i = random.randrange(len(self.get_elements(self.image_link)))
        self.driver.execute_script("arguments[0].click();", self.get_elements(self.image_link)[i])
        src = self.get_element(self.image).get_attribute('src')
        if platform.system() == 'Windows':
            with open("./image/image.jpg", "wb") as f:
                f.write(requests.get(src).content)
        else:
            my_path = os.path.abspath(os.path.dirname(__file__))
            my_path = my_path.split("page", 1)[0]
            path = os.path.join(my_path, "image/image.jpg")
            with open(path, "wb") as f:
                f.write(requests.get(src).content)

    def to_facebook(self):
        self.driver.get("https://www.facebook.com/")
예제 #2
0
class AddFriendsPage(Page):

    add_friend_button = Locator(
        "XPATH",
        "//button[contains(@class,'FriendRequestAdd') and not(contains(@class,'hidden_elem'))]"
    )
    confirm_friend = Locator("CSS", ".layerConfirm")
    cancel_check = Locator("CSS", ".layerCancel")

    def add_random_friends(self):
        time.sleep(1)
        for i in range(random.randrange(10)):
            if self.exists(self.add_friend_button):
                self.driver.execute_script(
                    "arguments[0].click();",
                    self.get_element(self.add_friend_button))
                time.sleep(1)
                if self.exists(self.confirm_friend):
                    self.driver.execute_script(
                        "arguments[0].click();",
                        self.get_element(self.confirm_friend))
                    time.sleep(1)
                if self.exists(self.cancel_check):
                    self.driver.execute_script(
                        "arguments[0].click();",
                        self.get_element(self.cancel_check))
                    self.driver.execute_script(
                        "arguments[0].className += 'hidden_elem';",
                        self.get_element(self.add_friend_button))
                    time.sleep(1)
예제 #3
0
class UserPage(Page):

    go_to_friends_loc = Locator("XPATH", "//a[@data-tab-key='friends']")

    def go_to_friends_page(self):
        time.sleep(1)
        self.driver.execute_script("arguments[0].click();",
                                   self.get_element(self.go_to_friends_loc))
        time.sleep(1)
예제 #4
0
class UserFriendsPage(Page):

    friends = Locator("XPATH", "//div[@data-testid='friend_list_item']/a")

    def go_to_random_friend(self):
        time.sleep(1)
        i = random.randrange(len(self.get_elements(self.friends)) - 1)
        self.get_elements(self.friends)[i].click()
        time.sleep(1)
예제 #5
0
class Page:

    logo = Locator("CSS", "._19eb")

    def __init__(self, driver):
        self.driver = driver

    def get_element(self, locator):
        return self.driver.find_element(locator.get_by(), locator.get_name())

    def get_elements(self, locator):
        return self.driver.find_elements(locator.get_by(), locator.get_name())

    def wait(self, locator):
        wait = WebDriverWait(self.driver, 10)
        wait.until(ec.visibility_of_element_located((locator.get_by(), locator.get_name())))

    def wait_to_be_click(self, locator):
        wait = WebDriverWait(self.driver, 10)
        wait.until(ec.element_to_be_clickable((locator.get_by(), locator.get_name())))

    def wait_to_disappear(self, locator):
        wait = WebDriverWait(self.driver, 30)
        wait.until(ec.invisibility_of_element((locator.get_by(), locator.get_name())))

    def exists(self, locator):
        try:
            self.get_element(locator)
        except NoSuchElementException:
            return False
        return True

    def scroll(self, length):
        scrolled = 0
        while scrolled < length:
            scrolled += 10
            self.driver.execute_script("window.scrollTo(0, " + str(scrolled) + ");")
            time.sleep(0.075)

    def to_main(self):
        self.get_element(self.logo).click()
        time.sleep(1)
예제 #6
0
class GroupPage(Page):

    input_field = Locator("CSS", "._3nd0")
    submit_button = Locator(
        "XPATH", "//button[@data-testid='react-composer-post-button']")
    add_photo_button = Locator("XPATH", "//a[@role='presentation']")
    add_image = Locator("XPATH", "//input[@type='file']")
    cancel_button = Locator("CSS", ".layerCancel")

    inbox_messages = Locator(
        "XPATH", "//a[contains(@href,'messages') and @role='tab'] ")

    notifications_loc = Locator(
        "XPATH", "//a[contains(@href,'notifications') and @role='tab'] ")

    like_buttons = Locator("XPATH", "//a[@data-testid='fb-ufi-likelink']")

    def post(self):
        self.wait(self.input_field)
        self.get_element(self.input_field).click()
        self.wait(self.add_photo_button)
        self.get_element(self.add_photo_button).click()
        time.sleep(1)
        my_path = os.path.abspath(os.path.dirname(__file__))
        my_path = my_path.split("page", 1)[0]
        path = os.path.join(my_path, "image/image.jpg")
        self.get_element(self.add_image).send_keys(path)

        text = {
            1:
            "Health and wellness is a process of achieving one’s personal best state of mental and physical being.",
            2:
            "All you need is love. But a little chocolate now and then doesn't hurt.",
            3: "Dieting is the only game where you win when you lose!",
            4: "Don't cry because it's over, smile because it happened.",
            5: "A fit, healthy body-- that is the best fashion statement."
        }.get(random.randrange(5),
              "A fit, healthy body-- that is the best fashion statement.")

        ActionChains(self.driver).send_keys(text).perform()
        self.get_element(self.submit_button).click()
        time.sleep(5)
        if self.exists(self.cancel_button):
            self.get_element(self.cancel_button).click()
            time.sleep(1)

    def check_group_inbox(self):
        time.sleep(1)
        self.get_element(self.inbox_messages).click()
        time.sleep(6)

    def check_group_notifications(self):
        time.sleep(1)
        self.get_element(self.notifications_loc).click()
        time.sleep(3)

    def like_group_posts(self):
        self.scroll(random.randrange(10000))
        if self.exists(self.like_buttons):
            for i in range(random.randrange(5)):
                range_num = len(self.get_elements(self.like_buttons))
                x = random.randrange(range_num)
                self.driver.execute_script(
                    "arguments[0].click();",
                    self.get_elements(self.like_buttons)[x])
예제 #7
0
 def get_posted_loc(self, text):
     return Locator("XPATH", "//p[contains(.,'" + text + "')]")
예제 #8
0
class FacebookPage(Page):

    logout_page_menu = Locator("CSS", ".loggedout_menubar")

    go_to_friends_but = Locator(
        "XPATH",
        "//a[contains(@href,'https://www.facebook.com/find-friends/')]")
    go_to_user_but = Locator(
        "XPATH",
        "//*[@id='userNav']//a[contains(@href,'https://www.facebook.com/')]")
    go_to_link_loc = Locator(
        "XPATH",
        "//a[contains(@href, 'http') and not(contains(@href, 'facebook'))]")

    black_monitor = Locator("CSS", "._3ixn")

    post_textarea = Locator("XPATH",
                            "//textarea[contains(@class,'navigationFocus')]")
    post_button = Locator(
        "XPATH", "//div[@data-testid='status-attachment-mentions-input']")
    submit_post = Locator(
        "XPATH", "//button[@data-testid='react-composer-post-button']")
    same_post_close = Locator("CSS", ".layerCancel")

    def get_posted_loc(self, text):
        return Locator("XPATH", "//p[contains(.,'" + text + "')]")

    comment_buttons = Locator("XPATH", "//a[@testid='UFI2CommentLink']")

    like_buttons = Locator("XPATH", "//a[@data-testid='UFI2ReactionLink']")

    image_link = Locator("XPATH", "//a[@data-render-location='newsstand']")
    image_div = Locator("XPATH", "//a[contains(@class,'snowliftPager next')]")
    image_close = Locator(
        "XPATH", "//div[@id='pagelet_photo_viewer_init']/../a[@role='button']")

    stories_div = Locator("CSS", ".vertical-4pog")
    story_div = Locator(
        "XPATH",
        "//div[contains(@class,'size-small-48') and not(contains(@class, 'no-border'))]"
    )
    close_story = Locator("XPATH",
                          "//div[@class='uiContextualLayerParent']/../a")

    watch_notifications_loc = Locator("XPATH", "//a[@name='notifications']")
    notifications_loc = Locator("XPATH",
                                "//a[@data-testid='notif_list_item_link']")

    watch_chats_loc = Locator("XPATH", "//a[@name='mercurymessages']")

    logout_menu_loc = Locator("CSS", "#logoutMenu a")
    group_link = Locator("XPATH", "//a[contains(@data-gt,'menu_pages')]")

    def is_login_page(self):
        return self.exists(self.logout_page_menu)

    def clean_monitor(self):
        if self.exists(self.black_monitor):
            self.get_element(self.black_monitor).click()

    def make_a_post(self):
        time.sleep(1)
        text = {
            1: "What a great day",
            2: "Really enjoying this weather lately",
            3: "What are some of your favorite songs lately?",
            4: "Anyone have great recommendations for great food",
            5: "Looking for a new TV show to watch",
            6: "Recommendations for top new movies to check out"
        }.get(random.randrange(6), "What a great day")
        if self.exists(self.post_textarea):
            self.get_element(self.post_textarea).send_keys(text)
        else:
            self.get_element(self.post_button).click()
            ActionChains(self.driver).send_keys(text).perform()
        time.sleep(1)
        self.driver.execute_script("arguments[0].click();",
                                   self.get_element(self.submit_post))
        self.wait(self.get_posted_loc(text))
        time.sleep(1)
        if self.exists(self.same_post_close):
            self.driver.execute_script("arguments[0].click();",
                                       self.get_element(self.same_post_close))
            time.sleep(1)

    def random_scroll(self):
        for i in range(random.randrange(5)):
            self.scroll(random.randrange(10000))

    def add_comment(self):
        time.sleep(1)
        i = random.randrange(len(self.get_elements(self.comment_buttons)))
        self.driver.execute_script("arguments[0].click();",
                                   self.get_elements(self.comment_buttons)[i])
        ActionChains(self.driver).send_keys('Cool!!').send_keys(
            Keys.ENTER).perform()

    def like_random_posts(self):
        if self.exists(self.like_buttons):
            for i in range(random.randrange(10)):
                range_num = len(self.get_elements(self.like_buttons))
                x = random.randrange(range_num)
                time.sleep(1)
                self.driver.execute_script(
                    "arguments[0].click();",
                    self.get_elements(self.like_buttons)[x])

    def open_image(self):
        if self.exists(self.image_link):
            print('Open Image')
            i = random.randrange(len(self.get_elements(self.image_link)))
            self.driver.execute_script("arguments[0].click();",
                                       self.get_elements(self.image_link)[i])
            time.sleep(1)
            for x in range(random.randrange(10)):
                ActionChains(self.driver).move_to_element(
                    self.get_element(self.image_div)).click().perform()
                time.sleep(1)
            self.driver.execute_script("arguments[0].click();",
                                       self.get_element(self.image_close))
            time.sleep(1)

    def watch_stories(self):
        if self.exists(self.story_div):
            print('Watch stories')
            self.driver.execute_script("arguments[0].click();",
                                       self.get_element(self.story_div))
            time.sleep(1)
            try:
                self.wait_to_disappear(self.black_monitor)
            except TimeoutException:
                self.driver.execute_script("arguments[0].click();",
                                           self.get_element(self.close_story))

    def watch_notifications(self):
        print('Watch notifications')
        self.driver.execute_script(
            "arguments[0].click();",
            self.get_element(self.watch_notifications_loc))
        time.sleep(1)
        i = random.randrange(len(self.get_elements(self.notifications_loc)))
        self.get_elements(self.notifications_loc)[i].click()
        time.sleep(2)
        if self.exists(self.black_monitor):
            self.driver.execute_script("arguments[0].click();",
                                       self.get_element(self.image_close))
        else:
            self.to_main()

    def watch_chats(self):
        print('Watch chats')
        self.get_element(self.watch_chats_loc).click()
        time.sleep(2)
        self.get_element(self.watch_chats_loc).click()
        time.sleep(1)

    def go_to_friends_page(self):
        if self.exists(self.go_to_friends_but):
            self.get_element(self.go_to_friends_but).click()
            return True
        else:
            return False

    def go_to_user_page(self):
        self.get_element(self.go_to_user_but).click()
        time.sleep(1)

    def go_to_link(self):
        self.scroll(random.randrange(10000))
        if self.exists(self.go_to_link_loc):
            print("Go to link")
            i = random.randrange(len(self.get_elements(self.go_to_link_loc)))
            self.driver.execute_script(
                "arguments[0].click();",
                self.get_elements(self.go_to_link_loc)[i])
            time.sleep(5)
            self.driver.switch_to.window(self.driver.window_handles[-1])
            if 'facebook' in self.driver.current_url:
                self.driver.get("https://www.facebook.com/")
            else:
                self.driver.close()
                self.driver.switch_to.window(self.driver.window_handles[0])
            time.sleep(2)

    def go_to_group(self):
        self.get_element(self.logout_menu_loc).click()
        time.sleep(1)
        if self.exists(self.group_link):
            self.get_element(self.group_link).click()
            time.sleep(2)
            return True
        return False

    def go_to_image(self):
        self.driver.get("https://pixabay.com/")