def _login(self, driver: WebDriver) -> bool: # https://stackoverflow.com/a/27417860/706389 BT_TIMEOUT = 5 # seconds driver.implicitly_wait(BT_TIMEOUT) driver.set_page_load_timeout(BT_TIMEOUT) BT_PAGE = "https://www.btopenzone.com:8443/home" driver.get(BT_PAGE) # TODO huh, on VPN it shows 'you may have lost connection'. weird. if "You’re now logged in to BT Wi-fi" in driver.page_source: self.logger.warning("Already logged... weird, doing nothing") return True # this is the weird bug 'wifi access has expired' if len(driver.find_elements_by_link_text("Buy more time")) > 0: driver.find_element_by_link_text("Logout").click() # TODO how to wait? # select 'BT Wi-fi driver.find_element_by_id("provider2").click() # for some reason every bt wifi option (FON/Broadband/Wifi) has its own form # so we have to find the one responsible for BT WiFI login login_form = driver.find_element_by_id('wifi_logon_form') login_form.find_element_by_id("username").send_keys(self.username) login_form.find_element_by_id("password").send_keys(self.password) login_form.find_element_by_id("loginbtn").click() return True
def verificar_elemento_html_hasta_no_existir_en_el_dom_html( web_driver: WebDriver, time=5, id=None, xpath=None, link_text=None, partial_link_text=None, name=None, tag_name=None, class_name=None, css_selector=None): msg_selector_html_a_localizar = HtmlActions.generar_identificador_excepcion( id, xpath, link_text, partial_link_text, name, tag_name, class_name, css_selector) tiempo_inicial = Temporizador.obtener_tiempo_timer() while True: try: if id is not None: web_driver.find_element_by_id(id) elif xpath is not None: web_driver.find_element_by_xpath(xpath) elif link_text is not None: web_driver.find_element_by_link_text(link_text) elif partial_link_text is not None: web_driver.find_element_by_partial_link_text( partial_link_text) elif name is not None: web_driver.find_element_by_name(name) elif tag_name is not None: web_driver.find_element_by_tag_name(tag_name) elif class_name is not None: web_driver.find_element_by_class_name(class_name) elif css_selector is not None: web_driver.find_element_by_css_selector(css_selector) segundos_transcurridos = Temporizador.obtener_tiempo_timer( ) - tiempo_inicial if segundos_transcurridos > time: e = TimeoutException() e.msg = webdriver_actions_constantes.WEBDRIVER_WAIT_UNTIL_NOT_TIMEOUT_EXCEPTION.format( time, msg_selector_html_a_localizar, e.msg) raise e else: pass except NoSuchElementException: break
def login_click_bt(driver: WebDriver, username: str, password: str): if "You’re now logged in to BT Wi-fi" in driver.page_source: logger.warning("Already logged... weird, doing nothing") return True # this is the weird bug 'wifi access has expired' if len(driver.find_elements_by_link_text("Buy more time")) > 0: driver.find_element_by_link_text("Logout").click() # TODO how to wait? driver.find_element_by_id("username").send_keys(username) driver.find_element_by_id("password-password").send_keys(password) driver.find_element_by_class_name("lgnbtn").click() return True
def login(driver: WebDriver, wait: WebDriverWait): """ ログイン """ driver.get(AMEBA_URL) # click login link_text = os.environ.get('SOCIAL_VENDOR_NAME') wait.until(ec.element_to_be_clickable((By.LINK_TEXT, link_text))) target_link = driver.find_element_by_link_text(link_text) target_link.click() # username username = driver.find_element_by_id('username') wait.until(ec.visibility_of(username)) username.send_keys(os.environ.get('SOCIAL_USERNAME')) driver.find_element_by_id('btnNext').click() time.sleep(2) # password wait.until(ec.visibility_of_element_located((By.ID, 'passwd'))) password = driver.find_element_by_id('passwd') password.send_keys(os.environ.get('SOCIAL_PASSWORD')) time.sleep(2) # click driver.find_element_by_id('btnSubmit').click()
def get_job_tran_run_id(browser: WebDriver, tran_name: str, submitter_input: str) -> str: console = browser.find_element_by_link_text('CDS Console') console.click() Transactionname = browser.find_element_by_css_selector('input[id = "Filters_TranName"]') Transactionname.send_keys(tran_name) submitter = browser.find_element_by_id('Filters_Submitter') submitter.clear() submitter = browser.find_element_by_id('Filters_Submitter') submitter.send_keys(submitter_input) show = browser.find_element_by_id('Filters_Timefilterval') show.send_keys('All') Apply_Filter = browser.find_element_by_id('ApplyFilterButton') Apply_Filter.click() rows = browser.find_elements_by_css_selector('tr.table_row') for row in rows: cells = row.find_elements_by_tag_name('td') for i, c in enumerate(cells): if c.text == tran_name: tran_run_id = cells[i + 10].text return tran_run_id
def get_populated_puzzle(self, driver: WebDriver, timeout: int): img_path = "tmp.png" board = (WebDriverWait(driver, timeout).until( EC.presence_of_element_located( (By.ID, "puzzletable"))).find_element_by_tag_name("tbody")) board.screenshot(img_path) clues = "\n".join(clue.text for clue in driver.find_element_by_id( "tabs-1").find_elements_by_class_name("clue")) driver.find_element_by_link_text("Story").click() story = (WebDriverWait(driver, timeout).until( EC.visibility_of_element_located( (By.ID, "tabs-2"))).find_element_by_tag_name("p").text.strip()) return PopulatedLogicPuzzle(img_path, story, clues, self.grid_size_option)
def login_click_with_fon(driver: WebDriver, username: str, password: str): if "You’re now logged in to BT Wi-fi" in driver.page_source: logger.warning("Already logged... weird, doing nothing") return True # this is the weird bug 'wifi access has expired' if len(driver.find_elements_by_link_text("Buy more time")) > 0: driver.find_element_by_link_text("Logout").click() # TODO how to wait? # select 'BT Wi-fi driver.find_element_by_id("provider2").click() # for some reason every bt wifi option (FON/Broadband/Wifi) has its own form # so we have to find the one responsible for BT WiFI login login_form = driver.find_element_by_id('wifi_logon_form') login_form.find_element_by_id("username").send_keys(username) login_form.find_element_by_id("password").send_keys(password) login_form.find_element_by_id("loginbtn").click() return True
class TestSelenium: driver = None sample_page = None @classmethod def setup_class(cls): """ Remove previous saved screenshots :return: """ if os.path.exists(DIR): shutil.rmtree(DIR) os.mkdir(DIR) def setup_method(self): config = LoadConfig() selenium_hub = config.get_value(SELENIUM_HUB) browser = config.get_value(BROWSER).upper() self.driver = WebDriver( command_executor=f'http://{selenium_hub}/wd/hub', desired_capabilities=getattr(DesiredCapabilities, browser).copy()) self.driver.get('http://store.demoqa.com') self.navigate_to_page(SAMPLE_PAGE) self.sample_page = SamplePage(self.driver) def teardown_method(self): # You cannot close Firefox and quit it after, or close or quit, not both. # self.driver.close() self.driver.quit() self.driver = None self.sample_page = None def navigate_to_page(self, link_text): """ Search for the first link that has test described in :param link_text: :param link_text: String :return: """ self.driver.find_element_by_link_text(link_text).click() self.driver.save_screenshot(f'{DIR}/{link_text}.png') @staticmethod def generate_string(size): # TODO I don't like how this string is generated text = TextGenerator() text.generate_dictionary(size) return ' '.join(map(str, text.get_dictionary())) def test_wrong_email_redirects_error_page(self, request): """ Writes a comment with wrong email, saves a screenshot. Checks that error messages appears. :param request: pytest fixture with built in methods :return: """ # when comment = self.generate_string(100) self.sample_page.write_comment(comment, NAME, WRONG_EMAIL) self.driver.save_screenshot(f'{DIR}/{request.node.name}.png') # then error = self.driver.find_element_by_id(ERROR_MESSAGE_ID) assert ERROR_MESSAGE in error.text def test_correct_email_adds_comment(self, request): """ Writes a comment with correct email, saves a screenshot. Checks that comment was inserted and it has the correct value. :param request: pytest fixture with built in methods :return: """ # when comment = self.generate_string(100) self.sample_page.write_comment(comment, NAME, CORRECT_EMAIL) # then # TODO self.driver.current_url works, but is a bit out of context, maybe it should be returned by .write_comment comment_found = self.sample_page.get_comment(self.driver.current_url) self.driver.save_screenshot(f'{DIR}/{request.node.name}.png') assert comment_found is not None assert comment == comment_found.find_element_by_class_name( COMMENT_BODY_ID).text def test_of_the_gods(self, request): """ Does everything, writes a comment with wrong email, checks that error messages appears, goes back, writes a new comment with correct email, saves a screenshot, checks that comment was inserted and it has the correct value. :param request: pytest fixture with built in methods :return: """ # when self.sample_page.write_comment(self.generate_string(100), NAME, WRONG_EMAIL) # then error = self.driver.find_element_by_id(ERROR_MESSAGE_ID) assert ERROR_MESSAGE in error.text # Navigate to Sample Page self.driver.back() # when # Chrome keeps values previously typed self.sample_page.clear_fields() comment = self.generate_string(100) self.sample_page.write_comment(comment, NAME, CORRECT_EMAIL) # TODO self.driver.current_url works, but is a bit out of context, maybe it should be returned by .write_comment comment_found = self.sample_page.get_comment(self.driver.current_url) self.driver.save_screenshot(f'{DIR}/{request.node.name}.png') assert comment_found is not None assert comment_found.find_element_by_class_name( COMMENT_BODY_ID).text == comment