Utils
Table of Contents
What is this repository about?
I made this repository as a way to keep my short and simple scripts using the python library faker
, appium
and tkinter
for automation.
You can find the repository here.
Person
"""
This module generates random personal and business information using the Faker
library for Brazilian Portuguese locale. It includes functions for generating
names, emails, identification numbers (CNPJ, CPF), phone numbers, passwords,
and birth dates.
"""
import unicodedata
import random
from faker import Faker
fake = Faker('pt_BR')
def create_random_full_name():
"""
Generates a random full name using the Faker library.
Returns:
str: A randomly generated full name in Portuguese (Brazilian).
"""
name = fake.name()
return name
def create_random_first_name():
"""
Generates a random first name using the Faker library.
Returns:
str: A randomly generated first name in Portuguese (Brazilian).
"""
while True:
name = fake.first_name()
# Verificar se o nome tem menos de 3 caracteres
if len(name) < 3:
continue
# Verificar se o nome contém acentos
normalized_name = unicodedata.normalize('NFKD', name).encode('ASCII', 'ignore').decode('ASCII')
if name != normalized_name:
continue
return name
def create_random_surname():
"""
Generates a random surname using the Faker library.
Returns:
str: A randomly generated surname in Portuguese (Brazilian).
"""
name = fake.name().split(' ')[1]
return name
def create_random_email():
"""
Generates a random email address using the Faker library.
Returns:
str: A randomly generated email address.
"""
email = fake.email()
return email
def create_cnpj():
"""
Generates a random CNPJ (Cadastro Nacional da Pessoa Jurídica), which is a
Brazilian business identification number, using the Faker library.
Returns:
str: A randomly generated CNPJ number in Brazilian format.
"""
cnpj = fake.cnpj()
return cnpj
def create_cpf():
"""
Generates a random CPF (Cadastro de Pessoas Físicas), which is a Brazilian
personal identification number, using the Faker library.
Returns:
str: A randomly generated CPF number in Brazilian format.
"""
cpf = fake.cpf()
return cpf
def create_phone():
"""
Generates a random phone number within a range for Brazilian phone numbers.
Returns:
int: A randomly generated phone number in numeric format.
"""
phone = random.randint(1111111111, 99999999999)
return phone
def create_password():
"""
Generates a random password using the Faker library.
Returns:
str: A randomly generated password.
"""
password = fake.password()
return password
def create_birth_day():
"""
Generates a random date of birth for an individual born in or before 2003,
formatted in the Brazilian day/month/year format.
Returns:
str: A randomly generated birth date in "DD/MM/YYYY" format.
"""
while True:
birth_day = fake.date_of_birth()
if birth_day.year <= 2003:
break
birth_day_br = birth_day.strftime("%d/%m/%Y")
return birth_day_br
Card
"""
This module generates random credit card details, specifically for Mastercard,
using the Faker library. The details returned include the card name, the cardholder's name,
the card number, expiration date, and CVV.
"""
from faker import Faker
fake = Faker('pt_BR')
def create_card():
"""
Generates a random Mastercard credit card with full details.
The function uses the Faker library to generate a Mastercard credit card's
full details, including:
- Card type (Mastercard)
- Cardholder's name
- Card number
- Expiration date
- CVV (Card Verification Value)
Returns:
tuple: A tuple containing the following details:
- str: Card type (always "Mastercard")
- str: Cardholder's name
- str: Card number
- str: Expiration date (in MM/YY format)
- str: CVV number
"""
while True:
card = fake.credit_card_full()
lines = card.splitlines()
if lines[0] == 'Mastercard':
master_card = lines[0]
nome = lines[1]
codigo, exp = lines[2].split(' ', 1)
cvv = lines[3].split('CVV: ')[1].strip()
return master_card, nome, codigo, exp, cvv
def create_bank_account():
"""
Generates a random bank account with agency, account number, and digit.
The function uses the Faker library to generate a bank account's details,
including:
- Agency number (4 digits)
- Account number
- Account digit
Returns:
tuple: A tuple containing the following details:
- str: Agency number
- str: Account number
- str: Account digit
"""
agency = fake.random_number(digits=4, fix_len=True)
account_number = ''.join([str(fake.random_digit()) for _ in range(10)])
account_digit = fake.random_digit()
return str(agency), account_number, account_digit
Address
"""
This module generates random Brazilian address details using the Faker library.
The details returned include the street name, street number, city, postal code (CEP),
and state.
"""
from faker import Faker
fake = Faker('pt_BR')
def create_address():
"""
Generates a random Brazilian address with full details.
The function uses the Faker library to generate a Brazilian address,
which includes:
- Street name (rua)
- Street number (numero)
- City (cidade)
- Postal code (CEP)
- State (estado)
The address is split into multiple lines, and the function extracts and cleans
each part of the address.
Returns:
tuple: A tuple containing the following details:
- str: Street name (rua)
- str: Street number (numero)
- str: City (cidade)
- str: Postal code (CEP)
- str: State (estado)
"""
while True:
address = fake.address()
lines = address.splitlines()
try:
rua, numero = lines[0].split(',', 1)
except ValueError:
continue
cidade = lines[1]
try:
cep, estado = lines[2].split(' ', 1)
except ValueError:
continue
estado = estado.split('/')[1]
if not numero.strip():
continue
return rua.strip(), numero.strip(), cidade.strip(), cep.strip(), estado.strip()
Get user imput
"""
This module provides a function to get user input via a simple dialog box
using tkinter and customtkinter libraries.
"""
from tkinter import simpledialog
import customtkinter as tk
def get_user_input(prompt):
"""
Displays a dialog box to get user input in a tkinter-based application.
This function creates a hidden tkinter window and opens an input dialog
to prompt the user for a response.
Args:
prompt (str): The message displayed to the user in the input dialog.
Returns:
str: The user input as a string. If the user cancels or closes the dialog,
returns None.
"""
root = tk.CTk()
root.withdraw() # Hide the main window
user_input = simpledialog.askstring("Input", prompt)
return user_input
Mobile gestures
"""
This module contains utility functions for performing mobile gestures
"""
import os
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.actions import interaction
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.actions.pointer_input import PointerInput
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from appium.webdriver.common.appiumby import AppiumBy
def app_swipe(self, x1: int, y1: int, x2: int, y2: int):
"""
Simulates a swipe gesture on the screen from one point to another.
Args:
x1 (int): The x-coordinate of the starting point.
y1 (int): The y-coordinate of the starting point.
x2 (int): The x-coordinate of the destination point.
y2 (int): The y-coordinate of the destination point.
This method uses the ActionChains API to simulate a touch swipe action,
starting from the coordinates (x1, y1) and ending at (x2, y2).
"""
actions = ActionChains(self.driver)
actions.w3c_actions = ActionBuilder(self.driver, mouse=PointerInput
(interaction.POINTER_TOUCH, "touch"))
actions.w3c_actions.pointer_action.move_to_location(x1, y1)
actions.w3c_actions.pointer_action.pointer_down()
actions.w3c_actions.pointer_action.move_to_location(x2, y2)
actions.w3c_actions.pointer_action.release()
actions.perform()
def app_tap(self, x, y):
"""
Simulates a tap gesture on the screen at the specified coordinates.
Args:
x (int): The x-coordinate of the tap location.
y (int): The y-coordinate of the tap location.
This method uses the tap API to simulate a single touch tap at the coordinates (x, y).
"""
self.driver.tap([(x, y)])
def screenshot_path_mobile():
"""
Generates the path where the screenshot will be saved.
"""
screenshot_dir = r'Images\Screenshots'
os.makedirs(screenshot_dir, exist_ok=True)
screenshot_path = os.path.join(screenshot_dir, 'screenshot.png')
return screenshot_path
def take_screenshot_mobile(self):
"""
Takes a screenshot of the current screen and saves it to the 'Images/Screenshots' directory.
"""
screenshot_path = screenshot_path_mobile()
self.driver.save_screenshot(screenshot_path)
return screenshot_path
def swipe_to_element(wait, uiautomator_selector: str, driver):
pass