async def test_create_token(driver: webdriver.Chrome, selenium_config: SeleniumConfig) -> None: cookie = State(token=selenium_config.token).as_cookie() driver.header_overrides = {"Cookie": f"{COOKIE_NAME}={cookie}"} tokens_url = urljoin(selenium_config.url, "/auth/tokens") driver.get(tokens_url) tokens_page = TokensPage(driver) assert tokens_page.get_tokens(TokenType.user) == [] session_tokens = tokens_page.get_tokens(TokenType.session) assert len(session_tokens) == 1 assert session_tokens[0].token == selenium_config.token.key # Drop our cookie in favor of the one the browser is now sending, since # the browser one contains a CSRF token that will be required for token # creation. del driver.header_overrides create_modal = await tokens_page.click_create_token() create_modal.set_token_name("test token") await create_modal.submit() new_token_modal = tokens_page.get_new_token_modal() assert new_token_modal.token.startswith("gt-") new_token_modal.dismiss() user_tokens = tokens_page.get_tokens(TokenType.user) assert len(user_tokens) == 1 assert user_tokens[0].name == "test token"
def parse(self, username, password): driver = Chrome() self.sign_in(username, password, driver) input_data = self.get_input_data() highlights = { item['title']: item['id'] for item in input_data['highlights'] } story_hashtags = { item['hash_tag']: item['id'] for item in input_data['story_hashtags'] } igtv_hashtags = { item['hash_tag']: item['id'] for item in input_data['igtv_hashtags'] } output_data = dict() for user in input_data['users']: driver.get(user['instagram']) request = driver.wait_for_request('include_suggested_users') output_data['stories'] = self.get_stories( user['id'], driver, request, highlights, story_hashtags, ) output_data['igtvs'] = self.get_igtv_data( driver, user, igtv_hashtags, ) self.send_to_app(output_data)
async def selenium_config( tmp_path: Path, driver: webdriver.Chrome, empty_database: None ) -> AsyncIterator[SeleniumConfig]: """Start a server for Selenium tests. The server will be automatically stopped at the end of the test. The Selenium web driver will be automatically configured with a valid authentication token in a cookie. Returns ------- config : `tests.support.selenium.SeleniumConfig` Configuration information for the server. """ settings_path = build_settings(tmp_path, "selenium") config_dependency.set_settings_path(str(settings_path)) async with run_app(tmp_path, settings_path) as config: cookie = await State(token=config.token).as_cookie() driver.header_overrides = {"Cookie": f"{COOKIE_NAME}={cookie}"} # The synthetic cookie doesn't have a CSRF token, so we want to # replace it with a real cookie. Do this by visiting the top-level # page of the UI and waiting for the token list to appear, which will # trigger fleshing out the state, and then dropping the header # override for subsequent calls so that the cookie set in the browser # will be used. driver.get(urljoin(config.url, "/auth/tokens/")) tokens_page = TokensPage(driver) tokens_page.get_tokens(TokenType.session) del driver.header_overrides yield config
async def test_expired_token(driver: webdriver.Chrome, selenium_config: SeleniumConfig) -> None: driver.get(urljoin(selenium_config.url, "/auth/tokens")) tokens_page = TokensPage(driver) session_tokens = tokens_page.get_tokens(TokenType.session) # Find the expired token, which is the one that doesn't match the one # returned by the selenium configuration. session_token = next(t for t in session_tokens if t.token != selenium_config.token.key) token = session_token.token # Check that the expiration time is displayed correctly. assert session_token.expires == "expired" # Go to the token page and check that the expiration there and in the # history is also correct. session_token.click_token() data_page = TokenDataPage(driver) assert data_page.username == "testuser" assert data_page.token_type == "session" scopes = sorted(selenium_config.config.known_scopes.keys()) assert data_page.scopes == ", ".join(scopes) assert data_page.expires == "expired" history = data_page.get_change_history() assert len(history) == 1 assert history[0].action == "create" assert history[0].token == token assert history[0].expires == "expired"
async def test_token_info(driver: webdriver.Chrome, selenium_config: SeleniumConfig) -> None: cookie = await State(token=selenium_config.token).as_cookie() # Create a notebook token and an internal token. r = httpx.get( urljoin(selenium_config.url, "/auth"), params={ "scope": "exec:test", "notebook": "true" }, headers={"Cookie": f"{COOKIE_NAME}={cookie}"}, ) assert r.status_code == 200 notebook_token = Token.from_str(r.headers["X-Auth-Request-Token"]) r = httpx.get( urljoin(selenium_config.url, "/auth"), params={ "scope": "exec:test", "delegate_to": "service" }, headers={"Cookie": f"{COOKIE_NAME}={cookie}"}, ) assert r.status_code == 200 internal_token = Token.from_str(r.headers["X-Auth-Request-Token"]) # Load the token page and go to the history for our session token. driver.get(urljoin(selenium_config.url, "/auth/tokens")) tokens_page = TokensPage(driver) session_tokens = tokens_page.get_tokens(TokenType.session) session_token = next(t for t in session_tokens if t.token == selenium_config.token.key) session_token.click_token() # We should now be at the token information page for the session token. data_page = TokenDataPage(driver) assert data_page.username == "testuser" assert data_page.token_type == "session" scopes = sorted(selenium_config.config.known_scopes.keys()) assert data_page.scopes == ", ".join(scopes) history = data_page.get_change_history() assert len(history) == 3 assert history[0].action == "create" assert history[0].token == internal_token.key assert history[0].scopes == "" assert history[1].action == "create" assert history[1].token == notebook_token.key assert history[1].scopes == ", ".join(scopes) assert history[2].action == "create" assert history[2].token == selenium_config.token.key assert history[2].scopes == ", ".join(scopes)
def get_stories(user: AnyStr, driver: Chrome, request: AnyStr, highlights: Dict, hashtags: Dict) -> List: """ :param user: instagram user :param driver: browser :param request: driver request :param highlights: dict of highlights names :param hashtags: dict of hashtags names :return: stories data list """ current_user_data = requests.get(request.url, headers=request.headers).text edges = json.loads( current_user_data)['data']['user']['edge_highlight_reels']['edges'] items = [] for edge in edges: node = edge['node'] title = node['title'] if title not in highlights: continue driver.get( 'https://www.instagram.com/stories/highlights/{}'.format( node['id'])) sleep(randint(2, 4)) request = driver.wait_for_request('show_story_viewer_list') response_data = json.loads( requests.get(request.url, headers=request.headers).text) for item in response_data['data']['reels_media'][0]['items']: video_resources = item.get('video_resources') tappable_objects = item['tappable_objects'] if tappable_objects and tappable_objects[0][ '__typename'] == 'GraphTappableHashtag': hashtag = tappable_objects[0]['name'] if hashtag not in hashtags or not video_resources: continue items.append({ 'hash_tag': hashtags[hashtag], 'highlight': highlights[title], 'user': user, 'url': video_resources[-1]['src'], }) return items
def __init__(self): global driver, _session # Ajustar el nivel de registro a debug options = webdriver.ChromeOptions() options.add_argument('--log-level=3') # Crear una instancia de webdriver print('Inicializando instancia de ChromeDriver...') driver = Driver(options=options) driver.get('https://consumopolis.consumo.gob.es/concurso/index.html') # Esperar a que se realice el inicio de sesión print('Esperando inicio de sesión') # Cambiar al iframe driver.switch_to.frame(0) while True: # Obtener los campos de entrada de usuario y contraseña try: _name = driver.find_element_by_id('nick').text except selenium.common.exceptions.NoSuchElementException: continue else: print('¡Sesión iniciada correctamente!') _session = (_name, re.search(r'\d+', [r for r in driver.requests if 'Avatar.aspx' in r.url][0].body.decode()).group(0)) print('Sesión iniciada como %s (ID: %s)' %(_session[0], _session[1])) break # Cambiar al contenido principal driver.switch_to.default_content() def _menu(): while True: print('1) Obtener respuestas del juego\n2) Modificar puntuaciones\n') _sel = input('Tu selección: ') if _sel == '1': self.get_answers() elif _sel == '2': self.modify_score() else: print('Opción inválida') _menu()
async def test_create_token(driver: webdriver.Chrome, selenium_config: SeleniumConfig) -> None: driver.get(urljoin(selenium_config.url, "/auth/tokens")) tokens_page = TokensPage(driver) assert tokens_page.get_tokens(TokenType.user) == [] session_tokens = tokens_page.get_tokens(TokenType.session) assert len(session_tokens) == 2 assert any(t for t in session_tokens if t.token == selenium_config.token.key) create_modal = tokens_page.click_create_token() create_modal.set_token_name("test token") create_modal.submit() new_token_modal = tokens_page.get_new_token_modal() assert new_token_modal.token.startswith("gt-") new_token_modal.dismiss() user_tokens = tokens_page.get_tokens(TokenType.user) assert len(user_tokens) == 1 assert user_tokens[0].name == "test token"
def browser(): driver = Chrome() driver.get(urls.SIGN_IN_URL) driver.implicitly_wait(5) yield driver driver.quit()
class Client(): def __init__( self, url, get_rate=0.5, chat_per_refresh=100, cli_login=True, user_data=None, timeout=10, ): self.chatURL = url self.chat_per_refresh = chat_per_refresh self.get_rate = get_rate self.timeout = timeout self.on_chat = lambda x, y: [] self.on_ready = lambda: [] self.dict_user = {} def response_interceptor(req, res): if 'sync_chat_channel' in req.url and req.method == 'GET': try: dict_raw = gzip.decompress(res.body) dict_usr = json.loads(dict_raw) dict_usr = dict_usr['result_data']['users'] for usr in dict_usr: self.dict_user[usr["user_no"]] = usr["name"] print("Found usercode-username dictionary") except: print("Invalid dictionary") caps = DesiredCapabilities.CHROME caps['goog:loggingPrefs'] = {'performance': 'ALL'} options = ChromeOptions() options.add_experimental_option('perfLoggingPrefs', { 'enableNetwork': True, 'enablePage': False }) options.add_argument('--disable-extensions') options.add_argument("--no-sandbox") if cli_login: options.headless = True if user_data is not None: options.add_argument(f"--user-data-dir={user_data}") print("Driver initializing...") self.driver = Chrome(options=options) self.driver.response_interceptor = response_interceptor self.driver.implicitly_wait(timeout) print("Driver initialized.") if cli_login: self.driver.get(self.chatURL) print("Get login page completed.") self._locate_by_css_selector(".uBtn.-icoType.-phone").click() phone_box = self._locate_by_id("input_local_phone_number") print("Get PhonenumberPage completed.") phone = input("Phone number: +82") phone_box.send_keys(phone) self._locate_by_css_selector(".uBtn.-tcType.-confirm").click() pw_box = self._locate_by_id("pw") print("Get PasswordPage completed.") pw = input("Password: "******"pw").send_keys(pw) self._locate_by_css_selector(".uBtn.-tcType.-confirm").click() print("Get SMSPage completed.") try: print("Trying to get hintNumberDiv...") hint = self._locate_by_id("hintNumberDiv") print("Hintnumber:", hint.text) input("Press Enter to continue...") except: print("Retrieving SMS authcode...") code_box = self._locate_by_id("code") print("codebox grabbed") pw_band = input("SMS authcode: ") code_box.send_keys(str(pw_band)) self._locate_by_css_selector( "button.uBtn.-tcType.-confirm").click() print("Login completed.") else: self.driver.get(self.chatURL) input("Please login from GUI.\nPress Enter to Continue...") self._refresh() self._clear_log() def _clear_log(self): self.driver.get_log('performance') def _refresh(self): self.driver.get(self.chatURL) self.msgWrite = self._locate_by_class("commentWrite") print("Refresh completed") def _locate_by_id(self, id_name): return WebDriverWait(self.driver, self.timeout).until( EC.presence_of_element_located((By.ID, id_name))) def _locate_by_class(self, class_name): return WebDriverWait(self.driver, self.timeout).until( EC.presence_of_element_located((By.CLASS_NAME, class_name))) def _locate_by_css_selector(self, css): return WebDriverWait(self.driver, self.timeout).until( EC.presence_of_element_located((By.CSS_SELECTOR, css))) def _send_image(self, rPath): try: absPath = os.path.abspath(rPath) img_up = self._locate_by_css_selector( "input[data-uiselector='imageUploadButton']") img_up.send_keys(absPath) sleep(2) except Exception as e: print(e) return def _send_chat(self, str_i): lines = str_i.split("\n") last_index = len(lines) - 1 for i, chat in enumerate(lines): self.msgWrite.send_keys(chat) if i != last_index: self.msgWrite.send_keys(Keys.SHIFT, Keys.ENTER) locator = lambda x: self.msgWrite.get_attribute('value') == str_i WebDriverWait(self.driver, 5).until(locator) self.msgWrite.send_keys(Keys.ENTER) def _parse_response(self, res_lst): try: for res in res_lst: if res[0] == "chat": self._send_chat(res[1]) elif res[0] == "image": self._send_image(res[1]) elif res[0] == "change": self.chatURL = res[1] self._refresh() elif res[0] == "delay": sleep(float(res[1])) except Exception as e: print(e) print("Error while parsing response") def on_event(self, ifunction): if ifunction.__name__ == "on_chat": self.on_chat = ifunction elif ifunction.__name__ == "on_ready": self.on_ready = ifunction def run(self): self._refresh() num_chats = 0 while True: if num_chats > self.chat_per_refresh: self._refresh() num_chats = 0 for log in self.driver.get_log('performance'): try: message = log['message'] if "Network.webSocketFrameReceived" not in message: continue elif "userNo" not in message: continue elif "contents" not in message: continue num_chats += 1 msg = json.loads(message) payload = msg['message']['params']['response'][ 'payloadData'] from_index = payload.find(',') + 1 message_parsed = json.loads(payload[from_index:]) chat_parsed = message_parsed[1]['message'] user_no = chat_parsed['userNo'] chat_body = chat_parsed['contents'] try: user_str = self.dict_user[int(user_no)] except Exception as e: print(e) user_str = "unknown_user" print(f"{user_str}: {chat_body}") res = self.on_chat(user_no, user_str, chat_body) self._parse_response(res) except Exception as e: print(e) continue sleep(self.get_rate)