Base

class tir.technologies.core.base.Base(config_path='', autostart=True)

Base class for any technology to implement Selenium Interface Tests.

This class instantiates the browser, reads the config file and prepares the log.

If no config_path is passed, it will read the config.json file that exists in the same folder as the file that would execute this module.

Parameters
  • config_path (str) – The path to the config file. - Default: “” (empty string)

  • autostart – Sets whether TIR should open browser and execute from the start. - Default: True

Type

bool

Usage:

The Base class must be inherited by every internal class of each technology that would exist in this module.

The classes must be declared under pwa/technologies/ folder.

>>> def WebappInternal(Base):
>>> def APWInternal(Base):
AssertFalse()

Defines that the test case expects a False response to pass

Usage:

>>> #Calling the method
>>> oHelper.AssertFalse()
AssertTrue()

Defines that the test case expects a True response to pass

Usage:

>>> #Calling the method
>>> oHelper.AssertTrue()
SetTIRConfig(config_name, value)

Changes a value of a TIR internal config during runtime.

This could be useful for TestCases that must use a different set of configs than the ones defined at config.json

Available configs:

  • Url - str

  • Environment - str

  • User - str

  • Password - str

  • Language - str

  • DebugLog - str

  • TimeOut - int

  • InitialProgram - str

  • Routine - str

  • Date - str

  • Group - str

  • Branch - str

  • Module - str

Parameters
  • config_name (str) – The config to be changed.

  • value (str) – The value that would be set.

Usage:

>>> # Calling the method:
>>> oHelper.SetTIRConfig(config_name="date", value="30/10/2018")
Start()

Opens the browser maximized and goes to defined URL.

Usage:

>>> # Calling the method:
>>> oHelper.Start()
TearDown()

Closes the webdriver and ends the test case.

Usage:

>>> #Calling the method
>>> oHelper.TearDown()
assert_result(expected)

[Internal]

Asserts the result based on the expected value.

Parameters

expected (bool) – Expected value

Usage :

>>> #Calling the method:
>>> self.assert_result(True)
click(element, click_type=<ClickType.JS: 1>, right_click=False)

[Internal]

Clicks on the Selenium element.

Supports three types of clicking: JavaScript, pure Selenium and Selenium’s ActionChains.

Default is JavaScript clicking.

Parameters
  • element (Selenium object) – Selenium element

  • click_type (enum.ClickType) – ClickType enum. - Default: enum.ClickType.JS

  • right_click – Clicks with the right button of the mouse in the last element of the tree.

Usage:

>>> #Defining the element:
>>> element = lambda: self.driver.find_element_by_id("example_id")
>>> #Calling the method
>>> self.click(element(), click_type=enum.ClickType.JS)
compare_field_values(field, user_value, captured_value, message)

[Internal]

Validates and stores field in the self.errors array if the values are different.

Parameters
  • field (str) – Field name

  • user_value (str) – User input value

  • captured_value (str) – Interface captured value

  • message (str) – Error message if comparison fails

Usage:

>>> #Calling the method
>>> self.compare_field_values("A1_NOME", "JOÃO", "JOOÃ", "Field A1_NOME has different values")
double_click(element)

[Internal]

Clicks two times on the Selenium element.

Parameters

element (Selenium object) – Selenium element

Usage:

>>> #Defining the element:
>>> element = lambda: self.driver.find_element_by_id("example_id")
>>> #Calling the method
>>> self.double_click(element())
element_exists(term, scrap_type=<ScrapType.TEXT: 1>, position=0, optional_term='', main_container='.tmodaldialog, .ui-dialog')

[Internal]

Returns a boolean if element exists on the screen.

Parameters
  • term (str) – The first term to use on a search of element

  • scrap_type (enum.ScrapType) – Type of element search. - Default: enum.ScrapType.TEXT

  • position (int) – Position which element is located. - Default: 0

  • optional_term (str) – Second term to use on a search of element. Used in MIXED search. - Default: “” (empty string)

Returns

True if element is present. False if element is not present.

Return type

bool

Usage:

>>> element_is_present = element_exists(term=".ui-dialog", scrap_type=enum.ScrapType.CSS_SELECTOR)
>>> element_is_present = element_exists(term=".tmodaldialog.twidget", scrap_type=enum.ScrapType.CSS_SELECTOR, position=initial_layer+1)
>>> element_is_present = element_exists(term=text, scrap_type=enum.ScrapType.MIXED, optional_term=".tsay")
filter_displayed_elements(elements, reverse=False)

[Internal]

Receives a BeautifulSoup element list and filters only the displayed elements.

Parameters
  • elements (List of BeautifulSoup objects) – BeautifulSoup element list

  • reverse (bool) – Boolean value if order should be reversed or not. - Default: False

Returns

List of filtered BeautifulSoup elements

Return type

List of BeautifulSoup objects

Usage:

>>> #Defining the element list:
>>> soup = self.get_current_DOM()
>>> elements = soup.select("div")
>>> #Calling the method
>>> self.filter_displayed_elements(elements, True)
find_first_div_parent(element)

[Internal]

Finds first div parent element of another BeautifulSoup element.

If element is already a div, it will return the element.

Parameters

element (BeautifulSoup object) – BeautifulSoup element

Returns

The first div parent of the element

Return type

BeautifulSoup object

Usage:

>>> parent_element = self.find_first_div_parent(my_element)
find_label_element(label_text, container)

[Internal]

Find input element next to label containing the label_text parameter.

Parameters
  • label_text (str) – The label text to be searched

  • container (BeautifulSoup object) – The main container object to be used

Returns

A list containing a BeautifulSoup object next to the label

Return type

List of BeautifulSoup objects

Usage:

>>> self.find_label_element("User:", container_object)
get_current_DOM()

[Internal]

Returns current HTML DOM parsed as a BeautifulSoup object

Returns

BeautifulSoup parsed DOM

Return type

BeautifulSoup object

Usage:

>>> #Calling the method
>>> soup = self.get_current_DOM()
get_element_text(element)

[Internal]

Gets element text.

Parameters

element (Selenium object) – Selenium element

Returns

Element text

Return type

str

Usage:

>>> #Defining the element:
>>> element = lambda: self.driver.find_element_by_id("example_id")
>>> #Calling the method
>>> text = self.get_element_text(element())
get_element_value(element)

[Internal]

Gets element value.

Parameters

element (Selenium object) – Selenium element

Returns

Element value

Return type

str

Usage:

>>> #Defining the element:
>>> element = lambda: self.driver.find_element_by_id("example_id")
>>> #Calling the method
>>> text = self.get_element_value(element())
log_error(message, new_log_line=True)

[Internal]

Finishes execution of test case with an error and creates the log information for that test.

Parameters
  • message (str) – Message to be logged

  • new_log_line (bool) – Boolean value if Message should be logged as new line or not. - Default: True

Usage:

>>> #Calling the method:
>>> self.log_error("Element was not found")
move_to_element(element)

[Internal]

Move focus to element on the screen.

Parameters

element (Selenium object) – Selenium element

Usage:

>>> #Defining an element:
>>> element = lambda: self.driver.find_element_by_id("example_id")
>>> #Calling the method
>>> self.scroll_to_element(element())
normalize_config_name(config_name)

[Internal]

Normalizes the config name string to respect the config object naming convention.

Parameters

config_name (str) – The config name string to be normalized.

Returns

The config name string normalized.

Return type

str

Usage:

>>> # Calling the method:
>>> normalized_name = self.normalize_config_name("InitialProgram") # "initial_program"
scroll_to_element(element)

[Internal]

Scroll to element on the screen.

Parameters

element (Selenium object) – Selenium element

Usage:

>>> #Defining an element:
>>> element = lambda: self.driver.find_element_by_id("example_id")
>>> #Calling the method
>>> self.scroll_to_element(element())
search_stack(function)

Returns True if passed function is present in the call stack.

Parameters

function (str) – Name of the function

Returns

Boolean if passed function is present or not in the call stack.

Return type

bool

Usage:

>>> # Calling the method:
>>> is_present = self.search_stack("MATA020")
search_zindex(element)

[Internal]

Returns zindex value of BeautifulSoup object.

Internal function created to be used inside lambda of zindex_sort method.

Only works if element has Style attribute.

Parameters

element (BeautifulSoup object) – BeautifulSoup element

Returns

z-index value

Return type

int

Usage:

>>> #Line extracted from zindex_sort method:
>>> elements.sort(key=lambda x: self.search_zindex(x), reverse=reverse)
select_combo(element, option)

Selects the option on the combobox.

Parameters
  • element (Beautiful Soup object) – Combobox element

  • option (str) – Option to be selected

Usage:

>>> #Calling the method:
>>> self.select_combo(element, "Chosen option")
send_keys(element, arg)

[Internal]

Clicks two times on the Selenium element.

Parameters
  • element (Selenium object) – Selenium element

  • arg (str or selenium.webdriver.common.keys) – Text or Keys to be sent to the element

Usage:

>>> #Defining the element:
>>> element = lambda: self.driver.find_element_by_id("example_id")
>>> #Calling the method with a string
>>> self.send_keys(element(), "Text")
>>> #Calling the method with a Key
>>> self.send_keys(element(), Keys.ENTER)
set_element_focus(element)

[Internal]

Sets focus on element.

Parameters

element (Selenium object) – Selenium element

Usage:

>>> #Defining the element:
>>> element = lambda: self.driver.find_element_by_id("example_id")
>>> #Calling the method
>>> text = self.set_element_focus(element())
soup_to_selenium(soup_object)

[Internal]

An abstraction of the Selenium call to simplify the conversion of elements.

Parameters

soup_object (BeautifulSoup object) – The BeautifulSoup object to be converted.

Returns

The object converted to a Selenium object.

Return type

Selenium object

Usage:

>>> # Calling the method:
>>> selenium_obj = lambda: self.soup_to_selenium(bs_obj)
take_screenshot(filename)

[Internal]

Takes a screenshot and saves on the screenshot folder defined in config.

Parameters

filename – The name of the screenshot file.

Type

str

Usage:

>>> # Calling the method:
>>> self.take_screenshot(filename="myscreenshot")
web_scrap(term, scrap_type=<ScrapType.TEXT: 1>, optional_term=None, label=False, main_container=None)

[Internal]

Returns a BeautifulSoup object list based on the search parameters.

Does not support ScrapType.XPATH as scrap_type parameter value.

Parameters
  • term (str) – The first search term. A text or a selector

  • scrap_type (enum.ScrapType.) – The type of webscraping. - Default: enum.ScrapType.TEXT

  • optional_term (str) – The second search term. A selector used in MIXED webscraping. - Default: None

  • label (bool) – If the search is based on a label near the element. - Default: False

  • main_container (str) – The selector of a container element that has all other elements. - Default: None

Returns

List of BeautifulSoup4 elements based on search parameters.

Return type

List of BeautifulSoup4 objects

Usage:

>>> #All buttons
>>> buttons = self.web_scrap(term="button", scrap_type=enum.ScrapType.CSS_SELECTOR)
>>> #----------------#
>>> #Elements that contain the text "Example"
>>> example_elements = self.web_scrap(term="Example")
>>> #----------------#
>>> #Elements with class "my_class" and text "my_text"
>>> elements = self.web_scrap(term="my_text", scrap_type=ScrapType.MIXED, optional_term=".my_class")
zindex_sort(elements, reverse=False)

[Internal]

Sorts list of BeautifulSoup elements based on z-index style attribute.

Only works if elements have Style attribute.

Parameters
  • elements (List of BeautifulSoup objects) – BeautifulSoup element list

  • reverse (bool) – Boolean value if order should be reversed or not. - Default: False

Returns

List of sorted BeautifulSoup elements based on zindex.

Return type

List of BeautifulSoup objects

Usage:

>>> #Defining the element list:
>>> soup = self.get_current_DOM()
>>> elements = soup.select("div")
>>> #Calling the method
>>> self.zindex_sort(elements, True)