简体   繁体   中英

Capturing output from Selenium and Python

I am new to python and testing. I have to do automation testing. I am using the following code to test my webApplication. I am able to use excel to read input data and write test output as Pass or Fail using programming logics without using Assert.

When I am using assert, I am getting output in console, but I am not able to capture it and so I can not push it in excel.

Code I am using is

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.keys import Keys
import unittest
import os

error = "Houstan you got some error"
assert 2 + 2 == 5, 'error'

Output in console

xample$ python assert.py
Traceback (most recent call last):
 File "assert.py", line 10, in <module>
   assert 2 + 2 == 5, error
AssertionError: Houstan you got some error

Can someone help me out to capture this output in some variable so that I can push them in excel in organized way. May be if data can be stored in different variables or in a list.

testdoc.xls

File ----------- Line ------ Error

assert.py --- 10 ------ Houstan you got some error

Consider using the logging library.

import logging

logging.basicConfig(filename='foo.log',
                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                    level=logging.INFO)
try:
    2 + 2 == 5
except Exception as e:
    logging.error('error msg', exc_info=True)

the traceback module is useful for formating and processing error information. for instance

import unittest
import os
import traceback

try:
    error = "Houstan you got some error"
    assert 2 + 2 == 5, 'error'
except Exception:
    var = traceback.format_exc()
    # now your traceback is in the variable var
    print ('not failed', var)
    # process the var as you want, then re raise the error
    raise

doc is full with examples

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM