Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions linkedin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager


import config
import constants
import models
Expand Down Expand Up @@ -107,11 +108,9 @@ def startApplying(self):
" jobs out of " + str(jobCounter.total) + ".", MessageTypes.SUCCESS)

except Exception as e:
logger.logDebugMessage("Unhandled exception in startApplying", MessageTypes.ERROR, e, True)
# TODO move this functionality to file.py
self.driver.save_screenshot("unhandled_exception.png")
with open("page_source_at_unhandled_exception.html", "w") as file:
file.write(self.driver.page_source)
logger.logDebugMessage("Unhandled exception in StartApplying", MessageTypes.ERROR, e, True)
resultFileWriter.captureScreenshot(self.driver, "unhandeled_exception.png")
resultFileWriter.captureHtml(self.driver, "page_source_at_unhandled_exception.html")


def goToJobsSearchPage(self):
Expand Down
39 changes: 16 additions & 23 deletions tests/base_test_class.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import unittest
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
import os
import utils.file as resultFileWriter


class BaseTestCase(unittest.TestCase):

def tearDown(self, driver: webdriver.Chrome):
"""Capture screenshot and HTML content if the test fails."""

#Capture screenshot and HTML content if the test fails
test_exceptions = self._outcome.result.errors
test_failures = self._outcome.result.failures
if any(error for (_, error) in test_exceptions) or test_failures:
Expand All @@ -16,35 +16,28 @@ def tearDown(self, driver: webdriver.Chrome):
super().tearDown()
# driver.quit()

# TODO Move this to file.py

def capture_test_failure_info(self, driver: webdriver.Chrome):
"""Capture screenshots and HTML content for debugging."""

#Capture screenshots and HTML content for debugging
try:
if not os.path.exists('data'):
os.makedirs('data')

if not os.path.exists('data/tests'):
os.makedirs('data/tests')

resultFileWriter.createDirectory('data')
resultFileWriter.createDirectory('data/tests')

id = self.id()
class_name = id.split('.')[0]
method_name = id.split('.')[-1]
className = id.split('.')[0]
methodName = id.split('.')[-1]

test_directory = os.path.join('data/tests', class_name, method_name)

if not os.path.exists(test_directory):
os.makedirs(test_directory)
testDirectory = resultFileWriter.joinPaths('data/tests', className, methodName)
resultFileWriter.createDirectory(testDirectory)

screenshot_path = os.path.join(test_directory, "screenshot.png")
html_path = os.path.join(test_directory, "page.html")
screenshotPath = resultFileWriter.joinPaths(testDirectory, "screenshot.png")
htmlPath = resultFileWriter.joinPaths(testDirectory, "page.html")

# Capture screenshot
driver.save_screenshot(screenshot_path)
resultFileWriter.captureScreenshot(driver, screenshotPath)

# Capture HTML content
with open(html_path, 'w', encoding='utf-8') as f:
f.write(driver.page_source)
resultFileWriter.captureHtml(driver, htmlPath)

except WebDriverException as e:
print(f"Failed to capture screenshot or HTML: {e}")
Expand Down
26 changes: 26 additions & 0 deletions utils/file.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import time
import os
import logging

import utils.logger as logger
from utils.logger import MessageTypes
from selenium import webdriver


def displayWriteResults(lineToWrite: str):
Expand Down Expand Up @@ -44,6 +46,30 @@ def __writeResultsIntoFile(text: str):
logger.logDebugMessage("Error in writeResults", logger.MessageTypes.ERROR, e)


def createDirectory(path: str):
if not os.path.exists(path):
os.makedirs(path)


def captureScreenshot(driver: webdriver.Chrome, screenshotPath: str):
try:
driver.save_screenshot(screenshotPath)
except Exception as e:
logging.error(f"Failed to capture screenshot: {e}")


def captureHtml(driver: webdriver.Chrome, htmlPath: str):
try:
with open(htmlPath, 'w', encoding='utf-8') as f:
f.write(driver.page_source)
except Exception as e:
logging.error(f"Failed to capture HTML: {e}")


def joinPaths(*paths: str):
return os.path.join(*paths)


# def __writeResults(text: str):
# timeStr = time.strftime("%Y%m%d")
# fileName = "Applied Jobs DATA - " +timeStr + ".txt"
Expand Down