Skip to content

Commit

Permalink
Develop (#93)
Browse files Browse the repository at this point in the history
### To be filled by the PR creator:

* A brief description of the changes made - 

* Do we have clean latest run report (Docker or Browserstack) attached
with this PR?
  * [ ] Yes
  * [ ] No (Please explain why)

* Does the PR contain changes to any core file?
  * [ ] Yes (Needs approval from at least 1 people)
  * [ ] No

* Is it
  * [ ] New Testcase
  * [ ] Fix


### To be filled by the PR reviewer:

* [ ] Verify the attached run report passed in GitHub Actions (Docker or
Browserstack run)

* General
    * [ ] Use the best strategy to locate the elements
    * [ ] Comments wherever the code is not readable by itself
    * [ ] Use of the right data structure for the use case
    * [ ] Reuse logic/functionality as much as possible
    * [ ] Cleanup of any test data that is generated by the tests
    * [ ] No static waits
  • Loading branch information
Tauqir Sarwar authored Apr 10, 2024
2 parents c2f17a8 + b02920a commit 6088c30
Show file tree
Hide file tree
Showing 10 changed files with 316 additions and 1 deletion.
7 changes: 6 additions & 1 deletion frontend/test_project/features/web/web_tests.feature
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,9 @@ Feature: OrangeHRM Login and Modus QA blog
And I click item 'Contractor. Understanding' for element 'Modus_Site > Careers > form_dropdown'
And I click item 'Yes' for element 'Modus_Site > Careers > position_dropdown'
And I click item 'data protection apps upon hire' for element 'Modus_Site > Careers > form_dropdown'
And I click item 'Yes' for element 'Modus_Site > Careers > protection_dropdown'
And I click item 'Yes' for element 'Modus_Site > Careers > protection_dropdown'


@email @automated
Scenario: Email verification
When I get email for '[email protected]'
1 change: 1 addition & 0 deletions main/frontend/common/step_definitions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
from .swipe_drag_and_drop import *
from .text_assertion_editing import *
from .visual_comparison import *
from .email import *
67 changes: 67 additions & 0 deletions main/frontend/common/step_definitions/email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import structlog

import base64
import os, re
import time

import pytest
import json

from bs4 import BeautifulSoup

from main.utils.email_reader import create_json

PROJECT_DIR = os.getcwd()
test_data_dir = os.path.join(PROJECT_DIR, "test_data/files/email_data.json")

from pytest_bdd import parsers, when, then
from main.frontend.common.helpers.selenium_generics import SeleniumGenerics

logger = structlog.get_logger(__name__)


@when(parsers.re("I get email for '(?P<user_type>.*)'"),
converters=dict(user_type=str))
def check_email(user_type, selenium_generics: SeleniumGenerics):
time.sleep(5)
create_json()
from datetime import date
today = date.today()
from datetime import timedelta

yesterday = today - timedelta(days=1)
date_today = today.strftime("%d %b %Y")
date_yesterday = yesterday.strftime("%d %b %Y")
if '0' in date_today[0]:
date_today = date_today[1:]
f = open(test_data_dir)
data = json.load(f)
for i in data:
value = i
if "Test Data" in value["Subject"] or "Test Data" in value["Subject"] and \
"[email protected]" in value["From"] and date_today in value["Date"] or \
date_yesterday in value["Date"] and user_type in value["To"]:
decoded_data = base64.b64decode(value["Message"])
soup = BeautifulSoup(decoded_data, "lxml")
email_body = str(soup.body()[0])
url = re.findall('https://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*, ]|(?:%[0-9a-fA-F][0-9a-fA-F]))+',
email_body)
final_url = ""
if len(url) >= 1:
final_url = str(url[0]).replace('amp;', '')
if len(url) == 0:
decoded_data = base64.b64decode(value["Url"])
soup = BeautifulSoup(decoded_data, "lxml")
email_body = str(soup.body()[0])
url = re.findall('https://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*, ]|(?:%[0-9a-fA-F][0-9a-fA-F]))+',
email_body)
new_url = ''
for j in url:
if 'Test Data' in j or 'Test Data' in j:
new_url = j
final_url = str(new_url).replace('amp;', '')
pytest.globalDict['final_url'] = final_url
selenium_generics.navigate_to_url(final_url)

break
f.close()
1 change: 1 addition & 0 deletions main/setup/setup_tests/test_installation_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def test_check_step_definitions_folder():
assert_that(os.path.isfile("./main/frontend/common/step_definitions/click_touch_and_keyboard_actions.py")).is_true()
assert_that(os.path.isfile("./main/frontend/common/step_definitions/date_time.py")).is_true()
assert_that(os.path.isfile("./main/frontend/common/step_definitions/dropdowns.py")).is_true()
assert_that(os.path.isfile("./main/frontend/common/step_definitions/email.py")).is_true()
assert_that(os.path.isfile("./main/frontend/common/step_definitions/attribute_assertion.py")).is_true()
assert_that(os.path.isfile("./main/frontend/common/step_definitions/environment_variables.py")).is_true()
assert_that(os.path.isfile("./main/frontend/common/step_definitions/excel_and_csv.py")).is_true()
Expand Down
12 changes: 12 additions & 0 deletions main/utils/cred.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"installed": {
"client_id": "44392rcontent.com",
"project_id": "modus-pytest",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"refresh_token": "1//0ewd1ANSvascVyJs2UBeQOY",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_secret": "MGS3sWe",
"redirect_uris": ["urn:ietf:wg:oauth:2.0:oob", "http://localhost", "http://localhost:8080", "http://127.0.0.1"]
}
}
119 changes: 119 additions & 0 deletions main/utils/email_reader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
import pickle
import os.path
import json
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']
PROJECT_DIR = os.getcwd()
test_data_dir_utils = os.path.join(PROJECT_DIR, "main/utils")
test_data_dir_email = os.path.join(PROJECT_DIR, "test_data/files")
# if 'utils' in PROJECT_DIRECTORY:
# PROJECT_DIR = PROJECT_DIRECTORY.replace("utils", "test_data")
print(PROJECT_DIR)


def get_email():
count = 0
creds = None
with open(test_data_dir_utils+'/token.pickle', 'rb') as token:
creds = pickle.load(token)
with open(test_data_dir_utils+'/cred.json', 'r') as infile:
my_data = json.load(infile)

if not creds.valid:
try:
creds.refresh(Request())
except:
print("Try to refresh token failed")

if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(test_data_dir_utils+'/cred.json', SCOPES)
creds = flow.run_local_server(port=0)

with open(test_data_dir_utils+'/token.pickle', 'wb') as token:
pickle.dump(creds, token)
my_data['installed']['refresh_token'] = creds.refresh_token
with open(test_data_dir_utils+'/cred.json', 'w') as outfile:
json.dump(my_data, outfile, indent=4)

service = build('gmail', 'v1', credentials=creds)
result = service.users().messages().list(userId='me').execute()
messages = result.get('messages')
counter = 0
for msg in messages:
if counter > 10:
break
counter = counter + 1
txt = service.users().messages().get(userId='me', id=msg['id']).execute()
try:
payload = txt['payload']
headers = payload['headers']
for d in headers:
if d['name'] == 'Subject':
subject = d['value']
if d['name'] == 'From':
sender = d['value']
if d['name'] == 'To':
receiver = d['value']
if d['name'] == 'Date':
date = d['value']
part1 = payload.get('parts')[0]
part2 = payload.get('parts')[1]
data = part1['body']['data']
data = data.replace("-", "+").replace("_", "/")
url_data = part2['body']['data']
url_data = url_data.replace("-", "+").replace("_", "/")
count = count + 1
if count == 15:
break
with open(test_data_dir_email + "/email_reader.txt", "a") as f:
f.write("Subject" + " : " + subject + '\n')
f.write("From" + " : " + sender + '\n')
f.write("To" + " : " + receiver + '\n')
f.write("Date" + " : " + str(date) + '\n')
f.write("Message" + " : " + str(data) + '\n')
f.write("Url" + " : " + str(url_data) + '\n')
f.write('\n')
except:
pass

f.close()


def try_to_convert_to_int(val):
try:
val = int(val)
except:
pass
return val


def create_json():
fp = open(test_data_dir_email + "/email_reader.txt", "w")
fp.close()
jp = open(test_data_dir_email + "/email_data.json", "w")
jp.close()
get_email()

data, group = [], {}
with open(test_data_dir_email + "/email_reader.txt", "r") as f_in:
for line in map(str.strip, f_in):
if line == "":
if group:
data.append(group)
group = {}
else:
k, v = map(str.strip, line.split(':', maxsplit=1))
group[k] = try_to_convert_to_int(v) if v else None

if group:
data.append(group)

json_data = json.dumps(data, indent=4)
with open(test_data_dir_email + "/email_data.json", "w") as f_in:
f_in.write(json_data)
f_in.close()
110 changes: 110 additions & 0 deletions main/utils/email_reader_token_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
import pickle
import os.path
import base64
from bs4 import BeautifulSoup
import json

# Define the SCOPES. If modifying it, delete the token.pickle file.
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']


def getEmails():
# Variable creds will store the user access token.
# If no valid token found, we will create one.
creds = None

# The file token.pickle contains the user access token.
# Check if it exists
if os.path.exists('token.pickle'):
# Read the token from the file and store it in the variable creds
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
with open('cred.json', 'r') as infile:
my_data = json.load(infile)

# If credentials are not available or are invalid, ask the user to log in.
if not creds.valid:
try:
creds.refresh(Request())
except:
print("Try to refresh token Failed")

if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
# creds.refresh(Request())
flow = InstalledAppFlow.from_client_secrets_file('cred.json', SCOPES)
creds = flow.run_local_server(port=0)
else:
flow = InstalledAppFlow.from_client_secrets_file('cred.json', SCOPES)
creds = flow.run_local_server(port=0)

# Save the access token in token.pickle file for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
my_data['installed']['refresh_token'] = creds.refresh_token
with open('cred.json', 'w') as outfile:
json.dump(my_data, outfile, indent=4)

# Connect to the Gmail API
service = build('gmail', 'v1', credentials=creds)

# request a list of all the messages
result = service.users().messages().list(userId='me').execute()

# We can also pass maxResults to get any number of emails. Like this:
# result = service.users().messages().list(maxResults=200, userId='me').execute()
messages = result.get('messages')

# messages is a list of dictionaries where each dictionary contains a message id.

# iterate through all the messages
counter = 0
for msg in messages:
if counter > 10:
break
counter = counter + 1
# Get the message from its id
txt = service.users().messages().get(userId='me', id=msg['id']).execute()

# Use try-except to avoid any Errors
try:
# Get value of 'payload' from dictionary 'txt'
payload = txt['payload']
headers = payload['headers']

# Look for Subject and Sender Email in the headers
for d in headers:
if d['name'] == 'Subject':
subject = d['value']
if d['name'] == 'From':
sender = d['value']
if d['name'] == 'To':
receiver = d['value']

# The Body of the message is in Encrypted format. So, we have to decode it.
# Get the data and decode it with base 64 decoder.
parts = payload.get('parts')[0]
data = parts['body']['data']
data = data.replace("-", "+").replace("_", "/")
decoded_data = base64.b64decode(data)

# Now, the data obtained is in lxml. So, we will parse
# it with BeautifulSoup library
soup = BeautifulSoup(decoded_data, "lxml")
body = soup.body()
# if sender == "[email protected]":
# print("Subject: ", subject)
# Printing the subject, sender's email and message
print("Subject: ", subject)
print("From: ", sender)
print("To: ", receiver)
print("Message: ", body)
print('\n')
except:
pass


getEmails()
Binary file added main/utils/token.pickle
Binary file not shown.
Empty file added test_data/files/email_data.json
Empty file.
Empty file.

0 comments on commit 6088c30

Please sign in to comment.