Advertisement
lexquarkie

test_py_1

Sep 30th, 2017
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.27 KB | None | 0 0
  1. from selenium import webdriver
  2. from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
  3. from selenium.webdriver.support.ui import WebDriverWait
  4. from selenium.webdriver.support import expected_conditions as EC
  5. from selenium.common.exceptions import WebDriverException,NoSuchElementException
  6.  
  7. import logging
  8. import time
  9. import inspect
  10.  
  11. ADMIN_CREDS = ['admin', 'admin']
  12. userdata = {}
  13.  
  14. userdata = {'username':'###########',
  15.   'email':'###########@mail.ru',
  16.  'first_name':'George',
  17.  'last_name':'Bush',
  18.  'second_name':'Middlest',
  19.  'password':'1QAZ2wsx',
  20.  'is_staff':False,
  21.  'is_superuser':False,
  22.  'is_active':True }
  23.  
  24.  
  25. sellerdata = {'username':'############',
  26.  'email':'#########_1111@mail.ru',
  27.  'first_name':'Seller',
  28.  'last_name':'Numero',
  29.  'second_name':'Uno',
  30.  'password':'1QAZ2wsx',
  31.  'inn': '01234564563',
  32.  'phone': '89161234567',
  33.  'is_staff':False,
  34.  'is_superuser':False,
  35.  'is_active':True }
  36.  
  37. #DOMAIN = 'http://###############/'
  38.  
  39.  
  40.  
  41. def login_to_admin(login, password):
  42.     go("/admin/")
  43.     wait(.3)
  44.    
  45.     #log(is_logined_as_admin())
  46.     by_name("username").clear()
  47.     by_name("username").send_keys(login)
  48.     by_name("password").clear()
  49.     by_name("password").send_keys(password)
  50.     by_xpath("/html/body/div[1]/div/div/div/div[2]/form/a").click()
  51.     h3_title = by_xpath('/html/body/div[4]/div[1]/div/div/div/h3').text
  52.     assert 'Панель управления' in h3_title
  53.     log('"%s" passed OK \n' % inspect.getframeinfo(inspect.currentframe())[2])
  54.  
  55. def unlogin_from_admin():
  56.     go("/admin/")
  57.     check_title('Панель управления - SpicyCMS')
  58.     wait(2)    
  59.     elem = by_xpath("//span[contains(text(),'admin')]")
  60.     elem.click()    
  61.     by_xpath("//li[5]/a/span").click()
  62.     good_bye_text = by_xpath("/html/body/div[1]/div/div/div/div[1]/span").text
  63.     assert 'До свидания' in good_bye_text
  64.     log('"%s" passed OK' % inspect.getframeinfo(inspect.currentframe())[2])
  65.  
  66. def register_buyer(userdata):
  67.     username = userdata['username']
  68.     password = userdata['password']
  69.     go("/")
  70.     by_xpath("//div[3]/div/div[2]/button").click()
  71.     wait(.3)
  72.     by_name("email").clear()
  73.     by_name("email").send_keys(username)
  74.     by_css("#signupForm > div.form-group > input[name=\"password\"]").clear()
  75.     by_css("#signupForm > div.form-group > input[name=\"password\"]").send_keys(password)
  76.     by_id("offer").click()
  77.    
  78.     by_id("signupBtn").click()
  79.     # ТУТ НУЖНО АКТИВИРОВАТЬ УЧЕТКУ
  80.  
  81. def register_seller(sellerdata):
  82.     go('/')
  83.     location_modal_yes()
  84.  
  85.     scr('sellerreg_1')
  86.     wait(.3)
  87.     by_class_name("vendor-reg-btn").click()
  88.     try:
  89.         element = WebDriverWait(driver, 10).until(
  90.         EC.visibility_of_element_located((By.NAME, "inn"))
  91.         )
  92.         element.click()
  93.           #page_source(200000)
  94.  
  95.     except NoSuchElementException:
  96.         log('Element Not Found')
  97.         driver.quit()
  98.    
  99.     wait(.3)
  100.     by_name("inn").click()
  101.     by_name("inn").clear()
  102.     by_name("inn").send_keys(sellerdata['inn'])
  103.     wait(.1)
  104.     by_name("phone").clear()
  105.     by_name("phone").send_keys(sellerdata['phone'])
  106.     wait(.1)
  107.     by_xpath('//*[@id="signupForm2"]/div[6]/input').clear()
  108.     by_xpath('//*[@id="signupForm2"]/div[6]/input').send_keys(sellerdata['email'])
  109.     wait(.1)
  110.     by_xpath('//*[@id="signupForm2"]/div[8]/input[1]').clear()
  111.     by_xpath('//*[@id="signupForm2"]/div[8]/input[1]').send_keys(sellerdata['password'])
  112.     wait(.1)
  113.     by_id("offer2").click()
  114.     by_id("signupBtn2").click()
  115.     scr('sellerreg_2')
  116.     log('"%s" passed OK' % inspect.getframeinfo(inspect.currentframe())[2])
  117.  
  118. def menu_users():
  119.     by_link_text(u"Все пользователи").click()
  120.  
  121. def menu_all_profiles():
  122.     by_link_text(u"Все профили").click()
  123.  
  124. def find_user(username):
  125.     menu_users()
  126.     menu_all_profiles()
  127.     by_id("id_search_text").clear()
  128.     by_id("id_search_text").send_keys(username)
  129.     by_css("button.btn.btn-blue").click()
  130.     assert username in driver.find_element_by_link_text(username).text
  131.     by_link_text(username).click()
  132.  
  133. def location_modal_yes():
  134.     wait(1.1)
  135.     try:
  136.         by_class_name('head-navbar__user-location')
  137.         by_class_name('user-location__yes').click() # Ваш город - Да
  138.         log('modal window Location found, clicked YES')
  139.     except:
  140.         log('modal window Location NOT found')
  141.  
  142. def location_modal_no():
  143.     wait(1.1)
  144.     try:
  145.         by_class_name('head-navbar__user-location')
  146.         by_class_name('user-location__no').click() # Ваш город - Да
  147.         log('modal window Location found, clicked YES')
  148.     except:
  149.         log('modal window Location NOT found')
  150.  
  151. def login_activated_buyer(userdata):
  152.     go('/')
  153.     wait(2)
  154.     scr('prelogin')  
  155.     location_modal_yes()
  156.     # wait(.5)
  157.    
  158.     by_xpath('//*[@id="navigation"]/ul/li[4]/button').click()
  159.     try:
  160.         element = WebDriverWait(driver, 10).until(
  161.         EC.visibility_of_element_located((By.NAME, "username"))
  162.         )
  163.         element.click()
  164.           #page_source(200000)
  165.  
  166.     except NoSuchElementException:
  167.         log('Element Not Found')
  168.         driver.quit()
  169.     by_name("username").clear()
  170.     by_name("username").send_keys(userdata['username'])
  171.     by_name("password").clear()
  172.     by_name("password").send_keys(userdata['password'])
  173.     by_id('loginBtn').click()
  174.     wait(.2)
  175.    
  176.     user_greeting = by_xpath('//*[@id="navigation"]/ul/li[3]/a').text
  177.     log(user_greeting)
  178.     assert 'Пользователь' in user_greeting
  179.     log('"%s" passed OK \n' % inspect.getframeinfo(inspect.currentframe())[2])
  180.  
  181. def register_user_from_admin(userdata):  
  182.     username = userdata['username']
  183.     email = userdata['email']
  184.     first_name = userdata['first_name']
  185.     last_name = userdata['last_name']
  186.     second_name = userdata['second_name']
  187.     password = userdata['password']
  188.     is_staff = userdata['is_staff']
  189.     is_superuser = userdata['is_superuser']
  190.     is_active = userdata['is_active']
  191.  
  192.     login_to_admin(ADMIN_CREDS[0],ADMIN_CREDS[1])
  193.     menu_users()    
  194.     by_link_text(u"Создать профайл").click()
  195.     wait(.1)
  196.  
  197.     by_name('username').clear()
  198.     by_name('username').send_keys(username)
  199.     wait(.1)
  200.     by_name('email').clear()
  201.     by_name('email').send_keys(email)
  202.     wait(.1)
  203.     by_name('first_name').clear()
  204.     by_name('first_name').send_keys(first_name)
  205.     wait(.1)
  206.     by_name('last_name').clear()
  207.     by_name('last_name').send_keys(last_name)
  208.     wait(.1)
  209.     by_name('second_name').clear()
  210.     by_name('second_name').send_keys(second_name)
  211.     wait(.1)
  212.     by_name('password1').clear()
  213.     by_name('password1').send_keys(password)
  214.     wait(.1)
  215.     by_name('password2').clear()
  216.     by_name('password2').send_keys(password)
  217.     wait(.1)
  218.  
  219.     if is_staff:
  220.         by_xpath('/html/body/div[4]/div[2]/div/div/div/div[2]/form/div[1]/div[1]/ul/li[5]/div[1]/ins').click()
  221.     if is_superuser:
  222.         by_xpath('/html/body/div[4]/div[2]/div/div/div/div[2]/form/div[1]/div[1]/ul/li[6]/div[1]/ins').click()
  223.     if not is_active:
  224.         by_xpath('/html/body/div[4]/div[2]/div/div/div/div[2]/form/div[1]/div[1]/ul/li[7]/div[1]/ins').click()
  225.  
  226.     wait(.1)
  227.     scr('reg')
  228.     # by_xpath('/html/body/div[4]/div[2]/div/div/div/div[2]/form/div[2]/button').click() # кнопка "Создать"
  229.  
  230.  
  231. def registered_user_deletion(username):
  232.     login_to_admin(ADMIN_CREDS[0],ADMIN_CREDS[1])
  233.     find_user(username)    
  234.     by_link_text(u"Удалить").click()
  235.     by_name("confirm").click()
  236.     by_css("ins").click()
  237.     by_css("ins").click()
  238.     by_css("button.btn.btn-red").click()
  239.     scr('deletion')
  240.     # ДОБАВИТЬ АССЕРТ
  241.  
  242. def activate_user(username):
  243.     login_to_admin(ADMIN_CREDS[0],ADMIN_CREDS[1])
  244.     find_user(username)
  245.     wait(1)
  246.     by_xpath('//*[@id="profileForm"]/div[1]/div[1]/ul/li[10]/div[1]/ins').click()
  247.     save_user()
  248.  
  249. def save_user():
  250.     by_xpath('//*[@id="saveProfileBtn"]').click()
  251.  
  252. def page_source(length=4000):    
  253.     log(driver.page_source[:length])
  254.  
  255. def curr_select():
  256.     login_to_admin(ADMIN_CREDS[0],ADMIN_CREDS[1])
  257.     wait(.2)
  258.     go('/')
  259.     wait(.2)
  260.     user_greeting = by_xpath('//*[@id="navigation"]/ul/li[3]/a').text
  261.     log(user_greeting)  
  262.     assert 'Администратор' in user_greeting # check userlogged as admin
  263.     wait(.1)
  264.     location_modal_yes()
  265.     wait(.1)
  266.     by_xpath('//*[@id="navigation"]/ul/li[3]/a').click()
  267.     wait(.5)
  268.     scr('index_admin')
  269.  
  270.     by_xpath('//*[@id="navigation"]/ul/li[3]/ul/li[4]/a').click()
  271.     wait(.5)
  272.     scr('index_admin2')
  273.  
  274. def check_title(title):
  275.     log('PAGE TITLE = ' + driver.title + '\n')    
  276.     assert title in driver.title
  277.  
  278. def suite_registration():
  279.     register_buyer(userdata)
  280.     activate_user(userdata['username'])
  281.     registered_user_deletion(userdata['username'])
  282.     register_seller(sellerdata)
  283.     activate_user(sellerdata['username'])
  284.     register_user_from_admin(userdata)
  285.     login_activated_buyer(userdata)
  286.     registered_user_deletion(userdata['username'])
  287.    
  288. def suite_product_placement():
  289.     pass
  290.  
  291.  
  292. def main():
  293.     curr_select()
  294.  
  295. if __name__ == '__main__':
  296.     logging.root.setLevel(logging.INFO)
  297.     logging.root.addHandler(logging.StreamHandler())
  298.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement