コード例 #1
0
def test_slowshot_with():
    disp = SmartDisplay(visible=False)
    py = Path(__file__).parent / ("slowgui.py")
    proc = EasyProcess([python, py])
    with disp:
        with proc:
            img = disp.waitgrab()
    assert img is not None
コード例 #2
0
 def test_slowshot_wrap(self):
     disp = SmartDisplay(visible=0)
     py = Path(__file__).parent / ("slowgui.py")
     proc = EasyProcess("python " + py)
     with disp:
         with proc:
             img = disp.waitgrab()
     eq_(img is not None, True)
コード例 #3
0
def test_slowshot():
    disp = SmartDisplay(visible=False).start()
    py = Path(__file__).parent / ("slowgui.py")
    proc = EasyProcess([python, py]).start()
    img = disp.waitgrab()
    proc.stop()
    disp.stop()
    assert img is not None
コード例 #4
0
 def test_slowshot_timeout(self):
     disp = SmartDisplay(visible=0)
     py = Path(__file__).parent / ("slowgui.py")
     proc = EasyProcess("python " + py)
     with disp:
         with proc:
             with self.assertRaises(DisplayTimeoutError):
                 img = disp.waitgrab(timeout=1)
コード例 #5
0
 def test_slowshot(self):
     disp = SmartDisplay(visible=0).start()
     py = Path(__file__).parent / ("slowgui.py")
     proc = EasyProcess("python " + py).start()
     img = disp.waitgrab()
     proc.stop()
     disp.stop()
     eq_(img is not None, True)
コード例 #6
0
def screenshot(cmd, fname):
    logging.info("%s %s", cmd, fname)
    # fpath = "docs/_img/%s" % fname
    # if os.path.exists(fpath):
    #     os.remove(fpath)
    with SmartDisplay() as disp:
        with EasyProcess(cmd):
            img = disp.waitgrab()
            img.save(fname)
コード例 #7
0
def prog_shot(cmd, f, wait, timeout, screen_size, visible, bgcolor, backend):
    '''start process in headless X and create screenshot after 'wait' sec.
    Repeats screenshot until it is not empty if 'repeat_if_empty'=True.

    wait: wait at least N seconds after first window is displayed,
    it can be used to skip splash screen

    :param wait: int
    '''
    # disp = SmartDisplay(visible=visible, size=screen_size, bgcolor=bgcolor)
    #'xvfb', 'xvnc' or 'xephyr',
    disp = SmartDisplay(visible=visible,
                        backend=backend,
                        size=screen_size,
                        bgcolor=bgcolor)
    proc = EasyProcess(cmd)

    def cb_imgcheck(img):
        """accept img if height > minimum."""
        rec = get_black_box(img)
        if not rec:
            return False
        left, upper, right, lower = rec
        accept = lower - upper > 30  # pixel
        log.debug('cropped img size=' + str((left, upper, right, lower)) +
                  ' accepted=' + str(accept))
        return accept

    # def func():
    #     if wait:
    #         proc.sleep(wait)
    #     try:
    #         img = disp.waitgrab(timeout=timeout, cb_imgcheck=cb_imgcheck)
    #     except DisplayTimeoutError as e:
    #         raise DisplayTimeoutError(str(e) + ' ' + str(proc))
    #     return img

    with disp:
        with proc:
            try:
                if wait:
                    proc.sleep(wait)
                img = disp.waitgrab(timeout=timeout, cb_imgcheck=cb_imgcheck)
            except DisplayTimeoutError as e:
                raise DisplayTimeoutError(str(e) + ' ' + str(proc))

    # img = disp.wrap(proc.wrap(func))()
    if img:
        bbox = get_black_box(img)
        assert bbox
        # extend to the left side
        bbox = (0, bbox[1], bbox[2], bbox[3])
        img = img.crop(bbox)

        img.save(f)
    return (proc.stdout, proc.stderr)
コード例 #8
0
def add_virtual_display_if_non_present():
    if 'DISPLAY' not in os.environ:
        # Necessary to display cartpole and other envs headlessly
        # https://stackoverflow.com/a/47968021
        from pyvirtualdisplay.smartdisplay import SmartDisplay

        display = SmartDisplay(visible=0, size=(1400, 900))
        display.start()
        display = os.environ['DISPLAY']

        print(f'>> ADDED VIRTUAL DISPLAY [{display}]')
コード例 #9
0
 def run(self, job):
     with SmartDisplay(visible=0) as disp:
         print "starting process"
         with EasyProcess(
                 'sensible-browser --user-data-dir=/tmp/ http://%s:80/' %
                 job):
             print "grabbing image"
             img = disp.waitgrab()
             print "am here"
             imgstr = img.tostring()
     print "got here"
     return imgstr
コード例 #10
0
    def setUp(self):
        if not os.environ.get('SHOW_SELENIUM'):
            self.display = SmartDisplay(visible=0, size=(1024, 768))
            self.display.start()

        remote_selenium = os.environ.get('REMOTE_SELENIUM')
        if not remote_selenium:
            self.driver = webdriver.Firefox()
        else:
            self.driver = webdriver.Remote(command_executor=remote_selenium,
                                           desired_capabilities={
                                               'browserName': 'unknown',
                                               'javascriptEnabled': True,
                                               'platform': 'ANY',
                                               'version': ''
                                           })
コード例 #11
0
def main():
    gendir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "gen")
    logging.info("gendir: %s", gendir)
    os.makedirs(gendir, exist_ok=True)
    empty_dir(gendir)
    pls = []
    try:
        os.chdir("gen")
        for cmd, grab, bg in commands:
            with SmartDisplay() as disp:
                logging.info("======== cmd: %s", cmd)
                fname_base = cmd.replace(" ", "_")
                fname = fname_base + ".txt"
                # logging.info("cmd: %s", cmd)
                print("file name: %s" % fname)
                with open(fname, "w") as f:
                    f.write("$ " + cmd + "\n")
                    if bg:
                        p = EasyProcess(cmd).start()
                    else:
                        p = EasyProcess(cmd).call()
                        f.write(p.stdout)
                        f.write(p.stderr)
                    pls += [p]
                if grab:
                    png = fname_base + ".png"
                    sleep(1)
                    img = disp.waitgrab(timeout=9)
                    logging.info("saving %s", png)
                    img.save(png)
    finally:
        os.chdir("..")
        for p in pls:
            p.stop()
    embedme = EasyProcess(["npx", "embedme", "../README.md"])
    embedme.call()
    print(embedme.stdout)
    assert embedme.return_code == 0
    assert not "but file does not exist" in embedme.stdout