Exemple #1
0
    def __init__(self, selenium, os, device, name=''):
        self._selenium = selenium
        self._os = os
        self._device = device
        self._name = name

        self._wait = CustomWait(self._selenium)
def page_visual_is_valid(selenium, selenium_generics: SeleniumGenerics, data_table):
    custom_wait = CustomWait(selenium)

    custom_wait.static_wait(2)
    selenium.execute_script(
        'window.scrollTo({ left: 0, top: 0});')
    custom_wait.static_wait(4)

    base_screenshot_dir = f"{pytest.globalDict['base_screenshot_dir']}/full_page/{data_table['page_name'][0]}_{selenium_generics._file_extension()}"
    actual_screenshot_dir = f"{pytest.globalDict['actual_screenshot_dir']}/full_page/{data_table['page_name'][0]}_{selenium_generics._file_extension()}"
    diff_screenshot_dir = f"{pytest.globalDict['diff_screenshot_dir']}/full_page/{data_table['page_name'][0]}_{selenium_generics._file_extension()}"

    device_offset = selenium.capabilities['deviceOffset'] \
        if 'deviceOffset' in selenium.capabilities else 0
    elements_to_hide = {
        "start": data_table['start'] if data_table['start'][0] != 'None' else None,
        "all": data_table['all'] if data_table['all'][0] != 'None' else None,
        "end": data_table['end'] if data_table['end'][0] != 'None' else None,
    }
    image_cv2 = selenium.get_full_page_screenshot_as_base64(elements_to_hide, device_offset)

    from utils.utils import compare_images
    base_score = 0.999
    score = compare_images(image_cv2, base_screenshot_dir, actual_screenshot_dir, diff_screenshot_dir, base_score)
    assert score >= base_score, f"Actual component screenshot is different from Base with {score}. Diff saved here: {diff_screenshot_dir}"
def take_element_screenshot_with_actions(selenium, selenium_generics, base_url, data_table):
    pytest.scenarioDict['screenshots'] = pytest.scenarioDict['screenshots'] \
        if 'screenshots' in pytest.scenarioDict else []

    custom_wait = CustomWait(selenium)

    actions = json.loads(data_table['actions'])
    if 'menu_button' in actions:
        selenium_generics.click(selector=actions['menu_button'])
        custom_wait.static_wait(4)

        actual_screenshot_dir = selenium_generics. \
            get_actual_screenshot_dir(base_url, base_url, 'megamenu_0', data_table)
        selenium.find_element(By.XPATH, actions['menu']).screenshot(actual_screenshot_dir)
        pytest.scenarioDict['screenshots'].append(actual_screenshot_dir)

    elements = selenium.find_elements(By.XPATH, actions['hover']) if 'hover' in actions \
        else selenium.find_elements(By.XPATH, actions['click']) if 'click' in actions \
        else []

    i = 1
    for element in elements:
        if 'hover' in actions:
            selenium_generics.hover(element=element)
            element = element.find_element(By.XPATH, actions['expanded'])
            custom_wait.static_wait(2)
            image_b = element.screenshot_as_base64
            element = selenium.find_element(By.XPATH, '//div[contains(@class, "bg-image")]')
            custom_wait.static_wait(2)
            image_a = element.screenshot_as_base64

            import base64
            from io import BytesIO
            from PIL import Image
            image_a = Image.open(BytesIO(base64.b64decode(image_a)))
            image_b = Image.open(BytesIO(base64.b64decode(image_b)))

            stitched_image = Image.new('RGB', (image_a.width, image_a.height + image_b.height))
            stitched_image.paste(im=image_a, box=(0, 0))
            stitched_image.paste(im=image_b, box=(0, image_a.height))

            i = i + 1
            actual_screenshot_dir = selenium_generics. \
                get_actual_screenshot_dir(base_url, base_url, f'megamenu_{i}', data_table)
            stitched_image.save(actual_screenshot_dir)
            pytest.scenarioDict['screenshots'].append(actual_screenshot_dir)

        if 'click' in actions:
            selenium_generics.click(element=element)
            custom_wait.static_wait(2)
            element = element.find_element(By.XPATH, actions['expanded'])
            actual_screenshot_dir = selenium_generics. \
                get_actual_screenshot_dir(base_url, base_url, f'megamenu_{i}', data_table)
            element.screenshot(actual_screenshot_dir)
            i = i + 1
            pytest.scenarioDict['screenshots'].append(actual_screenshot_dir)
            if 'click' in actions:
                selenium_generics.click(element=selenium.find_elements(By.XPATH, actions['click'])[0])
Exemple #4
0
    def __init__(self, selenium):
        self._os = re.sub(r"[\s]*", "", selenium.desired_capabilities['platformName'].lower()) \
            if 'platformName' in selenium.desired_capabilities else \
            re.sub(r"[\s]*", "", selenium.desired_capabilities['platform'].lower())
        self._device = 'mobile' if self._os in ['android', 'ios'
                                                ] else 'desktop'

        self._selenium = selenium
        self._custom_wait = CustomWait(selenium)
def take_full_page_screenshot_from_sitemap_with_actions(selenium, selenium_generics, base_url, data_table):
    pytest.scenarioDict['screenshots'] = pytest.scenarioDict['screenshots'] \
        if 'screenshots' in pytest.scenarioDict else []
    pytest.scenarioDict['pdf_file_name'] = \
        f"{pytest.globalDict['actual_screenshot_dir']}/full_page/{data_table['site']}_{selenium.get_window_size()['width']}_{selenium.get_window_size()['height']}.pdf"
    urls = pytest.scenarioDict['urls']
    custom_wait = CustomWait(selenium)

    for url in urls:
        response = requests.get(url, allow_redirects=False)
        if response.status_code >= 300:
            continue
        selenium.get(url)
        custom_wait.static_wait(10)

        page_size = selenium_generics.page_size()
        if page_size['height'] < 100 or page_size['width'] < 100:
            continue

        actual_screenshot_dir = selenium_generics.get_actual_screenshot_dir(url, base_url, '01', data_table)
        elements_to_hide = selenium_generics.get_elements_to_hide(data_table)
        device_offset = selenium.capabilities['deviceOffset'] if 'deviceOffset' in selenium.capabilities else 0

        if data_table['pre_action_screenshot'] == 'true':
            selenium.get_full_page_screenshot_as_png(actual_screenshot_dir, elements_to_hide, device_offset)
            pytest.scenarioDict['screenshots'].append(actual_screenshot_dir)
            base_screenshot_dir = actual_screenshot_dir

        for action in json.loads(data_table['actions']):
            selenium_generics.execute_action(action)

        custom_wait.static_wait(2)
        selenium.execute_script('window.scrollTo({ left: 0, top: 0});')
        custom_wait.static_wait(2)

        actual_screenshot_dir = selenium_generics.get_actual_screenshot_dir(url, base_url, '02', data_table)
        selenium.get_full_page_screenshot_as_png(actual_screenshot_dir, elements_to_hide, device_offset)

        if data_table['pre_action_screenshot'] == 'true':
            from utils.utils import compare_images
            base_score = 0.999
            score = compare_images(None, base_screenshot_dir, actual_screenshot_dir, None, base_score)
            if score >= base_score:
                import os
                os.remove(actual_screenshot_dir)
            else:
                pytest.scenarioDict['screenshots'].append(actual_screenshot_dir)
def element_not_exists(selenium, locators: Locators, locator_path):
    CustomWait(selenium).wait_for_element_not_visible(value=locators.parse_and_get(locator_path))
def element_not_selected(selenium, locators: Locators, locator_path):
    locator = locators.parse_and_get(locator_path)
    CustomWait(selenium).wait_for_element_visible(value=locator)
    assert_that(selenium.find_element(By.XPATH, locator).is_selected()).is_false()
def pause_execution(selenium, time):
    CustomWait(selenium).static_wait(int(time))
Exemple #9
0
def check_is_existing(selenium, locators: Locators, locator_path):
    CustomWait(selenium).wait_for_element_present(value=locators.parse_and_get(locator_path))
Exemple #10
0
def wait_for_displayed(selenium, locators: Locators, locator_path):
    CustomWait(selenium).wait_for_element_visible(value=locators.parse_and_get(locator_path))
Exemple #11
0
def check_element_enabled(selenium, locators: Locators, locator_path):
    locator = locators.parse_and_get(locator_path)
    CustomWait(selenium).wait_for_element_visible(value=locator)
    assert_that(selenium.find_element(By.XPATH, locator).is_enabled()).is_true()