def test_can_random_tmp(self, mock_randint, mock_time): self._empty_lock_pool() mock_randint.side_effect = [1, 2] mock_time.side_effect = [1, 2] mock_path = os.path.join('/tmp/', 'chrome_user_data_11') os.system('mkdir -p ' + mock_path) expected_path = os.path.join('/tmp/', 'chrome_user_data_22') os.system('rm -rf ' + expected_path) lock, path = get_chrome_resource() self.assertIsNone(lock) self.assertTrue(expected_path, path)
def __init__(self): """ Init BaseBrowserService """ display = Display(visible=0, size=(self.DISPLAY_WIDTH, self.DISPLAY_HEIGHT)) display.start() self.screen = display self.browser_lock, self.browser_user_path = utils.get_chrome_resource() logger.info('BrowserService.__init__ lock:%s, path:%s' % (self.browser_lock, self.browser_user_path)) chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('user-data-dir=' + self.browser_user_path) chrome_options.add_argument('homepage=' + settings.CHROME_HOME_PAGE) try: driver = webdriver.Chrome(chrome_options=chrome_options) except Exception as e: logger.error('BrowserService.__init__ driver error:' + str(e)) driver = webdriver.Chrome() driver.set_window_size(self.DISPLAY_WIDTH, self.DISPLAY_HEIGHT) self.browser = driver self.wait = WebDriverWait(self.browser, 60) self.jquery = open(settings.JQUERY_JS_FILE).read()
def test_when_no_resource_can_use_tmp(self): self._empty_lock_pool() lock, path = get_chrome_resource() self.assertIsNone(lock) self.assertTrue(os.path.exists(path))
def test_can_get_lock(self): lock, path = get_chrome_resource() self.assertIsNotNone(lock)
def _empty_lock_pool(self): for idx in xrange(settings.CHROME_USER_DATA_DIR_POOL_SIZE): get_chrome_resource()