Exemplo n.º 1
0
def test_create_file_name():
    assert (utils.create_file_name(bug_id='1661',
                                   browser='chrome',
                                   seq_no=None) == '1661_chrome')
    assert (utils.create_file_name(bug_id='1661',
                                   browser='chrome',
                                   width='20',
                                   height='10',
                                   seq_no=None) == '1661_H_20_V_10_chrome')
    assert (utils.create_file_name(bug_id='1661',
                                   browser='chrome',
                                   width='20',
                                   height='10',
                                   seq_no='2') == '1661_2_H_20_V_10_chrome')
Exemplo n.º 2
0
def get_coordinates(driver, bug_id, browser, seq_no):
    dom_tree = etree.HTML(
        driver.execute_script('return document.documentElement.outerHTML'))
    dom_element_tree = etree.ElementTree(dom_tree)
    loc_dict = {}
    dom_tree_elements = [elem for elem in dom_tree.iter(tag=etree.Element)]
    web_elements = driver.find_elements_by_css_selector('*')
    dom_xpaths = []

    for element in dom_tree_elements:
        dom_xpaths.append(dom_element_tree.getpath(element))

    for element in web_elements:
        xpath = driver.execute_script(get_xpath_script, element)

        if xpath in dom_xpaths:
            loc_dict[xpath] = element.size
            loc_dict[xpath].update(element.location)
            dom_xpaths.remove(xpath)

    for xpath in dom_xpaths:
        try:
            element = driver.find_element_by_xpath(xpath)
        except NoSuchElementException:
            continue
        loc_dict[xpath] = element.size
        loc_dict[xpath].update(element.location)

    file_name = 'loc_' + utils.create_file_name(
        bug_id=bug_id, browser=browser, seq_no=seq_no) + '.txt'
    file_name = os.path.join('data', file_name)
    with open(file_name, 'w') as f:
        json.dump(loc_dict, f)
Exemplo n.º 3
0
def get_domtree(driver, bug_id, browser, seq_no):
    file_name = 'dom_' + utils.create_file_name(
        bug_id=bug_id, browser=browser, seq_no=seq_no) + '.txt'
    file_name = os.path.join('data', file_name)
    with open(file_name, 'w', encoding='utf-8') as f:
        f.write(
            driver.execute_script('return document.documentElement.outerHTML'))
Exemplo n.º 4
0
def screenshot(driver, bug_id, browser, seq_no):
    WINDOW_HEIGHT = 732
    WINDOW_WIDTH = 412
    page_height = driver.execute_script('return document.body.scrollHeight;')
    page_width = driver.execute_script('return document.body.scrollWidth;')
    height = 0
    while height < page_height:
        width = 0
        while width < page_width:
            file_name = utils.create_file_name(bug_id=bug_id, browser=browser, width=str(width), height=str(height), seq_no=seq_no) + '.png'
            file_name = os.path.join('data', file_name)
            driver.execute_script('window.scrollTo(arguments[0], arguments[1]);', width, height)
            driver.get_screenshot_as_file(file_name)
            image = Image.open(file_name)
            image.save(file_name)
            width += WINDOW_WIDTH
        height += WINDOW_HEIGHT
Exemplo n.º 5
0
def migrate_v1_to_v2():

    def read_sequence(bug_id):
        with open('data/%d.txt' % bug_id) as f:
            return f.readlines()

    def write_sequence(bug_id, data):
        with open('./data/%d.txt' % bug_id, 'w') as f:
            for i in range(len(data)):
                for j in range(i + 1):
                    f.write(data[j])
                f.write('\n')

    all_data_files = os.listdir('data')
    label_files = os.listdir('label_persons')
    map_names = {}
    for i in range(7):
        map_names[i] = str(int((i + 1) * (i + 2) / 2 - 1))

    for f in all_data_files:
        if '.png' not in f:
            continue
        parts = f.split('_')

        if len(parts) <= 2:
            continue
        bug_id = parts[0]
        seq_no = int(parts[1])
        browser = parts[2]

        seq_no = map_names[seq_no]
        new_name = utils.create_file_name(bug_id=bug_id, browser=browser, seq_no=seq_no)
        os.rename(os.path.join('data', f), os.path.join('data', new_name))

    for f in all_data_files:
        if '.txt' not in f:
            continue
        bug_id = os.path.splitext(f)[0]
        data = read_sequence(int(bug_id))
        write_sequence(int(bug_id), data)

    for f in label_files:
        if 'csv' not in f:
            continue
        labels = utils.read_labels(os.path.join('label_persons', f))
        new_labels = {}

        for key, value in labels.items():
            key_info = utils.parse_file_name(key)

            if 'seq_no' not in key_info:
                new_labels[key] = value
                continue
            key = key.replace('_%d' % key_info['seq_no'], '_%s' % map_names[key_info['seq_no']])
            new_labels[key] = value
        utils.write_labels(new_labels, os.path.join('label_persons', f))

    for f in label_files:
        if 'json' not in f:
            continue

        bounding_boxes = utils.read_bounding_boxes(os.path.join('label_persons', f))
        new_bounding_boxes = {}

        for key, value in bounding_boxes.items():
            parts = key.split('_')

            if len(parts) <= 2:
                new_bounding_boxes[key] = value
                continue
            bug_id = parts[0]
            seq_no = int(parts[1])
            browser = parts[2]
            seq_no = map_names[seq_no]

            key = utils.create_file_name(bug_id=bug_id, browser=browser, seq_no=seq_no)
            new_bounding_boxes[key] = value
        utils.write_bounding_boxes(new_bounding_boxes, os.path.join('label_persons', f))