Created
January 3, 2020 20:38
-
-
Save bbchriscesar/8a8ece5436a52b8bb452d15697ef4aa8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from selenium import webdriver | |
| from selenium.webdriver.support import expected_conditions as EC | |
| from selenium.webdriver.support.ui import WebDriverWait | |
| from selenium.webdriver.common.by import By | |
| from selenium.webdriver.common.action_chains import ActionChains as chains | |
| from selenium.webdriver.support.ui import Select | |
| from selenium.webdriver.common.keys import Keys | |
| from selenium.common.exceptions import TimeoutException | |
| import time | |
| import os | |
| xpath = By.XPATH | |
| id = By.ID | |
| css = By.CSS_SELECTOR | |
| name = By.NAME | |
| prof1 = 'Profile_01' | |
| prof2 = 'Profile_02' | |
| prof3 = 'Profile_03' | |
| prof4 = 'Profile_04' | |
| prof5 = 'Profile_05' | |
| prof6 = 'Profile_06' | |
| prof7 = 'Profile_07' | |
| def setupBrowser(profile): | |
| global driver | |
| options = webdriver.ChromeOptions() | |
| options.add_argument('user-data-dir=/usr/local/google/home/christiannec/.config/google-chrome/' + profile) | |
| options.add_argument('--start-maximized') | |
| options.add_experimental_option("excludeSwitches", ['enable-automation']) | |
| driver = webdriver.Chrome(executable_path='/usr/local/google/home/christiannec/IdeaProjects/SaraoScreenshotFramework/Drivers/chromedriver', options=options) | |
| driver.implicitly_wait(10) | |
| def navigateToURL(URL): | |
| driver.get(URL) | |
| print('\n') | |
| print(URL + ' is launched.') | |
| def findAndClick(locator, value): | |
| driver.find_element(locator, value).click() | |
| time.sleep(2) | |
| def findAndClickElements(locator, value, index): | |
| driver.find_elements(locator, value)[index].click() | |
| time.sleep(2) | |
| def takeScreenshot(filename): | |
| time.sleep(1) | |
| driver.save_screenshot(filename + '.png') | |
| b = os.path.split(filename)[1] | |
| print('Take screenshot for ' + b) | |
| def sendText(locator, value, text): | |
| driver.find_element(locator, value).send_keys(text) | |
| time.sleep(1) | |
| print('Send text ' + text) | |
| def sendTextToElements(locator, value, index, text): | |
| driver.find_elements(locator, value)[index].send_keys(text) | |
| time.sleep(1) | |
| print('Send text ' + text) | |
| def waitForElement(locator, value): | |
| wait = WebDriverWait(driver, 60) | |
| try: | |
| element = wait.until(EC.element_to_be_clickable((locator, value))) | |
| print('Waiting for the element to be clickable') | |
| except Exception as ex: | |
| print(ex) | |
| def hoverToElement(locator, value): | |
| actions = chains(driver) | |
| element_hover = driver.find_element(locator, value) | |
| actions.move_to_element(element_hover).perform() | |
| time.sleep(1) | |
| print('Hover to an element') | |
| def hoverToElements(locator, value, index): | |
| actions = chains(driver) | |
| element_hover = driver.find_elements(locator, value)[index] | |
| actions.move_to_element(element_hover).perform() | |
| time.sleep(1) | |
| print('Hover to an element') | |
| def clearText(locator, value): | |
| element = driver.find_element(locator, value) | |
| element.clear() | |
| time.sleep(1) | |
| print('Text is cleared') | |
| def clearTexts(locator, value, index): | |
| element = driver.find_elements(locator, value)[index] | |
| element.clear() | |
| time.sleep(1) | |
| print('Text is cleared') | |
| def selectDropDown(locator, value, text): | |
| select = Select(driver.find_element(locator, value)) | |
| select.select_by_visible_text(text) | |
| time.sleep(1) | |
| print('Select drop down option') | |
| def selectDropDownElements(locator, value, index, text): | |
| select = Select(driver.find_elements(locator, value)[index]) | |
| select.select_by_visible_text(text) | |
| time.sleep(1) | |
| print('Select drop down option') | |
| def scrollIntoView(locator, value): | |
| target = driver.find_element(locator, value) | |
| target.location_once_scrolled_into_view | |
| time.sleep(1) | |
| print('Scroll to a specific element') | |
| def scrollIntoViewElements(locator, value, index): | |
| target = driver.find_elements(locator, value)[index] | |
| target.location_once_scrolled_into_view | |
| time.sleep(1) | |
| print('Scroll to a specific element') | |
| def rightClick(locator, value): | |
| action = chains(driver) | |
| action.move_to_element(driver.find_element(locator, value)).perform() | |
| action.context_click().perform() | |
| time.sleep(1) | |
| print('Right clicked to an element') | |
| def rightClickElements(locator, value, index): | |
| action = chains(driver) | |
| action.move_to_element(driver.find_elements(locator, value)[index]).perform() | |
| action.context_click().perform() | |
| time.sleep(1) | |
| print('Right clicked to an element') | |
| def alert(): | |
| time.sleep(2) | |
| obj = driver.switch_to.alert | |
| obj.accept() | |
| print('Alert popup accepted') | |
| def refresh(): | |
| driver.refresh() | |
| time.sleep(3) | |
| print('Page is refreshed') | |
| def openNewTab(URL): | |
| driver.execute_script("window.open('');") | |
| driver.switch_to.window(driver.window_handles[1]) | |
| driver.get(URL) | |
| time.sleep(1) | |
| print('Open new tab') | |
| def backToMainTab(): | |
| driver.switch_to.window(driver.window_handles[0]) | |
| time.sleep(1) | |
| print('Switched to main tab') | |
| def closeTab(): | |
| driver.close() | |
| time.sleep(1) | |
| print('Close tab') | |
| def getLocale(): | |
| element = driver.find_element_by_xpath("//html[@lang]") | |
| localeCode = element.get_attribute("lang") | |
| return localeCode | |
| def switchFrame(indexNumber): | |
| driver.switch_to.frame(indexNumber) | |
| time.sleep(1) | |
| print('Switched to iFrame number ' + indexNumber) | |
| def switchDefaultFrame(): | |
| driver.switch_to.default_content() | |
| time.sleep(1) | |
| print('Switched to default frame') | |
| def pageDown(): | |
| driver.find_element_by_tag_name('body').send_keys(Keys.PAGE_DOWN) | |
| time.sleep(1) | |
| print('Pressed page down key') | |
| def pageUp(): | |
| driver.find_element_by_tag_name('body').send_keys(Keys.PAGE_UP) | |
| print('Pressed page up key') | |
| def pressEscape(): | |
| driver.find_element_by_tag_name('body').send_keys(Keys.ESCAPE) | |
| time.sleep(1) | |
| print('Pressed escape key') | |
| def alertWait(): | |
| try: | |
| WebDriverWait(driver, 3).until(EC.alert_is_present(), 'Message 1' + 'Message 2') | |
| alert = driver.switch_to.alert | |
| alert.accept() | |
| except TimeoutException: | |
| print('no alert') | |
| def moveBackward(): | |
| driver.back() | |
| time.sleep(1) | |
| print('Clicked back <--') | |
| def customDelay(secondsToDelay): | |
| time.sleep(secondsToDelay) | |
| print('Added ' + str(secondsToDelay) + ' seconds to delay') | |
| def tearDown(): | |
| driver.quit() | |
| print('Quit driver') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment