Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from typing import re
- import selenium.common.exceptions as seleniumExceptions
- from selenium import webdriver
- from selenium.webdriver.common.by import By
- from selenium.webdriver.common.keys import Keys
- from selenium.webdriver.support import expected_conditions as EC
- from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
- class TMS:
- def __init__(self, address):
- self.driver = webdriver.Chrome()
- self.driver.get(address)
- def get_web_driver(self):
- return self.driver
- def close(self):
- self.driver.close()
- def login(self):
- login = input('Login: ')
- password = input('Password: ')
- self.__login__(login, password)
- def __login__(self, login, password):
- login_field, pass_field = self.__get_login_fields__()
- login_field.send_keys(login)
- pass_field.send_keys(password)
- pass_field.send_keys(Keys.ENTER)
- def __get_login_fields__(self):
- form = self.driver.find_element_by_xpath("//form[1]")
- username = form.find_element_by_xpath("//input[@name='login']")
- password = form.find_element_by_xpath("//input[@name='password']")
- return username, password
- class TMSSelector:
- def __init__(self, tms):
- self.__tms__ = tms
- self.__kill_popout__()
- self.__init_buttons__()
- def __init_buttons__(self):
- clients_button_xpath = "//button[text()='{0}']".format('Zmień klienta i aplikację')
- self.__clients_button__ = self.__tms__.get_web_driver().find_element_by_xpath(clients_button_xpath)
- self.__init_menu_buttons__()
- def __init_menu_buttons__(self):
- self.__wait_for_menu__()
- self.__side_menu__ = TMSSideMenu(self.__tms__.get_web_driver())
- def __kill_popout__(self):
- popout = ClientWindow(self.get_popout_window_by_header('Klienci i Aplikacje'))
- popout.cancel()
- def get_popout_window_by_header(self, header):
- WebDriverWait(self.__tms__.get_web_driver(), 10).until(EC.text_to_be_present_in_element((By.CLASS_NAME, "x-window-header-text"), header))
- return self.__tms__.get_web_driver().find_element_by_css_selector(".x-window.x-resizable-pinned")
- def choose_new_client(self, client):
- client_window = ClientWindow(self.__open_new_client_window__())
- try:
- client_window.select_client(client)
- except seleniumExceptions.NoSuchElementException as e:
- client_window.cancel()
- raise e
- client_window.submit()
- self.__wait_for_client_change__(client)
- def add_new_device(self, params):
- device_window = DeviceWindow(self.__open_new_device_window__())
- try:
- device_window.fill_fields(params)
- except Exception as e:
- device_window.cancel()
- raise e
- device_window.submit()
- def __open_new_client_window__(self):
- self.__clients_button__.click()
- return self.get_popout_window_by_header('Klienci i Aplikacje')
- def __wait_for_client_change__(self, client):
- xpath = "//div[@class='x-toolbar x-small-editor']//td/span[contains(text(), 'Klient:')]"
- WebDriverWait(self.__tms__.get_web_driver(), 10).until(EC.text_to_be_present_in_element((By.XPATH, xpath), client))
- def __open_new_device_window__(self):
- self.__open_new_window__('Urządzenia')
- return self.get_popout_window_by_header('Urządzenie')
- def __open_new_seller_window__(self):
- self.__open_new_window__('Sprzedawcy')
- return self.get_popout_window_by_header('Sprzedawca')
- def __open_new_terminal_window__(self):
- self.__open_new_window__('Terminale')
- return self.get_popout_window_by_header('Terminale')
- def __open_new_group_window__(self):
- self.__open_new_window__('Terminale', 3)
- return self.get_popout_window_by_header('Grupy terminali')
- def __open_new_window__(self, menu_tab, in_list_position=1):
- selection = self.__side_menu__.select(menu_tab)
- self.__wait_for_menu_unfold__()
- new_device = selection.find_elements_by_xpath(".//a[@class='x-tree-node-anchor']")
- new_device[in_list_position].click()
- return new_device[in_list_position]
- def __wait_for_menu__(self):
- WebDriverWait(self.__tms__.get_web_driver(), 10).until(EC.presence_of_element_located((By.ID, 'Menu')))
- def __wait_for_menu_unfold__(self):
- current_row = self.__side_menu__.get_current_row()
- WebDriverWait(self.__tms__.get_web_driver(), 10).until(EC.visibility_of((current_row.find_element_by_class_name("x-tree-node-anchor"))))
- class Menu:
- def select(self, field):
- pass
- def selected_field(self):
- pass
- class NoSuchMenuField(Exception):
- def __init__(self, msg=''):
- super(Menu.NoSuchMenuField, self).__init__(msg)
- self.msg = msg
- class TMSSideMenu(Menu):
- def __init__(self, window):
- self.__init_fields__(window)
- self.__selected_field__ = ''
- self.__window__ = window
- def __init_fields__(self, window):
- rows = window.find_elements_by_css_selector('.x-panel.x-tree')
- keys = [v.find_element_by_class_name('x-panel-header-text').get_attribute('innerHTML') for v in rows]
- self.__fields__ = {k: r for k, r in zip(keys, rows)}
- def selected_field(self):
- return self.__selected_field__
- def select(self, field):
- if field not in self.__fields__:
- raise Menu.NoSuchMenuField("No field '{0}' in TMSSideMenu".format(field))
- try:
- self.__fields__[field].click()
- except seleniumExceptions.StaleElementReferenceException:
- self.__refresh__()
- return self.select(field)
- else:
- self.__selected_field__ = field
- return self.__fields__[field]
- def get_current_row(self):
- return self.__fields__[self.selected_field()]
- def __refresh__(self):
- self.__init_fields__(self.__window__)
- class Window:
- def __init__(self, window):
- self.window = window
- def submit(self):
- xpath = ".//button[text()='{0}']"
- close_button = self.window.find_element_by_xpath(xpath.format('OK'))
- close_button.click()
- def get_window(self):
- return self.window
- def cancel(self):
- xpath = ".//button[text()='{0}']"
- close_button = self.window.find_element_by_xpath(xpath.format('OK'))
- close_button.click()
- class ClientWindow (Window):
- def __init__(self, window):
- super(ClientWindow, self).__init__(window)
- def submit(self):
- xpath = ".//button[text()='{0}']"
- close_button = self.window.find_element_by_xpath(xpath.format('OK'))
- close_button.click()
- def cancel(self):
- xpath = ".//button[text()='{0}']"
- close_button = self.window.find_element_by_xpath(xpath.format('Anuluj'))
- close_button.click()
- def select_client(self, client):
- xpath = ".//div[@class='x-grid3-cell-inner x-grid3-col-columnId'][text()='{0}']"
- client_row = self.window.find_element_by_xpath(xpath.format(client))
- client_row.click()
- class NewWindow (Window):
- def __init__(self, window):
- super(NewWindow, self).__init__(window)
- def submit(self):
- xpath = ".//button[text()='{0}']"
- close_button = self.window.find_element_by_xpath(xpath.format('Zapisz'))
- close_button.click()
- def cancel(self):
- xpath = ".//button[text()='{0}']"
- close_button = self.window.find_element_by_xpath(xpath.format('Anuluj'))
- close_button.click()
- class DeviceWindow (NewWindow):
- def __init__(self, window):
- super(DeviceWindow, self).__init__(window)
- self.__get_inputs__()
- def __get_inputs__(self):
- fieldset = FieldSet(self.__get_fieldset__())
- self.__fields__ = fieldset.get_inputs_dict()
- def __get_fieldset__(self):
- fieldset_xpath = ".//fieldset[@class=' x-fieldset x-form-label-right']"
- return self.window.find_element_by_xpath(fieldset_xpath)
- def fill_fields(self, values):
- assert len(values) == len(self.__fields__)
- for val, k in zip(values, self.__fields__):
- self.__fields__[k].send_keys(val)
- class FieldSet:
- def __init__(self, fieldset):
- self.fieldset = fieldset
- def __get_div_wrappers__(self):
- items_xpath = ".//div[@class='x-form-item ']"
- fields = self.fieldset.find_elements_by_xpath(items_xpath)
- return fields
- @staticmethod
- def __get_input_fields__(wrappers):
- return [FieldSet.__get_input_field__(el) for el in wrappers]
- @staticmethod
- def __get_input_field__(wrapper):
- pre_xpath = "./div[@class='x-form-element']/"
- xpath = pre_xpath + "/input[@type='text']"
- try:
- input_field = wrapper.find_element_by_xpath(xpath)
- except seleniumExceptions.NoSuchElementException:
- xpath = pre_xpath + 'textarea'
- input_field = wrapper.find_element_by_xpath(xpath)
- return input_field
- @staticmethod
- def __get_labels__(wrappers):
- return [w.find_element_by_xpath('./label').get_attribute('innerHTML') for w in wrappers]
- def get_inputs_dict(self):
- wrappers = self.__get_div_wrappers__()
- inputs = self.__get_input_fields__(wrappers)
- keys = self.__get_labels__(wrappers)
- return {k[:-1]: f for k, f in zip(keys, inputs)}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement