Created
September 1, 2015 06:28
-
-
Save looopTools/91847eadfcd4560167c8 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env python | |
# Copyright Steinwurf ApS 2015. | |
# Distributed under the "STEINWURF RESEARCH LICENSE 1.0". | |
# See accompanying file LICENSE.rst or | |
# http://www.steinwurf.com/licensing | |
import os | |
import shutil | |
import threading | |
import sys | |
import getpass | |
import platform | |
## selenium test suite imports | |
from selenium import webdriver | |
from selenium.webdriver.common.by import By | |
from selenium.common.exceptions import TimeoutException | |
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0 | |
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0 | |
chromedriver = './chromedriver' | |
os.environ["webdriver.chrome.driver"] = chromedriver | |
## | |
drivers = [] | |
threads = [] | |
def main(test_amount, website, element_id): | |
for x in range(0, test_amount): | |
threads.append(threading.Thread(target=thread_test, args=[website, element_id, x])) | |
for thread in threads: | |
print("Thread has been started") | |
thread.start() | |
thread.join() | |
for driver in drivers: | |
driver.quit() | |
# for thread in threads: | |
# thread.join() | |
if check_amount_of_files(test_amount): | |
print('>> there has been downloaded the expected amount of files') | |
else: | |
print('>> there has not been downloaded the expected amount of files') | |
def thread_test(website, element_id, test_id): | |
print('Test number {} has been spawned'.format(test_id)) | |
perform_click(website, element_id) | |
print('Test number {} has compledted its run'.format(test_id)) | |
def check_amount_of_files(test_amount): | |
counter = 0 | |
for f in os.listdir(generate_download_path()): | |
if "lorem" in f: | |
counter = counter + 1 | |
if counter == test_amount: | |
return True | |
else: | |
return False | |
def generate_download_path(): | |
username = getpass.getuser() | |
download_path = '' | |
if platform.system() == 'Linux': | |
download_path = '/home/{}/Downloads'.format(username) | |
return download_path | |
def perform_click(website, element_id): | |
driver = webdriver.Chrome(chromedriver) | |
drivers.append(driver) | |
driver.get(website) | |
try: | |
wait_element = WebDriverWait(driver, 50).until( | |
EC.presence_of_element_located((By.ID, element_id)) | |
) | |
element = driver.find_element_by_id(element_id) | |
print('>> element with id {} exist'.format(element_id)) | |
element.click() | |
check_is_server(driver, 'act_as_server') | |
finally: | |
print('>> doing it') | |
#driver.quit() | |
def check_is_server(driver, elments): | |
print('I am here') | |
res = False | |
try: | |
element = driver.find_element_by_id(elements) | |
while('YES' != element.text): | |
print('>> element text: {}'.format(element.text)) | |
element = driver.find_element_by_id(element) | |
res = True | |
finally: | |
return res | |
## wait_element | |
## act_as_server | |
## Only if asked for | |
def clean_up(folder_path, part_filename): | |
for f in os.listdir(folder_path): | |
if part_filename in f: | |
cfile = os.part.join(folder_path, part_filename) | |
try: | |
if os.path.isfile(cfile): | |
os.unlink(cfile) | |
except Exception, e: | |
print e | |
def extract_arg(input_str): | |
start_i = input_str.index('=') + 1 | |
return input_str[start_i:] | |
def find_arg(arg, args): | |
for s in args: | |
if arg in s: | |
return extract_arg(s) | |
return None | |
def print_help(): | |
print('Arguments:') | |
print('[arg] test_amount=') | |
print(' Example test_amount=10 - Explicit tell the test script,') | |
print('how many test should be run') | |
print('[arg] website=') | |
print(' Example website=http://127.0.0.1:4000/ - Explicit tell the test script,') | |
print('which website to test against') | |
print('[arg] element_id=') | |
print(' Example elment_id=fiel_1 - Explicit tell the test script,') | |
print('which web element to click on, when executing test') | |
## Start of script execution | |
## Amount of test threads spawned as standard | |
test_amount = 5 | |
## Website test against as standard | |
website = 'http://127.0.0.1:4000/' | |
## Element clicked for download as standard | |
element_id = 'file_1' | |
## Check for input arguments | |
if '--help' in sys.argv: | |
print_help() | |
else: | |
if not find_arg('test_amount', sys.argv) is None: | |
test_amount = int(find_arg('test_amount', sys.argv)) | |
if not find_arg('website', sys.argv) is None: | |
website = find_arg('website', sys.argv) | |
if not find_arg('element_id', sys.argv) is None: | |
element_id = find_arg('element_id', sys.argv) | |
main(test_amount, website, element_id) | |
if 'run_cleanup' in sys.argv: | |
folder_path = 'downloads' | |
file_name = 'lorem' | |
clean_up(folder_path, file_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment