Question: I have written the code below import os import sys import html import logging import colorama import coloredlogs from termcolor import colored from colorama import

I have written the code below

import os

import sys

import html

import logging

import colorama

import coloredlogs

from termcolor import colored

from colorama import Fore

import unittest

import logging

from appium import webdriver

from appium.webdriver.common.mobileby import MobileBy as AppiumBy

from logging import StreamHandler, Formatter

class ColoredFormatter(logging.Formatter):

def format(self, record):

if record.levelno == logging.ERROR:

color = 'red'

else:

color = 'green'

message = logging.Formatter.format(self, record)

return colored(message, color, attrs=['bold'])

handler = StreamHandler()

handler.setFormatter(ColoredFormatter())

logging.getLogger().addHandler(handler)

logging.basicConfig(filename="logfile.txt", level=logging.ERROR, format=ColoredFormatter)

class CalculatorTests(unittest.TestCase):

def setUp(self):

desired_caps = {}

desired_caps['platformName'] = 'Android'

desired_caps['deviceName'] = 'Pixel XL API 33'

desired_caps['appPackage'] = 'com.asus.calculator'

desired_caps['appActivity'] = 'com.asus.calculator.Calculator'

self.driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)

def teardown(self):

self.driver.quit()

def test_addition(self):

self.driver.find_element(by=AppiumBy.ID, value="com.asus.calculator:id/digit_1").click()

self.driver.find_element(by=AppiumBy.ACCESIBILITY_ID, value="plus").click()

self.driver.find_element(by=AppiumBy.ID, value="com.asus.calculator:id/digit_2").click()

self.driver.find_element(by=AppiumBy.ACCESSIBILITY_ID, value="equals").click()

result = self.driver.find_element(by=AppiumBy.ID, value='com.asus.calculator:id/result').text

self.assertEqual(int(result), 3)

if __name__ == '__main__':

suite = unittest.TestLoader().loadTestsFromTestCase(CalculatorTests)

with open('logfile.txt', 'w') as f:

runner = unittest.TextTestRunner(stream=f, descriptions=True, verbosity=2)

result = runner.run(suite)

if result.wasSuccessful():

print(colored("All tests passed", "green"))

sys.exit(1)

else:

print(colored("Tests failed", "red", attrs=['bold']))

for fail in result.failures:

print(fail)

with open("logfile.txt", "r") as logfile:

lines = logfile.readlines()

with open("colored_logfile.html", "a") as colored_logfile:

for line in lines:

if "ERROR" in line:

colored_logfile.write(colored(line,"red",attrs = ['bold'] ))

colored_logfile.write(' ')

else:

colored_logfile.write(colored(line, "green"))

colored_logfile.write(' ')

But I am unable to change the text of each and every error into red colour and bold from colored_log file can u help me what to add in the above code extra to change the text of each and every error into red

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!