Example #1
0
def download_lesson(driver: WebDriver, lesson: WebElement,
                    module_path: pathlib.Path) -> None:
    lesson_url = lesson.get_attribute('href')

    course_page = driver.current_window_handle

    driver.execute_script('window.open()')
    driver.switch_to.window(driver.window_handles[1])
    driver.get(lesson_url)

    youtube_url = get_youtube_url(driver)
    if youtube_url is not None:

        # BUG: the output option is not working; fix it;
        output_path = module_path / '%(title)s.%(ext)s'
        ydl_opts = {
            'output': str(output_path),
            'retries': 10,
        }

        try:
            with youtube_dl.YoutubeDL(ydl_opts) as ydl:
                ydl.download([youtube_url])
        except youtube_dl.utils.DownloadError as e:
            print(e)

    driver.close()
    driver.switch_to.window(course_page)
Example #2
0
def increase_quantity_to_two(driver: WebDriver):
    print("Attempting to increase quantity to two!")
    # Open quantity selection
    quantity_open_button = "guest-counter"
    WebDriverWait(driver, 5).until(
        EC.presence_of_element_located((By.ID, quantity_open_button)))

    open_quantity_button = driver.find_element_by_id(quantity_open_button)
    driver.execute_script("arguments[0].click();", open_quantity_button)

    # Find the div that holds the increase quantity button
    div_holder_of_increase_button = "rec-guest-counter-row"
    WebDriverWait(driver, 5).until(
        EC.presence_of_element_located(
            (By.CLASS_NAME, div_holder_of_increase_button)))
    open_div_holder_of_increase_button: WebElement = driver.find_element_by_class_name(
        div_holder_of_increase_button)

    # Get the button we want from within this element
    plus_and_minus_button_class_name = "sarsa-button-subtle"
    buttons = open_div_holder_of_increase_button.find_elements_by_class_name(
        plus_and_minus_button_class_name)

    select_plus_button = buttons[1]
    driver.execute_script("arguments[0].click();", select_plus_button)

    # # Hit close and continue (probably not needed)
    # close_button_selector = "sarsa-button-link"
    # button = open_div_holder_of_increase_button.find_element_by_class_name(close_button_selector)
    # driver.execute_script("arguments[0].click();", button)
    print("Successful at increasing quantity to two!")
Example #3
0
def log_in(driver: WebDriver):

    print("Logging in...")
    global PASSWORD, USERNAME

    username = USERNAME
    password = PASSWORD

    sign_in_div: WebElement = driver.find_element_by_class_name("nav-user-sign-in")  # type: ignore
    sign_in_buttons: list[WebElement] = sign_in_div.find_elements_by_class_name("nav-header-button")

    for button in sign_in_buttons:
        if button.text == "Log In":
            driver.execute_script("arguments[0].click();", button)

            email_input = driver.find_element_by_id("rec-acct-sign-in-email-address")
            password_input = driver.find_element_by_id("rec-acct-sign-in-password")

            email_input.send_keys(username)  # type: ignore
            password_input.send_keys(password)  # type: ignore

            # Press Log In Button
            log_in_btn = driver.find_element_by_class_name("rec-acct-sign-in-btn")
            driver.execute_script("arguments[0].click();", log_in_btn)
            print("Logged in.")

    # Wait till login page is gone.
    WebDriverWait(driver, 10).until(EC.invisibility_of_element((By.ID, "signInModalHeading")))
    def installAddon_cleanTabs_getLoginWindow(self, driver: WebDriver) -> None:
        """
        1) Install UblockOrigin
        2) If any "welcome" windows open, close them
        3) Click on login button
        """

        driver.install_addon(
            f"{self.cfg['EXTENSIONS']['FOX_EXT_BASE_PATH']}/{self.cfg['EXTENSIONS']['UBO']}"
        )

        driver.get(self.cfg["LOGIN"]["LOGIN_FROM"])
        WebDriverWait(self.driver, 7).until(
            EC.presence_of_all_elements_located((By.TAG_NAME, "main")))
        # wait for the page to load

        # Points to ww login-page
        MAIN_HANDLE: str = driver.current_window_handle
        for window_handle in driver.window_handles:
            if window_handle != MAIN_HANDLE:
                driver.switch_to.window(window_handle)
                # after closing all irrelevant tabs driver will focus back to the main one
                driver.close()
        # Puts focus back on ww login-page
        driver.switch_to.window(MAIN_HANDLE)
        # Use this stuff to setup the login window (You don't want any junk in some new tab)
        sleep(2)
        # click the first button on the page, it will make login button visible
        driver.execute_script(
            self.clickFirstElementFromElementList("button") +
            self.clickElementWithInnerTextS("button", "log in"))
Example #5
0
class BaseLiveServerTestCase(LiveServerTestCase):
    def setUp(self):
        get_or_create_test_admin()
        self.browser = WebDriver()
        self.do_admin_login('test', 'test')

    def tearDown(self):
        self.browser.quit()

    def do_admin_login(self, username, password):
        self.browser.get('%s%s' % (self.live_server_url, '/admin/'))
        username_input = self.browser.find_element_by_name("username")
        username_input.send_keys(username)
        password_input = self.browser.find_element_by_name("password")
        password_input.send_keys(password)
        self.browser.find_element_by_xpath('//input[@value="Log in"]').click()

    def new_window(self, name='other'):
        self.browser.execute_script('window.open("/admin/", "'+ name +'")')
        self.browser.switch_to_window(self.browser.window_handles[1])
        while self.warning_element() is False:
            time.sleep(0.1)
        self.browser.switch_to_window(self.browser.window_handles[0])

    def press_space(self):
        a = ActionChains(self.browser)
        a.key_down(Keys.SPACE)
        a.perform()

    def wait_for_pages_loaded(self):
        for win in self.browser.window_handles:
            while self.warning_element() is False:
                time.sleep(0.1)
Example #6
0
class StickToTopTest(LiveServerTestCase):

    def setUp(self):
        self.browser = WebDriver()
        self.browser.implicitly_wait(3)
        self.n1 = Node.objects.create(
            title='TestNodeOne',
            description='The first test node'
        )
        self.u1 = User.objects.create_user(
            username='******', email='*****@*****.**', password='******'
        )
        self.super_user = User.objects.create_user(
            username='******', email='*****@*****.**', password='******'
        )
        self.super_user.is_superuser = True
        self.super_user.is_staff = True
        self.super_user.save()
        # Create 99 topics
        for i in range(1, 100):
            setattr(
                self,
                't%s' % i,
                Topic.objects.create(
                    title='Test Topic %s' % i,
                    user=self.u1,
                    content_raw='This is test topic __%s__' % i,
                    node=self.n1
                )
            )

    def tearDown(self):
        self.browser.quit()

    def test_stick_to_top_admin(self):
        self.browser.get(self.live_server_url + reverse("niji:index"))
        login(self.browser, 'super', '123')
        self.assertIn("Log out", self.browser.page_source)

        lucky_topic1 = getattr(self, 't%s' % random.randint(1, 50))

        self.browser.get(self.live_server_url+reverse('niji:topic', kwargs={"pk": lucky_topic1.pk}))
        self.browser.find_element_by_class_name('move-topic-up').click()
        up_level = WebDriverWait(
            self.browser, 10
        ).until(
            expected_conditions.presence_of_element_located(
                (By.NAME, 'move-topic-up-level')
            )
        )
        up_level = Select(up_level)
        up_level.select_by_visible_text('1')
        time.sleep(1)
        self.browser.execute_script("$('.modal-confirm').click()")
        self.browser.get(self.live_server_url+reverse('niji:index'))
        first_topic_title = self.browser.find_elements_by_class_name('entry-link')[0].text

        self.assertEqual(first_topic_title, lucky_topic1.title)
Example #7
0
def scroll_down(driver: WebDriver):
    scrollheight = driver.execute_script("return document.body.scrollHeight")
    if type(scrollheight) == str:
        scrollheight = int(scrollheight)
    ## lets finish the page scroll by 10 count
    scroll_amount = scrollheight / 10  ### not we might have some remainder
    for num in range(11):
        scrollable = scroll_amount * (num + 1)
        driver.execute_script(f"window.scrollTo(0, {scrollable});")
        time.sleep(0.3)
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
Example #8
0
def open_link_in_new_tab(browser: webdriver.WebDriver, url: str):
    logging.info("Opening the new tab with the url {}".format(url))
    browser.execute_script("window.open('{}', 'new_window')".format(url))

    logging.info("Waiting for the tab to be opened")
    WebDriverWait(browser, 10).until(ec.new_window_is_opened)

    logging.info("Switching to newly opened tab")
    browser.switch_to.window(
        browser.window_handles[get_last_opened_tab_id(browser)])
    WebDriverWait(browser, 10).until(ec.url_contains(url))
def parse_all_saints(driver: WebDriver, url):
    driver.get(get_url_view(url))
    driver.implicitly_wait(30)
    time.sleep(10)
    try:
        driver.execute_script("window.scrollTo(0, 40000)")
        time.sleep(10)
    except NoSuchElementException:
        pass
    product_list = make_soup(driver.page_source).find_all("div",
                                                          class_="product")
    return map(parse_product, product_list)
Example #10
0
def add_campsite_to_cart(
    driver: WebDriver,
    rows: list[WebElement],
    desired_date: str,
    prefer_sites=set(range(107, 125)),
    reserved_count: int = 0,
):
    global curr_site, url_of_site

    for row in rows:
        campsite: list[WebElement] = row.find_elements_by_class_name("rec-availability-item")

        assert len(campsite) == 1

        campsite_num = campsite[0].text

        matched: list[WebElement] = row.find_elements_by_class_name("rec-availability-date")

        for element in matched:
            aria_label: str = element.get_attribute("aria-label")
            date_from_aria = aria_label.split(" - ")[0]

            availability = element.text
            # print(
            #     f"Campsite {campsite_num} at Date {date_from_aria} is currently: {'available' if availability not in ['R', 'X'] else 'reserved'}"
            # )

            parsed_date = parse_dates(desired_date)

            if date_from_aria != parsed_date:
                continue

            if availability not in ["R", "X"]:
                driver.execute_script("arguments[0].click();", element)
                print(f"Pressed Button on {desired_date} for campsite: {campsite_num}")

                add_to_cart = driver.find_element_by_class_name("availability-page-book-now-button-tracker")
                driver.execute_script("arguments[0].click();", add_to_cart)
                print(f"Pressed add to cart for campsite {campsite_num} at date {desired_date}")

                send_discord_notification(
                    f"Campsite {campsite_num} available at date: {parsed_date}!", curr_site, url_of_site
                )
                # for _ in range(5):
                #     playsound('beep-01a.mp3')

                time.sleep(2)
                check_availability_and_reserve(driver, url_of_site, reserved_count + 1)
                return

    print("No available sites. Nothing has been added to cart.")
Example #11
0
def run(*, driver: WebDriver, url: str, opt: int):
    driver.get(url)
    try:
        main_node = driver.find_element_by_xpath("/html/body/form/div[1]")
        els: list = main_node.find_elements_by_tag_name("input")
        el = els[int(opt) - 1]
        driver.execute_script("arguments[0].click();", el)
        # delay
        time.sleep(3)
        logger.info("ok")
    except:  # noqa
        logger.info("ko")
    finally:
        driver.close()
Example #12
0
def refresh_table(driver: WebDriver):
    # Be wary of the booking notification block after hitting add to cart.
    # need to navigate around it. It's class name is: booking-notification-block
    buttons: list[WebElement] = driver.find_elements_by_class_name("sarsa-button-sm")  # type: ignore

    smart_sleep()

    print("Looking for refresh table button.")
    for button in buttons:
        span_contents: list[WebElement] = driver.find_elements_by_class_name("sarsa-button-content")  # type: ignore
        for content in span_contents:
            if content.text == "Refresh Table":
                print("Refreshing table.")
                driver.execute_script("arguments[0].click();", button)
                return
Example #13
0
def parse_topman_page(driver: WebDriver, url):
    driver.get(url)
    driver.implicitly_wait(15)
    time.sleep(5)
    set_grid_to_4(driver)
    sources = [driver.page_source]
    position = 700
    while not is_bottom(driver):
        driver.execute_script("window.scrollTo(0, {})".format(position))
        sources.append(driver.page_source)
        time.sleep(0.3)
        position += 700
    product_list = (make_soup(source).find_all("div", class_="product")
                    for source in sources)
    items = []
    for l in product_list:
        items.extend(filter(bool, map(parse_product, l)))
    return items
Example #14
0
    def harvestChapterLinks(self, driver: WebDriver, novelPageUrl: str):
        """
        1) Assuming in novel page -> open "Chapters" section
        2) Find div elements with chapter links
        3) Get the returned list of chapter links

        NOTE: You NEED to be in the novel_page for this to work
        """
        driver.get(novelPageUrl)
        sleep(5)

        driver.execute_script(self.clickElementWithInnerTextS("button", "chapters"))

        driver.execute_script(self.openAccordian(self.accordianText))
        sleep(2)

        clsTuple = ("grid", "grid-cols-1", "md:grid-cols-2", "w-full")
        divList = driver.find_elements(
            By.XPATH,
            self.getXpathStrFrClsNames("div", *clsTuple),
        )

        return get_hrefList(divList)
def get_links_on_mobile_website(driver: WebDriver) -> List[str]:
    page_num = 0
    outer_html = ""
    driver.get(mobile_website_url)

    try:
        while True:
            driver.find_element_by_css_selector('#btn_showmore').click()
            page_num += 1
            print(f'getting page number {str(page_num)}')
            time.sleep(1)
            outer_html_new = driver.execute_script("return document.documentElement.outerHTML")
            if outer_html == outer_html_new:
                break
            outer_html = outer_html_new
    except Exception as e:
        print('Aborting page load:', e)
    new_links = get_links_from_driver(driver)
    print(f'Found {len(new_links)} links on mobile')
    return new_links
def test_selenium_works():
    """ Initialize and authenticate the selenium driver """
    options = Options()
    print("CI:", os.getenv("CI"))
    print("PATH:", os.getenv("PATH"))
    if os.getenv("CI"):
        options.headless = True
        options.log.level = "trace"

    print(list(Path().iterdir()))
    try:
        driver = WebDriver(firefox_options=options)
        driver.get("https://example.com")
        content = driver.execute_script(
            "return document.getElementsByTagName('html')[0].innerHTML")
    except Exception as exc:
        print("Exception happened!", exc)
        print(list(Path().iterdir()))
        raise
    assert 'This domain is for use in illustrative examples in documents' in content
Example #17
0
).click()

try:
    WebDriverWait(driver, args.timeout).until(
        EC.visibility_of_element_located(
            (By.XPATH,
             "//div[contains(text(),'User name or password incorrect')]")))
    print(bad("User name or password incorrect"))
    driver.quit()
    exit(1)
except TimeoutException:
    print(good("Successfully Logged in"))

driver.find_element_by_xpath(
    "//span[contains(text(),'Open/Close Extensions')]").click()
last_height = driver.execute_script("return document.body.scrollHeight")
while True:
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    time.sleep(args.timeout)
    new_height = driver.execute_script("return document.body.scrollHeight")
    if new_height == last_height:
        break
    last_height = new_height
driver.find_element_by_xpath(
    "//body/div[@id='oe_container']/div[@id='oe_admin-content']/div[@id='oe_sidebar']/aside[1]/nav[1]/ul[1]/li[5]/ul[1]/li[6]/a[1]"
).click()
driver.find_element_by_xpath(
    "//a[contains(text(),'Add User Defined Tag')]").click()
print(info("Creating vulnerable user tag"))
driver.find_element_by_xpath("//input[@id='name']").send_keys(U)
driver.find_element_by_xpath("//textarea[@id='udtcode']").send_keys(
Example #18
0
class ScriptTestCase(LiveServerTestCase):
    def setUp(self):
        self.browser = WebDriver()
        self.do_admin_login('test', 'test')

    def tearDown(self):
        self.browser.quit()

    def do_admin_login(self, username, password):
        self.browser.get('%s%s' % (self.live_server_url, '/admin/'))
        username_input = self.browser.find_element_by_name("username")
        username_input.send_keys(username)
        password_input = self.browser.find_element_by_name("password")
        password_input.send_keys(password)
        self.browser.find_element_by_xpath('//input[@value="Log in"]').click()

    def new_window(self, name='other'):
        self.browser.execute_script('window.open("/admin/", "'+ name +'")')
        self.browser.switch_to_window(self.browser.window_handles[1])
        while self.warning_element() is False:
            time.sleep(0.1)
        self.browser.switch_to_window(self.browser.window_handles[0])

    def press_space(self):
        a = ActionChains(self.browser)
        a.key_down(Keys.SPACE)
        a.perform()

    def warning_element(self):
        try:
            return self.browser.find_elements_by_css_selector(
                '#session_security_warning')[0]
        except IndexError:
            return False

    def wait_for_pages_loaded(self):
        for win in self.browser.window_handles:
            while self.warning_element() is False:
                time.sleep(0.1)

    def deadline_passed(self, now, deadline):
        return (datetime.now() - now).seconds > deadline

    def assertWarningShows(self, max_seconds):
        now = datetime.now()

        for win in self.browser.window_handles:
            self.browser.switch_to_window(win)

            while self.warning_element() is False:
                time.sleep(0.1)

                if self.deadline_passed(now, max_seconds):
                    self.fail('Warning did not make it into DOM')

        for win in self.browser.window_handles:
            self.browser.switch_to_window(win)

            while self.warning_element().is_displayed() is False:
                time.sleep(0.1)

                if self.deadline_passed(now, max_seconds):
                    self.fail('Warning did not make it into DOM')

    def assertWarningHides(self, max_seconds):
        now = datetime.now()

        for win in self.browser.window_handles:
            self.browser.switch_to_window(win)

            while self.warning_element().is_displayed() is not False:
                time.sleep(0.1)

                if self.deadline_passed(now, max_seconds):
                    self.fail('Warning did not hide')

    def assertExpires(self, max_seconds):
        now = datetime.now()

        for win in self.browser.window_handles:
            self.browser.switch_to_window(win)

            while self.warning_element() is not False:
                time.sleep(0.1)

                if self.deadline_passed(now, max_seconds):
                    self.fail('Warning did not make it out of DOM')

    def assertWarningShown(self):
        for win in self.browser.window_handles:
            self.browser.switch_to_window(win)
            self.assertTrue(self.warning_element().is_displayed())

    def assertWarningHidden(self):
        for win in self.browser.window_handles:
            self.browser.switch_to_window(win)
            self.assertFalse(self.warning_element().is_displayed())

    def assertWarningNotInPage(self):
        for win in self.browser.window_handles:
            self.browser.switch_to_window(win)
            self.assertTrue(self.warning_element() is False)


    def test_single_window_inactivity(self):
        self.wait_for_pages_loaded()
        self.assertWarningHidden()
        self.assertWarningShows(9)
        self.assertExpires(9)

    def test_single_dont_show_warning(self):
        self.wait_for_pages_loaded()
        self.assertWarningHidden()
        time.sleep(3.5)
        self.press_space()
        self.assertWarningHidden()
        time.sleep(4)
        self.assertWarningHidden()

    def test_single_hide_warning(self):
        self.assertWarningShows(9)
        self.press_space()
        self.assertWarningHides(2)

    def test_double_window_inactivity(self):
        self.new_window()
        self.wait_for_pages_loaded()
        self.assertWarningHidden()
        self.assertWarningShows(9)
        self.assertExpires(9)

    def test_double_dont_show_warning(self):
        self.new_window()
        self.wait_for_pages_loaded()
        self.assertWarningHidden()
        time.sleep(3.5)
        self.press_space()
        self.assertWarningHidden()
        time.sleep(4)
        self.assertWarningHidden()

    def test_double_hide_warning(self):
        self.new_window()
        self.assertWarningShows(9)
        self.press_space()
        self.assertWarningHides(6)
Example #19
0
class LiveServerTest(object):
    fixtures = ['rules.json']

    DOWNLOAD_DIRECTORY = '/tmp/work/downloads'


    ## List all ajax enabled pages that have initialization code and must wait
    AJAX_WAIT = ['mock_galaxy_factory', 'view_job']
    SUMMARY_INDEX = str(len(MODULE_INDICES)+1)
    OUTPUT_FORMATS = [
        {'value':'csv', 'text':'CSV (Text)', 'extension':'csv'},
        {'value':'hdf5', 'text':'HDF5', 'extension':'hdf5'},
        {'value': 'fits', 'text': 'FITS', 'extension': 'fits'},
        {'value': 'votable', 'text': 'VOTable', 'extension': 'xml'}
    ]

    def wait(self, secs=1):
        time.sleep(secs * 1.0)

    def setUp(self):

        from selenium.webdriver.firefox.webdriver import FirefoxProfile
        fp = FirefoxProfile()
        fp.set_preference("browser.download.folderList", 2)
        fp.set_preference("browser.download.dir", self.DOWNLOAD_DIRECTORY)
        fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/html, application/zip, text/plain, application/force-download, application/x-tar")
        
        self.selenium = WebDriver(firefox_profile=fp)
        self.selenium.implicitly_wait(1) # wait one second before failing to find

        # create the download dir
        if not os.path.exists(self.DOWNLOAD_DIRECTORY):
            os.makedirs(self.DOWNLOAD_DIRECTORY)

    def tearDown(self):
        self.selenium.quit()
        # remove the download dir
        for root, dirs, files in os.walk(self.DOWNLOAD_DIRECTORY, topdown=False):
            for name in files:
                os.remove(os.path.join(root, name))
            for name in dirs:
                os.rmdir(os.path.join(root, name))

    def lc_id(self, bare_field):
        return '#id_light_cone-%s' % bare_field

    def lc_2select(self, bare_field):
        return 'id_light_cone-output_properties_%s' % bare_field

    def rf_id(self, bare_field):
        return '#id_record_filter-%s' % bare_field

    def sed(self, bare_field):
        return 'id_sed-%s' % bare_field

    def mi_id(self, bare_field):
        return 'id_mock_image-%s' % bare_field

    def sed_id(self, bare_field):
        return '#%s' % self.sed(bare_field)

    def sed_2select(self, bare_field):
        return 'id_sed-band_pass_filters_%s' % bare_field

    def job_select(self, bare_field):
        return 'id-job_%s' % bare_field

    def job_id(self, bare_field):
        return '#%s' % self.job_select(bare_field)

    def get_parent_element(self, element):
        return self.selenium.execute_script('return arguments[0].parentNode;', element)

    def get_element_css_classes(self, element):
        list = []
        found = element.get_attribute('class')
        if found is not None: list = found.split()
        return list

    def get_closest_by_class(self, element, css_class):
        while css_class not in self.get_element_css_classes(element):
            element = self.get_parent_element(element)
        return element

    def get_summary_selector(self, form_name, field_name):
        return 'div.summary_%s .%s' % (form_name, field_name)

    def get_summary_field(self, form_name, field_name):
        summary_selector = self.get_summary_selector(form_name, field_name)
        return self.selenium.find_element_by_css_selector(summary_selector)

    def get_summary_field_text(self, form_name, field_name):
        return self.get_summary_field(form_name, field_name).text

    def get_info_field(self, section, field):
        elem = self.selenium.find_element_by_css_selector("div.%(section)s-info .%(field)s" % {'section': section, 'field': field})
        return elem.text

    def find_element_by_css_selector(self, selector):
        retries = 3
        while retries > 0:
            try:
                elem = self.selenium.find_element_by_css_selector(selector)
                return elem
            except NoSuchElementException:
                retries -= 1
                self.wait(1)
        # If it hasn't been found by now, try one more time and let the exception through
        return self.selenium.find_element_by_css_selector(selector)

    def find_element_by_id(self, elem_id):
        retries = 3
        while retries > 0:
            try:
                elem = self.selenium.find_element_by_id(elem_id)
                return elem
            except NoSuchElementException:
                retries -= 1
                self.wait(1)
        # If it hasn't been found by now, try one more time and let the exception through
        return self.selenium.find_element_by_id(elem_id)

    def assert_email_body_contains(self, email, text):
        pattern = re.escape(text)
        matches = re.search(pattern, email.body)
        self.assertTrue(matches, "Email does not contain " + text)

    def get_page_source(self):
        try:
            return self.selenium.page_source
        except:
            while True:
                self.wait(0.2)
                try:
                    self.selenium.switch_to_alert().accept()
                except:
                    return self.selenium.page_source

    def assertTrue(self, value, msg):
        if not value:
            raise AssertionError(msg)
        return

    def assertEqual(self, vala, valb):
        if vala != valb:
            msg = 'FAIL: "{0}" != "{1}"'.format(vala, valb)
            raise AssertionError(msg)
        return


    def assert_page_has_content(self, string):
        page_source = self.get_page_source()
        pattern = re.escape(string)
        self.assertTrue((string in page_source) or re.search(pattern, page_source), "page source did not contain %s" % pattern)
        
    def assert_page_does_not_contain(self, string):
        page_source = self.get_page_source()
        
        pattern = re.escape(string)
        self.assertFalse(re.search(pattern, page_source), "page source contained %s" % pattern)
        
    def assert_element_text_equals(self, selector, expected_value):
        text = self.find_visible_element(selector).text.strip()
        self.assertEqual(expected_value.strip(), text.strip())

    def assert_element_value_equals(self, selector, expected_value):
        text = self.find_visible_element(selector).get_attribute('value')
        self.assertEqual(expected_value.strip(), text.strip())

    def assert_selector_texts_equals_expected_values(self, selector_value):
        # selector_value is a dict of selectors to expected text values
        for selector, expected_value in selector_value.items():
            self.assert_element_text_equals(selector, unicode(expected_value))
    
    def assert_attribute_equals(self, attribute, selector_values):
        # selector_values is a dict of selectors to attribute values
        for selector, expected_value in selector_values.items():
            element = self.find_visible_element(selector)
            actual_value = element.get_attribute(attribute)
            self.assertEqual(expected_value, actual_value)

    def assert_is_checked(self, selector):
        field = self.selenium.find_element_by_css_selector(selector)
        self.assertEqual('true', field.get_attribute('checked'))

    def assert_is_unchecked(self, selector):
        field = self.selenium.find_element_by_css_selector(selector)
        self.assertIsNone(field.get_attribute('checked'))

    def assert_is_enabled(self, selector):
        field = self.selenium.find_element_by_css_selector(selector)
        self.assertIsNone(field.get_attribute('disabled'))
        
    def assert_is_disabled(self, selector):
        field = self.selenium.find_element_by_css_selector(selector)
        self.assertEqual('true', field.get_attribute('disabled'))

    def assert_are_displayed(self, name):
        fields = self.selenium.find_elements_by_name(name)
        self.assertTrue([field.is_displayed() for field in fields])

    def assert_are_displayed_by_class_name(self, name):
        fields = self.selenium.find_elements_by_class_name(name)
        self.assertTrue([field.is_displayed() for field in fields])

    def assert_are_not_displayed(self, name):
        fields = self.selenium.find_elements_by_name(name)
        self.assertFalse(all([field.is_displayed() for field in fields]))

    def assert_is_displayed(self, selector):
        field = self.selenium.find_element_by_css_selector(selector)
        self.assertTrue(field.is_displayed())
        
    def assert_not_displayed(self, selector):
        field = self.selenium.find_element_by_css_selector(selector)
        self.assertFalse(field.is_displayed())

    def assert_not_in_page(self, selector):
        "Assert that the supplied selector is not part of the page content"
        elements = self.selenium.find_elements_by_css_selector(selector)
        self.assertTrue(len(elements) == 0)

    def assert_on_page(self, url_name, ignore_query_string=False):
        retries = 30
        while retries > 0:
            try:
                self._assert_on_page(url_name, ignore_query_string)
                return
            except AssertionError:
                retries -= 1
                print "assert_on_page: retry"
                self.wait(1)
        self._assert_on_page(url_name, ignore_query_string)

    def _assert_on_page(self, url_name, ignore_query_string=False):
        if not ignore_query_string:
            self.assertEqual(self.selenium.current_url, self.get_full_url(url_name))
        else:
            split_url = self.selenium.current_url.split('?')
            url = split_url[0]
            self.assertEqual(url, self.get_full_url(url_name))

    def assert_multi_selected_text_equals(self, id_of_select, expected):
        actual = self.get_multi_selected_option_text(id_of_select)
        remaining = []
        for value in expected:
            if value not in actual:
                remaining.append(value)
            else:
                actual.remove(value)
        self.assertTrue(not actual and not remaining)

    def assert_summary_field_correctly_shown(self, expected_value, form_name, field_name):
        value_displayed = self.get_summary_field_text(form_name, field_name)
        self.assertEqual(expected_value, strip_tags(value_displayed))

    def fill_in_fields(self, field_data, id_wrap=None, clear=False):
        for selector, text_to_input in field_data.items():
            if id_wrap:
                selector = id_wrap(selector)
            elem = self.selenium.find_element_by_css_selector(selector)
            if elem.tag_name == 'select':
                self.select(selector, str(text_to_input))
            else:
                if clear:
                    elem.clear()
                elem.send_keys(str(text_to_input))
        self.wait(0.5)

    def clear(self, selector):
        elem = self.selenium.find_element_by_css_selector(selector)
        elem.clear()

    def click(self, elem_id):
        elem = self.find_element_by_id(elem_id)
        elem.click()
        self.wait(0.5)

    def click_by_css(self, element_css):
        elem = self.selenium.find_element_by_css_selector(element_css)
        elem.click()
        self.wait(0.5)

    def click_by_class_name(self, class_name):
        elem = self.selenium.find_element_by_class_name(class_name)
        elem.click()
        self.wait(0.5)

    def login(self, username, password):
        self.visit('accounts/login')

        username_input = self.selenium.find_element_by_id('id_username')
        password_input = self.selenium.find_element_by_id('id_password')
        submit_button = self.selenium.find_element_by_tag_name('button')  # TODO make this more specific

        username_input.send_keys(username)
        password_input.send_keys(password)

        submit_button.submit()
        
    def visit(self, url_name, *args, **kwargs):
        """ self.visit(name_of_url_as_defined_in_your_urlconf) """
        self.selenium.get(self.get_full_url(url_name, *args, **kwargs))
        if url_name in LiveServerTest.AJAX_WAIT:
            self.wait(2)
            self.assertTrue(self.selenium.execute_script('return (window.catalogue !== undefined ? catalogue._loaded : true)'),
                            'catalogue.js loading error')

    def get_actual_filter_options(self):
        option_selector = '%s option' % self.rf_id('filter')
        return [x.get_attribute('value').encode('ascii') for x in self.selenium.find_elements_by_css_selector(option_selector)]
    
    def get_expected_filter_options(self, data_set):
        def gen_bp_pairs(objs):
            for obj in objs:
                yield ('B-' + str(obj.id) + '_apparent')
                yield ('B-' + str(obj.id) + '_absolute')
        normal_parameters = datasets.filter_choices(data_set.simulation.id, data_set.galaxy_model.id)
        bandpass_parameters = datasets.band_pass_filters_objects()
        return ['D-' + str(x.id) for x in normal_parameters] + [pair for pair in gen_bp_pairs(bandpass_parameters)]

    def get_actual_snapshot_options(self):
        option_selector = '%s option' % self.lc_id('snapshot')
        return [x.get_attribute("innerHTML") for x in self.selenium.find_elements_by_css_selector(option_selector)]

    def get_expected_snapshot_options(self, snapshots):
        return [str("%.5g" % snapshot.redshift) for snapshot in snapshots]
        
    def get_full_url(self, url_name, *args, **kwargs):
        return "%s%s" % (self.job_params.BASE_URL, url_name)
    
    def get_selected_option_text(self, id_of_select):
        select = self.selenium.find_element_by_css_selector(id_of_select)
        options = select.find_elements_by_css_selector('option')
        selected_option = None
        for option in options:
            if option.get_attribute('selected'):
                selected_option = option
        return selected_option.text

    def get_multi_selected_option_text(self, id_of_select):
        select = self.selenium.find_element_by_css_selector(id_of_select)
        options = select.find_elements_by_css_selector('option')
        return [option.text for option in options]

        
    def get_selector_value(self, selector): 
        return self.selenium.find_element_by_css_selector(selector).get_attribute('value')
    
    def select(self, selector, value):
        from selenium.webdriver.support.ui import Select

        elem = self.selenium.find_element_by_css_selector(selector)
        select = Select(elem)

        select.select_by_visible_text(value)
        
    def find_visible_elements(self, css_selector):
        elements = self.selenium.find_elements_by_css_selector(css_selector)
        return [elem for elem in elements if elem.is_displayed()]
    
    def find_visible_element(self, css_selector):
        elements = self.find_visible_elements(css_selector)
        num_elements = len(elements)
        if num_elements != 1:
            raise Exception("Found %s elements for selector %s" % (num_elements, css_selector))
        return elements[0]
    
    def select_dark_matter_simulation(self, simulation):
        self.select(self.lc_id('dark_matter_simulation'), simulation.name)
        self.wait(0.5)
        
    def select_galaxy_model(self, galaxy_model):
        self.select(self.lc_id('galaxy_model'), galaxy_model.name)
        self.wait(0.5)

    def select_stellar_model(self, stellar_model):
        self.select(self.sed_id('single_stellar_population_model'), stellar_model.label)
        self.wait(0.5)

    def select_record_filter(self, filter, extension=None):
        text = ''
        if isinstance(filter, DataSetProperty):
            units_str = ''
            if filter.units is not None and len(filter.units) > 0:
                units_str = ' (' + filter.units + ')'
            text = filter.label + units_str
        elif isinstance(filter, BandPassFilter):
            text = filter.label
            if extension is not None:
                text += ' (' + extension.capitalize() + ')'
        else:
            raise TypeError("Unknown filter type")
        self.select(self.rf_id('filter'), text)
        
    #a function to make a list of list of text inside the table
    def table_as_text_rows(self, selector):
        table = self.selenium.find_element_by_css_selector(selector)
        rows = table.find_elements_by_css_selector('tr')
        cells = [[cell.text for cell in row.find_elements_by_css_selector('th, td')] for row in rows]
        return cells

    def submit_support_form(self):
        submit_button = self.selenium.find_element_by_css_selector('button[type="submit"]')
        submit_button.submit()
    driver.find_element_by_xpath("//ul[@class='ari-caused-spacer-expand']/li[3]").click()
    driver.find_element_by_id("arisearch_txtSearch").click()
    driver.find_element_by_id("arisearch_txtSearch").clear()
    driver.find_element_by_id("arisearch_txtSearch").send_keys("209617GS")
    driver.find_element_by_id("arisearch_btnLookup").click()

    # The following div classes get in the way of our user click as seen in errors
    # and we must change the css with javascript to allow the button click without a sleep statement
    # <div class="blockUI blockOverlay" ... wait; position: absolute;"></div>

    try:
        hideElement = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "blockUI")))
    except Exception, e:
        print("BlockUI not found")
    try:
        driver.execute_script("document.getElementsByClassName('blockUI blockOverlay')[0].style.height = '1px';")
    except Exception, e:
        print('blocking class not there')

    # The curtain <div> also prevents us from click so that is handled by a wait statement
    check = WebDriverWait(driver, 10).until_not(EC.presence_of_element_located((By.ID, "curtain")))

    # Add item 209617GS to cart
    WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.CSS_SELECTOR, "input.ariPartListAddToCart.ariImageOverride")))
    element = driver.find_element_by_xpath("//input[@id='aripartsSearch_btnCart0']")
    WebDriverWait(driver, 10).until_not(EC.presence_of_element_located((By.ID, "curtain")))
    driver.find_element_by_id('aripartsSearch_btnCart0').click()

    # Click My Cart(1) to load Shopping cart page
    wait = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.LINK_TEXT, "My Cart(1)")))
Example #21
0
def scroll_to_and_hover_over_element(browser: webdriver.WebDriver,
                                     element: WebElement):
    logging.info(f"Scrolling to and hovering over element {element}")
    browser.execute_script("arguments[0].scrollIntoView();", element)
    actions = ActionChains(browser)
    actions.move_to_element(element).perform()
Example #22
0
def dictwebpage(request):
    # case 1:  (OUTDATED since Glosbe doesn't work anymore) It needs to fetch an API.
    # AJAX has fetched the JSON on the wwww, then
    # the JSON obj from the www is sent to the view dictwebpage which processes it and
    # and sends back html.
    if 'json_obj' in request.GET.keys():
        parsed_json_obj = json.loads(request.GET['json_obj'])
        return render(request, 'lwt/_glosbe_api.html',
                      {'result': parsed_json_obj})

    # case 2:  AJAX sends the link to process to the view dictwebpage,
    # and the view sends backs a JSON containing the string URL. <iframe> displays it then.
    else:
        word = request.GET['word']
        word_escaped = urllibparsequote(word)
        wbl = request.GET['wbl']

        # case where it's a lookup sentence:
        if 'issentence' in request.GET.keys() and request.GET[
                'issentence'] != '':  # no key "issentence" is sent if the value of 'issentence' is empty in AJAX
            wo_id = int(request.GET['issentence'])
            word = Sentences.objects.values_list(
                'sentencetext', flat=True).get(sentence_having_this_word=wo_id)
            word_escaped = urllibparsequote(word)
        finalurl = wbl.replace('<WORD>', word_escaped)
        #         finalurl = createTheDictLink(wbl, word) # create the url of the dictionary, integrating the searched word

        # case where we can't put the url in an iframe src. we must request the entire html webpage
        # and will display it in the iframe srcdoc

        if finalurl[0] == '^' or finalurl[
                0] == '!':  # case where we open into the frame
            #             try: # check that the URL is working. else display a well-formed error
            headers = {
                "User-Agent":
                "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)",
                "Accept":
                "text/html,application/xhtml+xml,application/xml; q=0.9,image/webp,*/*;q=0.8"
            }
            reqest = Request(finalurl[1:], headers=headers)
            try:
                content = urlopen(reqest)
            # catch the redirect from Wiktionary
            except urllib.error.HTTPError as httpError:
                error = httpError.read().decode()
                # wiktionary has a special way to redirect to similar word if nothing found
                if 'wiktionary' in finalurl:
                    redirect_url = _wiki_API_redirect(error, finalurl[1:],
                                                      word_escaped)
                    reqest = Request(redirect_url, headers=headers)
                    try:
                        content = urlopen(reqest)
                    except:
                        content = error  # redirect doesn't work neither, so display the error
                else:
                    content = error

        if finalurl[0] == '^':
            try:
                soup = BeautifulSoup(content, 'html.parser')
                html = _clean_soup(soup, finalurl)
            except:
                html = render_to_string('lwt/dictwebpage_not_working.html')
            result_str = escape(html)
            return HttpResponse(json.dumps(result_str))

        if finalurl[
                0] == '#':  # case where we use Selenium (Tricky website where scrapping is bloked)
            # detect if mac or else
            system = platform.system().lower()
            if system == 'windows' or system == 'linux':
                is_Mac = False
            else:
                is_Mac = True

            from selenium.webdriver.firefox.webdriver import WebDriver
            from functional_tests.selenium_base import Base
            from selenium.webdriver.common.by import By
            from selenium.webdriver.firefox.options import Options as FirefoxOptions
            options = FirefoxOptions()
            options.add_argument("--headless")
            selenium = WebDriver(options=options)
            selenium.get('{}'.format(finalurl[1:]))
            base = Base()
            base.selenium = selenium
            if 'naver' in finalurl:
                base.wait_until_appear(By.ID, 'searchPage_entry')
            content = selenium.execute_script(
                "return document.documentElement.outerHTML;")

            if 'naver' in finalurl:
                translation_result = _naver_API(content, finalurl)
                context = {
                    'translation_result': translation_result,
                    'API_name': 'naver'
                }
            context['is_Mac'] = is_Mac

            return render(request, 'lwt/_translation_api.html', context)

        if finalurl[0] == '!' or finalurl[
                0] == '#':  # this dictionary uses my custom APIs (for ex. Google translate)

            context = {}
            # detect if mac or else
            system = platform.system().lower()
            if system == 'windows' or system == 'linux':
                is_Mac = False
            else:
                is_Mac = True

            if 'https://translate.google.com' in finalurl:
                translation_result = _google_API(content)
                context = {
                    'url': finalurl[1:],
                    'url_name': 'Google Translate',
                    'trans_item_nb': len(translation_result),
                    'translation_result': translation_result,
                    'word_OR_sentence_origin': word,
                    'is_Mac': is_Mac
                }
                return render(request, 'lwt/_google_api.html', context)
            if 'pons.com/translate' in finalurl:
                translation_result = _pons_API(content, finalurl)
                context = {
                    'translation_result': translation_result,
                    'API_name': 'pons'
                }
            if 'dict.cc' in finalurl:
                translation_result = _dictcc_API(content, finalurl)
                context = {
                    'translation_result': translation_result,
                    'API_name': 'dictcc'
                }
            if 'wordref' in finalurl:
                translation_result = _wordref_API(content, finalurl)
                context = {
                    'translation_result': translation_result,
                    'API_name': 'wordref'
                }
            if 'wiktionary' in finalurl:
                translation_result = _wiki_API(content, finalurl)
                context = {
                    'translation_result': translation_result,
                    'API_name': 'wiki'
                }
            if 'youdao' in finalurl:
                translation_result = _youdao_API(content, finalurl)
                context = {
                    'translation_result': translation_result,
                    'API_name': 'youdao'
                }
            context['is_Mac'] = is_Mac
            return render(request, 'lwt/_translation_api.html', context)

        return HttpResponse(
            json.dumps(finalurl))  # case where we open into a new window
Example #23
0
 def wait_until_ready(self):
     self.wait.until(lambda driver: WebDriver.execute_script(
         driver, 'return document.readyState;') == 'complete')