def prog_shot(cmd, f, wait, timeout, screen_size, visible, bgcolor, cwd=None): '''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 enable_crop: True -> crop screenshot :param wait: int ''' disp = SmartDisplay(visible=visible, size=screen_size, bgcolor=bgcolor) disp.pyscreenshot_backend = None disp.pyscreenshot_childprocess = True proc = EasyProcess(cmd, cwd=cwd) def func(): try: img = disp.waitgrab(timeout=timeout) except DisplayTimeoutError as e: raise DisplayTimeoutError(str(e) + ' ' + str(proc)) if wait: proc.sleep(wait) img = disp.grab() return img img = disp.wrap(proc.wrap(func))() if img: img.save(f) return (proc.stdout, proc.stderr)
def test_empty(self): disp = SmartDisplay(visible=0) proc = EasyProcess(sys.executable) with disp: with proc: with self.assertRaises(Exception): img = disp.waitgrab()
class Test(TestCase): def wait(self): self.screen.waitgrab() def setUp(self): self.screen = SmartDisplay() self.screen.start() self.p = None def tearDown(self): self.p.stop() self.screen.stop() # def test_empty(self): # self.p = EasyProcess('zenity --warning').start() # wnd is not ready # self.assertRaises(EmptyScreenException, tab_rectangles) # def test_zenity(self): # self.p = EasyProcess('zenity --warning').start() # self.wait() # ls = tab_rectangles() # self.assertEquals(len(ls), 2) def test_notab(self): self.p = EasyProcess('xmessage hi').start() self.wait() ls = tab_rectangles() self.assertEquals(len(ls), 0) def test_gmessage(self): self.p = EasyProcess('gmessage -buttons x,y,z hi').start() self.wait() ls = tab_rectangles() self.assertEquals(len(ls), 4)
def test_slowshot_wrap(self): disp = SmartDisplay(visible=0) py = path(__file__).parent / ('slowgui.py') proc = EasyProcess('python ' + py) f = disp.wrap(proc.wrap(disp.waitgrab)) img = f() eq_(img is not None, True)
def __init__(self, tor=False, tor_tries=10, save_directory="data", url_delay=10, random_pages=False, test=False): self.tor = tor self.tor_tries = tor_tries self.save_directory = save_directory self.url_delay = url_delay self.random_pages = random_pages self.sites = ["amazon", "apple", "nytimes", "google", "duckduckgo"] pathlib.Path(self.save_directory).mkdir(parents=True, exist_ok=True) self.display = SmartDisplay(backend='xephyr', visible=1, size=(1920, 1080)) self.display.start() self.firefox_profile_path = tempfile.mkdtemp() self.firefox_profile = webdriver.FirefoxProfile( self.firefox_profile_path) if self.tor: for i in range(self.tor_tries): try: self.socks_port = free_port() self.control_port = free_port() self.tor_data_dir = tempfile.mkdtemp() self.torrc = { 'ControlPort': str(self.control_port), 'SOCKSPort': str(self.socks_port), 'DataDirectory': self.tor_data_dir, 'EntryNodes': '{us}', 'ExitNodes': '{us}', 'StrictNodes': '1' } self.tor_process = launch_tor_with_config( config=self.torrc, tor_cmd="/usr/sbin/tor") except: print("Tor connection attempt {} failed.".format(i)) sys.stdout.flush() if i == self.tor_tries - 1: sys.exit() continue break self.firefox_profile.set_preference("network.proxy.type", 1) self.firefox_profile.set_preference("network.proxy.socks", "localhost") self.firefox_profile.set_preference("network.proxy.socks_port", self.socks_port) self.firefox_profile.set_preference("network.proxy.socks_version", 5) self.firefox_profile.update_preferences() self.driver = webdriver.Firefox(service_log_path=os.path.join( self.firefox_profile_path, "geckodriver.log"), firefox_profile=self.firefox_profile) self.driver.maximize_window() if test: self.driver.get("https://www.{}.com/".format( random.choice(self.sites))) time.sleep(self.url_delay)
def test_empty_nocrop(): disp = SmartDisplay(visible=False) proc = EasyProcess([python]) with disp: with proc: with pytest.raises(Exception): disp.waitgrab(autocrop=False, timeout=10)
def test_empty(): disp = SmartDisplay(visible=False) proc = EasyProcess([python]) with disp: with proc: with pytest.raises(Exception): disp.waitgrab()
class Test(TestCase): def wait(self): self.screen.waitgrab() def setUp(self): self.screen = SmartDisplay() self.screen.start() def tearDown(self): self.p.stop() self.screen.stop() # def test_empty(self): # self.p = EasyProcess('zenity --warning').start() # wnd is not ready # time.sleep(0.2) # self.assertRaises(EmptyScreenException, active_rectangles) def test_zenity(self): self.p = EasyProcess('zenity --warning').start() self.wait() ls = active_rectangles() self.assertEquals(len(ls), 1) def test_notab(self): self.p = EasyProcess('xmessage -buttons x,y,z hi').start() self.wait() ls = active_rectangles(grid=10) self.assertEquals(len(ls), 3) def test_gmessage(self): self.p = EasyProcess('gmessage -buttons x,y,z hi').start() self.wait() ls = active_rectangles() self.assertEquals(len(ls), 3)
def _start_simulation(self): sumo_cmd = [self._sumo_binary, '-n', self._net, '-r', self._route, '--max-depart-delay', str(self.max_depart_delay), '--waiting-time-memory', '10000', '--time-to-teleport', str(self.time_to_teleport)] if self.begin_time > 0: sumo_cmd.append('-b {}'.format(self.begin_time)) if self.sumo_seed == 'random': sumo_cmd.append('--random') else: sumo_cmd.extend(['--seed', str(self.sumo_seed)]) if not self.sumo_warnings: sumo_cmd.append('--no-warnings') if self.use_gui: sumo_cmd.extend(['--start', '--quit-on-end']) if self.virtual_display is not None: sumo_cmd.extend(['--window-size', f'{self.virtual_display[0]},{self.virtual_display[1]}']) from pyvirtualdisplay.smartdisplay import SmartDisplay print("Creating a virtual display.") self.disp = SmartDisplay(size=self.virtual_display) self.disp.start() print("Virtual display started.") if LIBSUMO: traci.start(sumo_cmd) self.sumo = traci else: traci.start(sumo_cmd, label=self.label) self.sumo = traci.getConnection(self.label) if self.use_gui: self.sumo.gui.setSchema(traci.gui.DEFAULT_VIEW, "real world")
def __init__(self, browser="chrome", executable_path=None): """ Initialization does two things: 1. Makes sure that there is active X session. 2. Starts browser. On initialization it stats X session if can't find 'DISPLAY' in os.environ. For this purposes used *pyvirtualdisplay* package. For handling browser used seleniumwrapper library. """ # lets start display in case if no is available self.hangout_id = None self.display = None if not os.environ.get('DISPLAY'): self.display = SmartDisplay() self.display.start() kwargs = {} if browser == "chrome": kwargs['executable_path'] = executable_path or CHROMEDRV_PATH self.browser = selwrap.create(browser, **kwargs) self.video = VideoSettings(self) self.microphone = MicrophoneSettings(self) self.audio = AudioSettings(self) self.bandwidth = BandwidthSettings(self)
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)
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)
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)
def test_slowshot_timeout(): disp = SmartDisplay(visible=False) py = Path(__file__).parent / ("slowgui.py") proc = EasyProcess([python, py]) with disp: with proc: with pytest.raises(DisplayTimeoutError): img = disp.waitgrab(timeout=1)
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
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
def test_disp(): with Display(): d = SmartDisplay(visible=True).start().stop() assert d.return_code == 0 d = SmartDisplay(visible=False).start().stop() assert d.return_code == 0
def cli_parse(): '''Parse command-line arguments''' options = {'title': None, 'desc': None, 'date': None, 'time': None, 'id': None} parser = argparse.ArgumentParser() parser.add_argument("action", help='''use "create" to create a new event\n "update" to update an event\n"details" to get event info''') # noqa parser.add_argument("--title", help="event title") parser.add_argument("--date", help="event date") parser.add_argument("--id", help="event id") parser.add_argument("--desc", help="event description") parser.add_argument("--filedesc", help="path to txt file w/ event description", # noqa type=argparse.FileType('r')) parser.add_argument("--otp", help="2-step verification code") parser.add_argument("--show", help="1 to display the browser, 0 for invisible virtual display", # noqa default=0, type=int) try: args = parser.parse_args() except: parser.print_help() exit(1) disp = None if not args.show: # set up invisible virtual display from pyvirtualdisplay.smartdisplay import SmartDisplay try: disp = SmartDisplay(visible=0, bgcolor='black').start() atexit.register(disp.stop) except: if disp: disp.stop() raise if args.title: options['title'] = args.title if args.desc: options['desc'] = args.desc elif args.filedesc: options['desc'] = args.filedesc.read() if args.date: options['date'] = dtparser.parse(args.date).strftime('%Y-%m-%d') options['time'] = dtparser.parse(args.date).strftime('%I:%M %p') if args.id: options['id'] = args.id if args.otp: options['otp'] = args.otp options['action'] = args.action return options
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)
def test_disp(self): vd = SmartDisplay().start() # d = SmartDisplay(visible=1).start().sleep(2).stop() # self.assertEquals(d.return_code, 0) d = SmartDisplay(visible=0).start().stop() self.assertEquals(d.return_code, 0) vd.stop()
def test_disp(self): vd = SmartDisplay().start() d = SmartDisplay(visible=1).start().sleep(2).stop() self.assertEquals(d.return_code, 0) d = SmartDisplay(visible=0).start().stop() self.assertEquals(d.return_code, 0) vd.stop()
def test_double(): with SmartDisplay(visible=False, bgcolor="black") as disp: with EasyProcess(["xmessage", "hello1"]): img = disp.waitgrab() assert img is not None with SmartDisplay(visible=False, bgcolor="black") as disp: with EasyProcess(["xmessage", "hello2"]): img = disp.waitgrab() assert img is not None
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}]')
class CuiJsTestCase(LiveServerTestCase): def setUp(self): # See https://code.djangoproject.com/ticket/10827 from django.contrib.contenttypes.models import ContentType ContentType.objects.clear_cache() 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': '' } ) def tearDown(self): if hasattr(self, 'driver'): self.driver.quit() if hasattr(self, 'display'): self.display.stop() def wait_until_expr(self, expr, timeout=60): WebDriverWait(self.driver, timeout).until( lambda driver: driver.execute_script('return (%s)' % expr)) def test(self): sys.stderr.write('\n\nRunning candidate UI unit tests...\n') sys.stderr.flush() tests_url = self.live_server_url + reverse('cui_test') jasmine_spec = os.environ.get('JASMINE_SPEC') if jasmine_spec: tests_url += "?spec={}".format(urlquote(jasmine_spec)) self.driver.get(tests_url) self.wait_until_expr('window.seleniumReporter') runner = SeleniumRunner(self.driver, sys.stderr) success = runner.run() self.assertTrue(success, 'JS tests failed. See full report on stderr')
def convert_html(source, destination, delay=0): print(source, destination, delay) with SmartDisplay(visible=0, bgcolor='black') as disp: subprocess.run([ 'cutycapt', '--url="{}"'.format(source), '--out={}'.format(destination), '--delay={}'.format(delay) ])
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': '' })
def set_virtual_display(self): if is_windows(): self.log.warning( "Cannot have virtual display on Windows, ignoring") return if self.engine in VirtualDisplay.SHARED_VIRTUAL_DISPLAY: self.virtual_display = VirtualDisplay.SHARED_VIRTUAL_DISPLAY[ self.engine] else: width = self.parameters.get("width", 1024) height = self.parameters.get("height", 768) self.virtual_display = Display(size=(width, height)) msg = "Starting virtual display[%s]: %s" self.log.info(msg, self.virtual_display.size, self.virtual_display.new_display_var) self.virtual_display.start() # roll DISPLAY back for online report browser if self.virtual_display.old_display_var: os.environ["DISPLAY"] = self.virtual_display.old_display_var else: del os.environ["DISPLAY"] VirtualDisplay.SHARED_VIRTUAL_DISPLAY[ self.engine] = self.virtual_display self.engine.shared_env.set( {"DISPLAY": self.virtual_display.new_display_var})
def main(): 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) 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
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 in commands: with SmartDisplay(): logging.info("cmd: %s", cmd) fname_base = cmd.replace(" ", "_").replace("/", "_").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") p = EasyProcess(cmd).call() f.write(p.stdout) f.write(p.stderr) pls += [p] 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 "but file does not exist" not in embedme.stdout
def prog_shot(cmd, f, wait, timeout, screen_size, visible, bgcolor): '''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) 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, e: raise DisplayTimeoutError(str(e) + ' ' + str(proc)) return img
def create_driver( cls, platform=environment_config['platform'], profile_directory_path: Optional[str] = None, display_size: Optional[Tuple[int, int]] = (1024, 768) ) -> any: if platform == 'docker': from pyvirtualdisplay.smartdisplay import SmartDisplay cls.display = SmartDisplay(visible=0, size=display_size) cls.display.start() firefox_profile = webdriver.FirefoxProfile() firefox_profile.set_preference('browser.download.folderList', 2) firefox_profile.set_preference( 'browser.download.manager.showWhenStarting', False) firefox_profile.set_preference('browser.download.dir', os.getcwd()) firefox_profile.set_preference( 'browser.helperApps.neverAsk.saveToDisk', 'text/csv, application/zip') return webdriver.Firefox(firefox_profile=firefox_profile) else: cls._add_geckodriver_to_path() path = profile_directory_path if profile_directory_path else interactor_config[ 'geckodriver_profile_path'] firefox_profile = webdriver.FirefoxProfile(path) firefox_profile.set_preference('browser.download.folderList', 2) firefox_profile.set_preference( 'browser.download.manager.showWhenStarting', False) firefox_profile.set_preference('browser.download.dir', os.getcwd()) firefox_profile.set_preference( 'browser.helperApps.neverAsk.saveToDisk', 'text/csv, application/zip') return webdriver.Firefox(firefox_profile)
class CuiJsTestCase(StaticLiveServerTestCase): def setUp(self): # See https://code.djangoproject.com/ticket/10827 from django.contrib.contenttypes.models import ContentType ContentType.objects.clear_cache() 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': '' }) def tearDown(self): if hasattr(self, 'driver'): self.driver.quit() if hasattr(self, 'display'): self.display.stop() def wait_until_expr(self, expr, timeout=60): WebDriverWait(self.driver, timeout).until( lambda driver: driver.execute_script('return (%s)' % expr)) def test(self): sys.stderr.write('\n\nRunning candidate UI unit tests...\n') sys.stderr.flush() tests_url = self.live_server_url + reverse('cui_test') jasmine_spec = os.environ.get('JASMINE_SPEC') if jasmine_spec: tests_url += "?spec={}".format(urlquote(jasmine_spec)) self.driver.get(tests_url) self.wait_until_expr('window.seleniumReporter') runner = SeleniumRunner(self.driver, sys.stderr) success = runner.run() self.assertTrue(success, 'JS tests failed. See full report on stderr')
def check_double(self, backend1, backend2=None): if not backend2: backend2 = backend1 with SmartDisplay(visible=0, bgcolor='black') as disp: disp.pyscreenshot_backend = backend1 disp.pyscreenshot_childprocess = True # error if FALSE with EasyProcess('xmessage hello1'): img = disp.waitgrab() eq_(img is not None, True) with SmartDisplay(visible=0, bgcolor='black') as disp: disp.pyscreenshot_backend = backend2 disp.pyscreenshot_childprocess = True # error if FALSE with EasyProcess('xmessage hello2'): img = disp.waitgrab() eq_(img is not None, True)
def get_display(): kwargs = { 'visible': 0, 'bgcolor': 'black', 'size': (1920, 1080), } with SmartDisplay(**kwargs) as display: yield display
def AddDisplay(): global disps, easyprocs, sshconns numdispwins = len(disps) if numdispwins > 0: numdispwins += 1 # this hack, so we have first top left, second bottom left, third bottom right mypos = ((numdispwins % 2) * wdeskhalf, get_bit(numdispwins, 1) * hdeskhalf) disp = SmartDisplay(visible=1, size=(wdeskhalf, hdeskhalf), position=mypos).start() print("** AddDisplay is: " + os.environ['DISPLAY'] + " " + os.environ['MATE_DESKTOP_SESSION_ID'] + " " + os.environ['DESKTOP_SESSION'] + " " + os.environ['XDG_CURRENT_DESKTOP']) disps.append((disp, os.environ['DISPLAY'])) # #mycmd='bash -c "echo AAA >> /tmp/test.log"' # shell redir has to be called like this! # # unfortunately, we cannot just call `nemo` here like the usual: # - it will take over the first Xephyr window as desktop manager, and all subsequent `nemo`s will open there; # however, we can use SSH X11 forwarding, which seems to fix that: # however, we must have mate-session (or gnome-session) ran before that; # else the wmctrl "cannot get client list properties"; mate-panel is not enough, but marco is (and it keeps small fonts - but adds titlebars) mycmd = 'ssh -XfC -c blowfish {}@localhost marco --replace --no-composite'.format( curuser) # -F ssh.config #mycmd='ssh -XfC -c blowfish {}@localhost tinywm'.format(curuser) # -F ssh.config # tinywm is so tiny, lists of windows are not managed print("mycmd: {}".format(mycmd)) #gscmdproc = EasyProcess(mycmd).start() #easyprocs.append(gscmdproc) ssh_cmd = pexpect.spawn(mycmd) ssh_cmd.expect('password: '******'t wait after this for EOF? time.sleep(0.25) ssh_cmd.expect(pexpect.EOF) sshconns.append(ssh_cmd) time.sleep(0.1) # mycmd = 'giggle /tmp' print("mycmd: {}".format(mycmd)) gigglecmdproc = EasyProcess(mycmd).start() easyprocs.append(gigglecmdproc) time.sleep(0.1) # mycmd = 'pcmanfm /tmp' print("mycmd: {}".format(mycmd)) pcmancmdproc = EasyProcess(mycmd).start() easyprocs.append(pcmancmdproc) time.sleep(0.1) # mycmd = [ 'gnome-terminal', '--working-directory=/tmp', '-e', r'bash -c "bash --rcfile <( echo source $HOME/.bashrc ; echo PS1=\\\"user@PC:\\\\[\\\\033[0\;33\;1m\\\\]\\\w\\\[\\\033[00m\\\]\\\\$ \\\" ) -i"' ] print("mycmd: {}".format(mycmd)) termcmdproc = EasyProcess(mycmd).start() easyprocs.append(termcmdproc)
class Test(TestCase): def wait(self): self.screen.waitgrab() def setUp(self): self.screen = SmartDisplay(visible=VISIBLE) self.screen.start() self.p = None def tearDown(self): self.p.stop() self.screen.stop() def test_zenity(self): self.p = EasyProcess('zenity --warning').start() self.wait() send_key('\n') self.assertFalse(getbbox(grab())) self.p = EasyProcess('zenity --warning').start() self.wait() send_key_list(['\n']) self.assertFalse(getbbox(grab())) self.p = EasyProcess('zenity --warning').start() self.wait() send_key(' ') self.assertFalse(getbbox(grab())) self.p = EasyProcess('zenity --warning').start() self.wait() send_key('x') self.assertTrue(getbbox(grab())) def test_gcalctool1(self): self.p = EasyProcess('gnome-calculator').start() self.wait() focus_wnd() send_key('ctrl+q') time.sleep(1) # img_debug(grab(), 'ctrl+q') self.assertFalse(getbbox(grab()))
class RealBrowser(webdriver.Firefox): def __init__(self, timeout=30): self._abstract_display = SmartDisplay(visible=0) self._abstract_display.start() super(RealBrowser, self).__init__() self.implicitly_wait(timeout) def quit(self): super(RealBrowser, self).quit() self._abstract_display.stop() @timeout(REQUEST_TIMEOUT) def _open(self, url): self.get(url) def open(self, url): try: self._open(url) return True except Exception, e: logger.error('network error for %s: %s', url, str(e))
class CuiJsTestCase(LiveServerTestCase): def setUp(self): if not os.environ.get('SHOW_SELENIUM'): self.display = SmartDisplay(visible=0, size=(1024, 768)) self.display.start() self.driver = webdriver.Firefox() def tearDown(self): if hasattr(self, 'driver'): self.driver.quit() if hasattr(self, 'display'): self.display.stop() def wait_until_expr(self, expr, timeout=60): WebDriverWait(self.driver, timeout).until( lambda driver: driver.execute_script('return (%s)' % expr)) def test(self): sys.stderr.write('\n\nRunning candidate UI unit tests...\n') sys.stderr.flush() self.driver.get(self.live_server_url+reverse('cui_test')) self.wait_until_expr('window.jsApiReporter !== undefined && window.jsApiReporter.finished') specs = self.driver.execute_script('return window.jsApiReporter.specs()') self.assertTrue(len(specs) > 0, 'No test results found! The tests probably contain syntax errors.') passed = True for spec in specs: sys.stderr.write(' %s ... %s\n' % (spec['fullName'], spec['status'])) if spec['status'] != 'passed': passed = False for exp in spec['failedExpectations']: sys.stderr.write(' %s\n' % exp['message']) sys.stderr.write('Access full report at %s\n\n' % reverse('cui_test')) sys.stderr.flush() self.assertTrue(passed, 'JS tests failed. See full report on stderr')
def set_virtual_display(self): display_conf = self.settings.get("virtual-display") if display_conf: if is_windows(): self.log.warning("Cannot have virtual display on Windows, ignoring") else: if self.engine in SeleniumExecutor.SHARED_VIRTUAL_DISPLAY: self.virtual_display = SeleniumExecutor.SHARED_VIRTUAL_DISPLAY[self.engine] else: width = display_conf.get("width", 1024) height = display_conf.get("height", 768) self.virtual_display = Display(size=(width, height)) msg = "Starting virtual display[%s]: %s" self.log.info(msg, self.virtual_display.size, self.virtual_display.new_display_var) self.virtual_display.start() SeleniumExecutor.SHARED_VIRTUAL_DISPLAY[self.engine] = self.virtual_display
class VirtualDisplay(Service, Singletone): """ Selenium executor :type virtual_display: Display """ SHARED_VIRTUAL_DISPLAY = None def __init__(self): super(VirtualDisplay, self).__init__() self.virtual_display = None def get_virtual_display(self): return self.virtual_display def set_virtual_display(self): if is_windows(): self.log.warning("Cannot have virtual display on Windows, ignoring") return if VirtualDisplay.SHARED_VIRTUAL_DISPLAY: self.virtual_display = VirtualDisplay.SHARED_VIRTUAL_DISPLAY else: width = self.parameters.get("width", 1024) height = self.parameters.get("height", 768) self.virtual_display = Display(size=(width, height)) msg = "Starting virtual display[%s]: %s" self.log.info(msg, self.virtual_display.size, self.virtual_display.new_display_var) self.virtual_display.start() VirtualDisplay.SHARED_VIRTUAL_DISPLAY = self.virtual_display self.engine.shared_env.set({'DISPLAY': os.environ['DISPLAY']}) # backward compatibility def free_virtual_display(self): if self.virtual_display and self.virtual_display.is_alive(): self.virtual_display.stop() if VirtualDisplay.SHARED_VIRTUAL_DISPLAY: VirtualDisplay.SHARED_VIRTUAL_DISPLAY = None def startup(self): self.set_virtual_display() def check_virtual_display(self): if self.virtual_display: if not self.virtual_display.is_alive(): self.log.info("Virtual display out: %s", self.virtual_display.stdout) self.log.warning("Virtual display err: %s", self.virtual_display.stderr) raise TaurusInternalException("Virtual display failed: %s" % self.virtual_display.return_code) def check(self): self.check_virtual_display() return False def shutdown(self): self.free_virtual_display()
def set_virtual_display(self): if is_windows(): self.log.warning("Cannot have virtual display on Windows, ignoring") return if VirtualDisplay.SHARED_VIRTUAL_DISPLAY: self.virtual_display = VirtualDisplay.SHARED_VIRTUAL_DISPLAY else: width = self.parameters.get("width", 1024) height = self.parameters.get("height", 768) self.virtual_display = Display(size=(width, height)) msg = "Starting virtual display[%s]: %s" self.log.info(msg, self.virtual_display.size, self.virtual_display.new_display_var) self.virtual_display.start() VirtualDisplay.SHARED_VIRTUAL_DISPLAY = self.virtual_display self.engine.shared_env.set({'DISPLAY': os.environ['DISPLAY']}) # backward compatibility
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': '' } )
def __init__(self, executable_path=None, chrome_options=None): self.hangout_id = None self.on_air = None # lets start display in case if no is available self.display = None if not os.environ.get('DISPLAY'): self.display = SmartDisplay() self.display.start() kwargs = {'executable_path': executable_path or CHROMEDRV_PATH} if chrome_options is not None: kwargs['chrome_options'] = chrome_options self.browser = selwrap.create('chrome', **kwargs) self.utils = Utils(self.browser) for name, instance in getUtilitiesFor(IModule): setattr(self, name, instance(self.utils))
def prepare(self): display_conf = self.settings.get("virtual-display") if display_conf: if is_windows(): self.log.warning("Cannot have virtual display on Windows, ignoring") else: width = display_conf.get("width", 1024) height = display_conf.get("height", 768) self.virtual_display = Display(size=(width, height)) self.scenario = self.get_scenario() self._verify_script() self.kpi_file = self.engine.create_artifact("selenium_tests_report", ".csv") self.err_jtl = self.engine.create_artifact("selenium_tests_err", ".xml") script_type = self.detect_script_type(self.scenario.get(Scenario.SCRIPT)) if script_type == ".py": runner_class = NoseTester runner_config = self.settings.get("selenium-tools").get("nose") elif script_type == ".jar" or script_type == ".java": runner_class = JUnitTester runner_config = self.settings.get("selenium-tools").get("junit") runner_config['props-file'] = self.engine.create_artifact("customrunner", ".properties") else: raise ValueError("Unsupported script type: %s" % script_type) runner_config["script-type"] = script_type self.runner_working_dir = self.engine.create_artifact(runner_config.get("working-dir", "classes"), "") runner_config["working-dir"] = self.runner_working_dir runner_config.get("artifacts-dir", self.engine.artifacts_dir) runner_config.get("working-dir", self.runner_working_dir) runner_config.get("report-file", self.kpi_file) runner_config.get("err-file", self.err_jtl) runner_config.get("stdout", self.engine.create_artifact("junit", ".out")) runner_config.get("stderr", self.engine.create_artifact("junit", ".err")) self._cp_resource_files(self.runner_working_dir) self.runner = runner_class(runner_config, self.scenario, self.get_load(), self.log) self.runner.prepare() self.reader = JTLReader(self.kpi_file, self.log, self.err_jtl) if isinstance(self.engine.aggregator, ConsolidatingAggregator): self.engine.aggregator.add_underling(self.reader)
def setUp(self): # See https://code.djangoproject.com/ticket/10827 from django.contrib.contenttypes.models import ContentType ContentType.objects.clear_cache() 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': '' } )
def set_virtual_display(self): if is_windows(): self.log.warning("Cannot have virtual display on Windows, ignoring") return if self.engine in VirtualDisplay.SHARED_VIRTUAL_DISPLAY: self.virtual_display = VirtualDisplay.SHARED_VIRTUAL_DISPLAY[self.engine] else: width = self.parameters.get("width", 1024) height = self.parameters.get("height", 768) self.virtual_display = Display(size=(width, height)) msg = "Starting virtual display[%s]: %s" self.log.info(msg, self.virtual_display.size, self.virtual_display.new_display_var) self.virtual_display.start() # roll DISPLAY back for online report browser if self.virtual_display.old_display_var: os.environ["DISPLAY"] = self.virtual_display.old_display_var else: del os.environ["DISPLAY"] VirtualDisplay.SHARED_VIRTUAL_DISPLAY[self.engine] = self.virtual_display self.engine.shared_env.set({"DISPLAY": self.virtual_display.new_display_var})
from easyprocess import EasyProcess from pyvirtualdisplay.smartdisplay import SmartDisplay disp = SmartDisplay(visible=0, bgcolor='black').start() xmessage = EasyProcess('xmessage hello').start() img = disp.waitgrab() xmessage.stop() disp.stop() img.show()
def setUp(self): self.screen = SmartDisplay() self.screen.start() self.p = None
class SeleniumExecutor(ScenarioExecutor, WidgetProvider, FileLister): """ Selenium executor :type virtual_display: Display :type runner: AbstractTestRunner """ SELENIUM_DOWNLOAD_LINK = "http://selenium-release.storage.googleapis.com/{version}/" \ "selenium-server-standalone-{version}.0.jar" SELENIUM_VERSION = "2.53" JUNIT_DOWNLOAD_LINK = "http://search.maven.org/remotecontent?filepath=junit/junit/{version}/junit-{version}.jar" JUNIT_VERSION = "4.12" JUNIT_MIRRORS_SOURCE = "http://search.maven.org/solrsearch/select?q=g%3A%22junit%22%20AND%20a%3A%22junit%22%20" \ "AND%20v%3A%22{version}%22&rows=20&wt=json".format(version=JUNIT_VERSION) HAMCREST_DOWNLOAD_LINK = "https://hamcrest.googlecode.com/files/hamcrest-core-1.3.jar" SUPPORTED_TYPES = [".py", ".jar", ".java"] SHARED_VIRTUAL_DISPLAY = {} def __init__(self): super(SeleniumExecutor, self).__init__() self.additional_env = {} self.virtual_display = None self.start_time = None self.end_time = None self.runner = None self.widget = None self.reader = None self.kpi_file = None self.err_jtl = None self.runner_working_dir = None self.scenario = None def set_virtual_display(self): display_conf = self.settings.get("virtual-display") if display_conf: if is_windows(): self.log.warning("Cannot have virtual display on Windows, ignoring") else: if self.engine in SeleniumExecutor.SHARED_VIRTUAL_DISPLAY: self.virtual_display = SeleniumExecutor.SHARED_VIRTUAL_DISPLAY[self.engine] else: width = display_conf.get("width", 1024) height = display_conf.get("height", 768) self.virtual_display = Display(size=(width, height)) msg = "Starting virtual display[%s]: %s" self.log.info(msg, self.virtual_display.size, self.virtual_display.new_display_var) self.virtual_display.start() SeleniumExecutor.SHARED_VIRTUAL_DISPLAY[self.engine] = self.virtual_display def free_virtual_display(self): if self.virtual_display and self.virtual_display.is_alive(): self.virtual_display.stop() if self.engine in SeleniumExecutor.SHARED_VIRTUAL_DISPLAY: del SeleniumExecutor.SHARED_VIRTUAL_DISPLAY[self.engine] def _get_script_path(self): script = self.scenario.get(Scenario.SCRIPT) if not script: return None return self.engine.find_file(script) def prepare(self): self.set_virtual_display() self.scenario = self.get_scenario() self._verify_script() self.kpi_file = self.engine.create_artifact("selenium_tests_report", ".csv") self.err_jtl = self.engine.create_artifact("selenium_tests_err", ".xml") script_path = self._get_script_path() script_type = self.detect_script_type(script_path) runner_config = BetterDict() if script_type == ".py": runner_class = NoseTester runner_config.merge(self.settings.get("selenium-tools").get("nose")) else: # script_type == ".jar" or script_type == ".java": runner_class = JUnitTester runner_config.merge(self.settings.get("selenium-tools").get("junit")) runner_config['props-file'] = self.engine.create_artifact("customrunner", ".properties") runner_config["script-type"] = script_type if self.runner_working_dir is None: self.runner_working_dir = self.engine.create_artifact(runner_config.get("working-dir", "classes"), "") runner_config["working-dir"] = self.runner_working_dir runner_config.get("artifacts-dir", self.engine.artifacts_dir) runner_config.get("working-dir", self.runner_working_dir) runner_config.get("report-file", self.kpi_file) runner_config.get("err-file", self.err_jtl) runner_config.get("stdout", self.engine.create_artifact("junit", ".out")) runner_config.get("stderr", self.engine.create_artifact("junit", ".err")) self._cp_resource_files(self.runner_working_dir) self.runner = runner_class(runner_config, self) self.runner.prepare() self.reader = JTLReader(self.kpi_file, self.log, self.err_jtl) if isinstance(self.engine.aggregator, ConsolidatingAggregator): self.engine.aggregator.add_underling(self.reader) def _verify_script(self): if not self.scenario.get(Scenario.SCRIPT): if self.scenario.get("requests"): self.scenario[Scenario.SCRIPT] = self.__tests_from_requests() else: raise RuntimeError("Nothing to test, no requests were provided in scenario") def _cp_resource_files(self, runner_working_dir): """ :return: """ script = self._get_script_path() if script is not None: if os.path.isdir(script): shutil.copytree(script, runner_working_dir) else: os.makedirs(runner_working_dir) shutil.copy2(script, runner_working_dir) @staticmethod def detect_script_type(script_path): """ checks if script is java or python if it's folder or single script :param script_path: str :return: """ if not isinstance(script_path, string_types) and not isinstance(script_path, text_type): raise ValueError("Nothing to test, no files were provided in scenario") if not os.path.exists(script_path): raise ValueError("Script %s doesn't exist" % script_path) file_types = set() if os.path.isfile(script_path): # regular file received file_types.add(os.path.splitext(script_path)[1].lower()) else: # dir received: check files for walk_rec in os.walk(script_path): for file_name in walk_rec[2]: file_types.add(os.path.splitext(file_name)[1].lower()) if '.java' in file_types: file_ext = '.java' elif '.py' in file_types: file_ext = '.py' elif '.jar' in file_types: file_ext = '.jar' else: raise ValueError("Unsupported script type: %s" % script_path) return file_ext def startup(self): """ Start runner :return: """ self.start_time = time.time() self.runner.env = self.additional_env self.runner.run_tests() def check(self): """ check if test completed :return: """ if self.widget: self.widget.update() if self.virtual_display: if not self.virtual_display.is_alive(): self.log.info("Virtual display out: %s", self.virtual_display.stdout) self.log.warning("Virtual display err: %s", self.virtual_display.stderr) raise RuntimeError("Virtual display failed: %s" % self.virtual_display.return_code) return self.runner.is_finished() def shutdown(self): """ shutdown test_runner :return: """ self.runner.shutdown() if self.start_time: self.end_time = time.time() self.log.debug("Selenium tests ran for %s seconds", self.end_time - self.start_time) def post_process(self): self.free_virtual_display() if self.reader and not self.reader.read_records: raise RuntimeWarning("Empty results, most likely Selenium failed") def get_widget(self): if not self.widget: self.widget = SeleniumWidget(self.scenario.get(Scenario.SCRIPT), self.runner.settings.get("stdout")) return self.widget def resource_files(self): if not self.scenario: self.scenario = self.get_scenario() if Scenario.SCRIPT not in self.scenario: return [] script_path = self._get_script_path() if os.path.isdir(script_path): files = next(os.walk(script_path)) resources = [os.path.join(files[0], f) for f in files[2]] else: resources = [script_path] return resources def __tests_from_requests(self): filename = self.engine.create_artifact("test_requests", ".py") nose_test = SeleniumScriptBuilder(self.scenario, self.log) if self.virtual_display: nose_test.window_size = self.virtual_display.size nose_test.gen_test_case() nose_test.save(filename) return filename
def onJoin(self, details): # SUBSCRIBE to a topic and receive events # self.zstoreFlag = 1 self.lastzValue = 0.0 self.zCounter = 0 self.ystoreFlag = 1 self.lastyValue = 0.0 def y_direction(IMUTable): if self.ystoreFlag == 1: self.lastyValue = abs( IMUTable['y']) self.ystoreFlag = 0 return curryValue = abs(IMUTable['y']) ydiff = self.lastyValue - curryValue gammaValue = IMUTable['gamma'] if abs(gammaValue) > 66: print "Z Stable" if ( curryValue > 1.5) and (abs(ydiff) > 0.4): print "Right" self.keyb.emit_click(uinput.KEY_V) if ( curryValue < 1.5) and (abs(ydiff) > 0.4): print "Left" self.keyb.emit_click(uinput.KEY_END) self.lastyValue = curryValue print "The diff is ",ydiff, abs(ydiff) def z_direction(IMUTable): if self.zstoreFlag == 1: self.lastzValue = IMUTable['z'] self.zstoreFlag = 0 return currzValue = IMUTable['z'] zdiff = self.lastzValue - currzValue gammaValue = IMUTable['gamma'] if abs(gammaValue) > 66: print "Z Stable" if ( currzValue > 1.5) and (abs(zdiff) > 0.4): print "Forward" self.keyb.emit_click(uinput.KEY_UP) if ( currzValue < 1.5) and (abs(zdiff) > 0.4): print "Backward" self.keyb.emit_click(uinput.KEY_DOWN) self.lastzValue = currzValue print "The diff is ",zdiff, abs(zdiff) def on_event(i): print("Got event: {}".format(i)) IMUTab = json.loads(i); print IMUTab['z'] z_direction(IMUTab) y_direction(IMUTab) #if self.storeFlag == 1: # self.lastzValue = IMUTab['z'] # self.storeFlag = 0 # return #currzValue = IMUTab['z'] #zdiff = self.lastzValue - currzValue yield self.subscribe(on_event, 'com.myapp.topic1') self.log.info("subscribed to topic 'onhello'") # REGISTER a procedure for remote calling # def add2(x, y): self.log.info("add2() called with {x} and {y}", x=x, y=y) return x + y yield self.register(add2, 'com.example.add2') self.log.info("procedure add2() registered") self.keyb = uinput.Device(self.events) self.disp = SmartDisplay(visible=1, size=(800,600)).start() # self.disp = SmartDisplay(visible=1, size=(640, 480) ).start() # self.command = EasyProcess('celestia -s -f').start() # self.command = EasyProcess('lightdm --test-mode').start() self.p = Popen(['celestia'], stdout=PIPE, stdin=PIPE, stderr=PIPE) # self.p = Popen(['xterm'], stdout=PIPE, stdin=PIPE, stderr=PIPE) # PUBLISH and CALL every second .. forever # counter = 0 sleep(10) while True: # stdout_data = p.communicate(input="\x1B[C")[0] # print stdout_data self.buffer = cStringIO.StringIO() #self.img = self.disp.wait() self.img = self.disp.waitgrab(timeout=0.1, autocrop=False) self.img.save(self.buffer, "JPEG") self.imgStr = base64.b64encode(self.buffer.getvalue()) yield self.publish('com.example.image', self.imgStr) # CALL a remote procedure # #try: # res = yield self.call('com.example.mul2', counter, 3) # self.log.info("mul2() called with result: {result}", # result=res) #except ApplicationError as e: # # ignore errors due to the frontend not yet having # # registered the procedure we would like to call # if e.error != 'wamp.error.no_such_procedure': # raise e #return #yield sleep(0) # self.keyb.emit_click(uinput.KEY_HOME) yield sleep(0)
def test_slowshot_timeout(self): disp = SmartDisplay(visible=0) py = path(__file__).parent / ('slowgui.py') proc = EasyProcess('python ' + py) f = disp.wrap(proc.wrap(lambda: disp.waitgrab(timeout=1))) self.assertRaises(DisplayTimeoutError, f)
def test_empty(self): disp = SmartDisplay(visible=0) proc = EasyProcess(sys.executable) f = disp.wrap(proc.wrap(disp.waitgrab)) self.assertRaises(Exception, f)
class Hangouts(object): """ Main class for controlling hangout calls. Initialization does two things: 1. Makes sure that there is active X session. 2. Starts the browser. If 'DISPLAY' can't be found in os.environ than new X session starts. Starting new session handels `PyVirtualDisplay`_. .. _PyVirtualDisplay: http://ponty.github.io/PyVirtualDisplay/ For handling browser used seleniumwrapper library. .. testsetup:: HangoutsBase import os os.environ['DISPLAY'] = '1' from hangout_api import Hangouts from hangout_api.tests.doctests_utils import DummySelenium import seleniumwrapper def get_attribute(self, name): if name == 'aria-label': return ' John Doe ' elif name == 'data-userid': return '108775712935' else: return 'hello' DummySelenium.get_attribute = get_attribute seleniumwrapper.create = DummySelenium .. testsetup:: HangoutsBase2 import os os.environ['DISPLAY'] = '1' from hangout_api import Hangouts from hangout_api.tests.doctests_utils import DummySelenium import seleniumwrapper DummySelenium.location = {'x': 1} seleniumwrapper.create = DummySelenium hangout = Hangouts() .. doctest:: HangoutsBase >>> hangout = Hangouts() """ def __init__(self, executable_path=None, chrome_options=None): self.hangout_id = None self.on_air = None # lets start display in case if no is available self.display = None if not os.environ.get('DISPLAY'): self.display = SmartDisplay() self.display.start() kwargs = {'executable_path': executable_path or CHROMEDRV_PATH} if chrome_options is not None: kwargs['chrome_options'] = chrome_options self.browser = selwrap.create('chrome', **kwargs) self.utils = Utils(self.browser) for name, instance in getUtilitiesFor(IModule): setattr(self, name, instance(self.utils)) def start(self, on_air=None): """ Start a new hangout. After new hangout is created its id is stored in 'hangout_id' attribure .. doctest:: HangoutsBase >>> hangout.start() >>> hangout.hangout_id 'gs4pp6g62w65moctfqsvihzq2qa' To start OnAir just pass on_air argument to 'start' method. .. doctest:: HangoutsBase >>> hangout.start( ... on_air={'name':'My OnAir', 'attendees':['Friends']}) >>> hangout.start(on_air='https://plus.google.com/events/df34...') """ # onair if on_air is not None: self.on_air = on_air if isinstance(on_air, dict): # in case if on_air is a dict create new hangout event _create_hangout_event( self.browser, on_air['name'], on_air['attendees']) else: # otherwise (hangout is a string) go to event page self.browser.get(on_air) # on event page, redirecting can take some time self.browser.xpath( '//div[@data-tooltip="Start the Hangout On Air"]', timeout=TIMEOUTS.extralong).click(TIMEOUTS.fast) for name, instance in getUtilitiesFor(IOnAirModule): setattr(self, name, instance(self.utils)) else: if not self.browser.current_url.startswith( URLS.hangouts_active_list): self.browser.get(URLS.hangouts_active_list) # G+ opens new window for new hangout, so we need to # switch selenium to it self.browser.by_text( 'Start a video Hangout').click(timeout=TIMEOUTS.fast) # waiting until new window appears tries_n_time_until_true( lambda: len(self.browser.window_handles) <= 1, try_num=100) # self.browser.close() # closing old window self.browser.switch_to_window(self.browser.window_handles[-1]) self.utils.click_cancel_button_if_there_is_one( timeout=TIMEOUTS.extralong) if self.on_air: # waiting for broadcasting to be ready broadcast_button = self.browser.by_text( 'Start broadcast', timeout=TIMEOUTS.long) tries_n_time_until_true(broadcast_button.is_displayed, try_num=600) # setting hangout id property self.hangout_id = self.browser.current_url.replace( URLS.hangout_session_base, '', 1).split('?', 1)[0] # @retry(stop_max_attempt_number=3) def connect(self, hangout_id): """ Connect to an existing hangout. Takes id of targeted hangout as argument. Also it sets hangout_id property: .. doctest:: HangoutsBase >>> hangout.connect('fnar4989hf9834h') >>> hangout.hangout_id 'fnar4989hf9834h' """ self.hangout_id = hangout_id self.browser.get(URLS.hangout_session_base + hangout_id) # there may be a big delay before 'Join' button appears, so there is a # need wait longer than usual join_button = self.browser.xpath( '//*[text()="Join" or text()="Okay, got it!"]', timeout=TIMEOUTS.long) button_text = names_cleaner(join_button.get_attribute('innerText')) if button_text == 'Okay, got it!': # to join hangout we need to set agreement checkbox self.browser.xpath('//*[@role="presentation"]').click( timeout=TIMEOUTS.fast) join_button.click(timeout=TIMEOUTS.average) self.browser.by_text('Join', timeout=TIMEOUTS.long).click( timeout=TIMEOUTS.fast) @retry(stop_max_attempt_number=3) def login(self, username, password, otp=None): """ Log in to google plus. *otp* argument is one time password and it's optional, set it only if you're using 2-factor authorization. .. doctest:: HangoutsBase >>> hangout.login('*****@*****.**', 'password') >>> hangout.login('*****@*****.**', 'password', otp='123456') """ # Open login form and sing in with credentials self.browser.get(URLS.service_login) self.browser.by_id('Email').send_keys(username) self.browser.by_id('Passwd').send_keys(password) self.browser.by_id('signIn').click(timeout=TIMEOUTS.fast) # filling up one time password if provides if otp: self.browser.by_id('smsUserPin').send_keys(otp) self.browser.by_id('smsVerifyPin').click(timeout=TIMEOUTS.fast) # checking if log in was successful if not self.utils.is_logged_in: raise LoginError( 'Wasn\'t able to login. Check if credentials are correct' 'and make sure that you have G+ account activated') @retry(stop_max_attempt_number=3) def invite(self, participants): """ Invite person or circle to hangout: .. doctest:: HangoutsBase2 >>> hangout.invite("*****@*****.**") >>> hangout.invite(["*****@*****.**", "Public", "Friends"]) """ self.utils.click_cancel_button_if_there_is_one() if not any(isinstance(participants, i) for i in (list, tuple)): participants = [participants, ] # click on Invite People button self.utils.click_menu_element('//div[@aria-label="Invite People"]') input_field = self.browser.xpath( '//input[@placeholder="+ Add names, circles, or email addresses"]') input_field.click() input_field.clear() for participant in participants: input_field.send_keys(participant) sleep(1) # need to wait a bit for HG to make request input_field.send_keys(Keys.RETURN) self.browser.by_text('Invite').click(timeout=TIMEOUTS.fast) # making sure that invitation is posted xpath = '//*[text()="Invitation posted"'\ 'or text()="Waiting for people to join this video call..."]' self.browser.xpath(xpath) @retry(stop_max_attempt_number=3) def participants(self): """ Returns list of namedtuples of current participants: .. doctest:: HangoutsBase >>> hangout.participants() [Participant(name='John Doe', profile_id='108775712935')] """ xpath = '//div[@data-userid]' # for some reason some persons can be listed twice, so let's # filter them with dict participants = { p.get_attribute('data-userid'): p.get_attribute( 'aria-label')[5:-11] for p in self.browser.xpath(xpath, eager=True)} return [Participant(name=pname, profile_id=pid) for pid, pname in participants.items()] @retry(stop_max_attempt_number=3) def disconnect(self): """ Leave hangout (equal on clicking on "Leave call" button). After leaving the call you can create a new one or connect to existing. .. doctest:: HangoutsBase2 >>> hangout.disconnect() """ self.utils.click_cancel_button_if_there_is_one() self.utils.click_menu_element('//div[@aria-label="Leave call"]') self.hangout_id = None if self.on_air is not None: # removing properties that is available only for OnAir for name, _ in getUtilitiesFor(IOnAirModule): delattr(self, name) self.on_air = None # waiting until hangout windows closes tries_n_time_until_true( lambda: len(self.browser.window_handles) == 1, try_num=100) self.browser.switch_to_window(self.browser.window_handles[-1]) def __del__(self): # and quiting browser and display if self.browser: try: if self.hangout_id and self.browser.window_handles: # leaving the call first self.disconnect() finally: self.browser.quit() if self.display is not None: self.display.stop()
tar_file.write(requests.get(template_spec["url"]).content) with open(tar_path, "rb") as tar_file: if verify(template_spec["hash"], tar_file.read()): tar_file.seek(0) temp_dir = tempfile.mkdtemp(prefix="malucrawl_prefix-") tarfile.open(fileobj=tar_file, mode="r:gz").extractall(temp_dir) else: raise IOError("Wine bottle is not valid") try: yield temp_dir finally: shutil.rmtree(temp_dir) disp = SmartDisplay(visible=0, bgcolor="black") master_prefix = create_wine_prefix(TEMPLATE) atexit.register(master_prefix.__exit__, None, None, None) atexit.register(disp.__exit__, None, None, None) master_prefix = master_prefix.__enter__() disp = disp.__enter__() def check_change(temp, master): dir_cmp = filecmp.dircmp(temp, master) newfiles = dir_cmp.left_only deletedfiles = dir_cmp.right_only funnyfiles = dir_cmp.funny_files
port = httpd.socket.getsockname()[-1] # Start up a virtual display, useful for testing on headless servers. # ----------------------------------------------------------------------------- if args.virtual: try: from pyvirtualdisplay.smartdisplay import SmartDisplay except ImportError: print "Please install the Python pyvirtualdisplay module." print " sudo pip install pyvirtualdisplay" sys.exit(-1) disp = None try: disp = SmartDisplay(visible=0, bgcolor='black').start() atexit.register(disp.stop) except: if disp: disp.stop() # Start up the web browser and run the tests. # ---------------------------------------------------------------------------- try: from selenium import webdriver except ImportError: print "Please install the Python selenium module." print " sudo pip install selenium" sys.exit(-1)
class SeleniumExecutor(ScenarioExecutor, WidgetProvider, FileLister): """ Selenium executor :type virtual_display: Display :type runner: AbstractTestRunner """ SELENIUM_DOWNLOAD_LINK = "http://selenium-release.storage.googleapis.com/{version}/" \ "selenium-server-standalone-{version}.0.jar" SELENIUM_VERSION = "2.53" JUNIT_DOWNLOAD_LINK = "http://search.maven.org/remotecontent?filepath=junit/junit/" \ "{version}/junit-{version}.jar" JUNIT_VERSION = "4.12" JUNIT_MIRRORS_SOURCE = "http://search.maven.org/solrsearch/select?q=g%3A%22junit%22%20AND%20a%3A%22" \ "junit%22%20AND%20v%3A%22{version}%22&rows=20&wt=json".format(version=JUNIT_VERSION) HAMCREST_DOWNLOAD_LINK = "http://search.maven.org/remotecontent?filepath=org/hamcrest/hamcrest-core" \ "/1.3/hamcrest-core-1.3.jar" JSON_JAR_DOWNLOAD_LINK = "http://search.maven.org/remotecontent?filepath=org/json/json/20160810/json-20160810.jar" SUPPORTED_TYPES = ["python-nose", "java-junit", "ruby-rspec"] SHARED_VIRTUAL_DISPLAY = {} def __init__(self): super(SeleniumExecutor, self).__init__() self.additional_env = {} self.virtual_display = None self.end_time = None self.runner = None self.report_file = None self.scenario = None self.script = None self.self_generated_script = False self.generated_methods = BetterDict() self.runner_working_dir = None def set_virtual_display(self): display_conf = self.settings.get("virtual-display") if display_conf: if is_windows(): self.log.warning("Cannot have virtual display on Windows, ignoring") else: if self.engine in SeleniumExecutor.SHARED_VIRTUAL_DISPLAY: self.virtual_display = SeleniumExecutor.SHARED_VIRTUAL_DISPLAY[self.engine] else: width = display_conf.get("width", 1024) height = display_conf.get("height", 768) self.virtual_display = Display(size=(width, height)) msg = "Starting virtual display[%s]: %s" self.log.info(msg, self.virtual_display.size, self.virtual_display.new_display_var) self.virtual_display.start() SeleniumExecutor.SHARED_VIRTUAL_DISPLAY[self.engine] = self.virtual_display def free_virtual_display(self): if self.virtual_display and self.virtual_display.is_alive(): self.virtual_display.stop() if self.engine in SeleniumExecutor.SHARED_VIRTUAL_DISPLAY: del SeleniumExecutor.SHARED_VIRTUAL_DISPLAY[self.engine] def get_script_path(self, scenario=None): if scenario: return super(SeleniumExecutor, self).get_script_path(scenario) else: return self.engine.find_file(self.script) def get_runner_working_dir(self): if self.runner_working_dir is None: self.runner_working_dir = self.engine.create_artifact("classes", "") return self.runner_working_dir def _create_runner(self, report_file): script_path = self.get_script_path() script_type = self.detect_script_type(script_path) runner_config = BetterDict() if script_type == "python-nose": runner_class = NoseTester runner_config.merge(self.settings.get("selenium-tools").get("nose")) elif script_type == "java-junit": runner_class = JUnitTester runner_config.merge(self.settings.get("selenium-tools").get("junit")) runner_config['working-dir'] = self.get_runner_working_dir() runner_config['props-file'] = self.engine.create_artifact("customrunner", ".properties") elif script_type == "ruby-rspec": runner_class = RSpecTester elif script_type == "js-mocha": runner_class = MochaTester else: raise ValueError("Unsupported script type: %s" % script_type) runner_config["script"] = script_path runner_config["script-type"] = script_type runner_config["artifacts-dir"] = self.engine.artifacts_dir runner_config["report-file"] = report_file runner_config["stdout"] = self.engine.create_artifact("selenium", ".out") runner_config["stderr"] = self.engine.create_artifact("selenium", ".err") return runner_class(runner_config, self) def _register_reader(self, report_file): if self.engine.is_functional_mode(): reader = FuncSamplesReader(report_file, self.log, self.generated_methods) if isinstance(self.engine.aggregator, FunctionalAggregator): self.engine.aggregator.add_underling(reader) else: reader = LoadSamplesReader(report_file, self.log, self.generated_methods) if isinstance(self.engine.aggregator, ConsolidatingAggregator): self.engine.aggregator.add_underling(reader) return reader def prepare(self): self.set_virtual_display() self.scenario = self.get_scenario() self.__setup_script() self.report_file = self.engine.create_artifact("selenium_tests_report", ".ldjson") self.runner = self._create_runner(self.report_file) self.runner.prepare() self.reader = self._register_reader(self.report_file) def __setup_script(self): if Scenario.SCRIPT in self.scenario and self.scenario.get(Scenario.SCRIPT): self.script = self.scenario.get(Scenario.SCRIPT) elif "requests" in self.scenario: self.script = self.__tests_from_requests() self.self_generated_script = True else: raise ValueError("Nothing to test, no requests were provided in scenario") def detect_script_type(self, script_path): if not isinstance(script_path, string_types) and not isinstance(script_path, text_type): raise ValueError("Nothing to test, no files were provided in scenario") if not os.path.exists(script_path): raise ValueError("Script %s doesn't exist" % script_path) if "language" in self.execution: lang = self.execution["language"] if lang not in self.SUPPORTED_TYPES: tmpl = "Language '%s' is not supported. Supported languages are: %s" raise ValueError(tmpl % (lang, self.SUPPORTED_TYPES)) self.log.debug("Using script type: %s", lang) return lang file_types = set() if os.path.isfile(script_path): # regular file received file_types.add(os.path.splitext(script_path)[1].lower()) else: # dir received: check contained files for file_name in get_files_recursive(script_path): file_types.add(os.path.splitext(file_name)[1].lower()) if '.java' in file_types or '.jar' in file_types: script_type = 'java-junit' elif '.py' in file_types: script_type = 'python-nose' elif '.rb' in file_types: script_type = 'ruby-rspec' elif '.js' in file_types: script_type = 'js-mocha' else: raise ValueError("Unsupported script type: %s" % script_path) self.log.debug("Detected script type: %s", script_type) return script_type def startup(self): """ Start runner :return: """ self.start_time = time.time() self.runner.env = self.additional_env self.runner.run_tests() def check_virtual_display(self): if self.virtual_display: if not self.virtual_display.is_alive(): self.log.info("Virtual display out: %s", self.virtual_display.stdout) self.log.warning("Virtual display err: %s", self.virtual_display.stderr) raise RuntimeError("Virtual display failed: %s" % self.virtual_display.return_code) def check(self): """ check if test completed :return: """ if self.widget: self.widget.update() self.check_virtual_display() return self.runner.is_finished() def report_test_duration(self): if self.start_time: self.end_time = time.time() self.log.debug("Selenium tests ran for %s seconds", self.end_time - self.start_time) def shutdown(self): """ shutdown test_runner :return: """ self.runner.shutdown() self.report_test_duration() def post_process(self): self.free_virtual_display() def has_results(self): if self.reader and self.reader.read_records: return True else: return False def get_widget(self): if not self.widget: self.widget = SeleniumWidget(self.script, self.runner.settings.get("stdout")) return self.widget def resource_files(self): self.scenario = self.get_scenario() self.__setup_script() script_path = self.get_script_path() resources = [] if script_path is not None: resources.append(script_path) return resources def __tests_from_requests(self): filename = self.engine.create_artifact("test_requests", ".py") wdlog = self.engine.create_artifact('webdriver', '.log') nose_test = SeleniumScriptBuilder(self.scenario, self.log, wdlog) if self.virtual_display: nose_test.window_size = self.virtual_display.size self.generated_methods.merge(nose_test.build_source_code()) nose_test.save(filename) return filename