BeautifulSoup Python printing out the extracted data from a table the 2nd column is dropping onto a new line. How ot keep it on same line

Riaz Ladhani

I have extracted the data I require from the HTML using BeautifulSoup. I am printing out the data into an email. The data from the 2nd column where it says "pass" is dropping onto a newline in the email body. I would like to keep the "pass" text onto the same line as the test case name.

The sample email body is:

ClearCore 5_1_1 Automated GUI Test_IE11_Selenium_VM Test Report 

Status: Pass 89 Error 1

test_000001_login_valid_user
pass
test_000002_select_a_project
pass
test_000003_verify_Lademo_CRM_DataPreview_is_present
pass
test_000004_view_data_preview_Lademo_CRM_and_test_scrollpage
pass

I would like the output to be (would be nice to have the pass aligned nicely in a column):

ClearCore 5_1_1 Automated GUI Test_IE11_Selenium_VM Test Report 

Status: Pass 89 Error 1    

test_000001_login_valid_user                                  pass
test_000002_select_a_project                                  pass
test_000003_verify_Lademo_CRM_DataPreview_is_present          pass
test_000004_view_data_preview_Lademo_CRM_and_test_scrollpage  pass

My code to extract the data is:

def extract_testcases_from_report_htmltestrunner():
    filename = (r"E:\test_runners 2 edit project\selenium_regression_test_5_1_1\TestReport\ClearCore501_Automated_GUI_TestReport.html")
    html_report_part = open(filename,'r')
    soup = BeautifulSoup(html_report_part, "html.parser")
    for div in soup.select("#result_table tr div.testcase"):
          yield div.text.strip().encode('utf-8'), div.find_next("a").text.strip().encode('utf-8')

My email code:

from email.mime.text import MIMEText
def send_report_summary_from_htmltestrunner_selenium_report():
    msg = MIMEText("\n ClearCore 5_1_1 Automated GUI Test_IE11_Selenium_VM Test Report \n " + "\n" +
                   "".join([' - '.join(seq) for seq in extract_status_from_report_htmltestrunner()]) + "\n\n" +
                   '\n'.join([elem
                              for seq in extract_testcases_from_report_htmltestrunner()
                              for elem in seq]) + "\n" +
                    "\n Report location = : \\\storage-1\Testing\Selenium_Test_Report_Results\ClearCore_5_1_1\Selenium VM\IE11 \n")

    msg['Subject'] = "ClearCore 5_1_1 Automated GUI Test"
    msg['to'] = "[email protected]"
    msg['From'] = "[email protected]"

    s = smtplib.SMTP()
    s.connect(host=SMTP_SERVER)
    s.sendmail(msg['From'], msg['To'], msg.as_string())
    s.close()

How can i format it so pass is nearly aligned on the same line as the test case name? Instead of pass dropping onto a new line every time.

Thanks, Riaz

The HTML snippet is if it helps:

    <table id='result_table'>
<colgroup>
<col align='left' />
<col align='right' />
<col align='right' />
<col align='right' />
<col align='right' />
<col align='right' />
</colgroup>
<tr id='header_row'>
    <td>Test Group/Test case</td>
    <td>Count</td>
    <td>Pass</td>
    <td>Fail</td>
    <td>Error</td>
    <td>View</td>
</tr>

<tr class='passClass'>
    <td>Regression_TestCase.RegressionProjectEdit_TestCase.RegressionProject_TestCase_Project_Edit</td>
    <td>75</td>
    <td>75</td>
    <td>0</td>
    <td>0</td>
    <td><a href="javascript:showClassDetail('c1',75)">Detail</a></td>
</tr>

<tr id='pt1.1' class='hiddenRow'>
    <td class='none'><div class='testcase'>test_000001_login_valid_user</div></td>
    <td colspan='5' align='center'>

    <!--css div popup start-->
    <a class="popup_link" onfocus='this.blur();' href="javascript:showTestDetail('div_pt1.1')" >
        pass</a>

    <div id='div_pt1.1' class="popup_window">
        <div style='text-align: right; color:red;cursor:pointer'>
        <a onfocus='this.blur();' onclick="document.getElementById('div_pt1.1').style.display = 'none' " >
           [x]</a>
        </div>
        <pre>

pt1.1: *** test_login_valid_user ***
test login with a valid user - Passed


        </pre>
    </div>
    <!--css div popup end-->

    </td>
</tr>

<tr id='pt1.2' class='hiddenRow'>
    <td class='none'><div class='testcase'>test_000002_select_a_project</div></td>
    <td colspan='5' align='center'>

    <!--css div popup start-->
    <a class="popup_link" onfocus='this.blur();' href="javascript:showTestDetail('div_pt1.2')" >
        pass</a>

    <div id='div_pt1.2' class="popup_window">
        <div style='text-align: right; color:red;cursor:pointer'>
        <a onfocus='this.blur();' onclick="document.getElementById('div_pt1.2').style.display = 'none' " >
           [x]</a>
        </div>
        <pre>

pt1.2: *** test_login_valid_user ***
test login with a valid user - Passed
*** test_select_a_project ***
08_12_1612_08_03
Selenium_Regression_Edit_Project_Test


        </pre>
    </div>
    <!--css div popup end-->

    </td>
</tr>

<tr id='pt1.3' class='hiddenRow'>
    <td class='none'><div class='testcase'>test_000003_verify_Lademo_CRM_DataPreview_is_present</div></td>
    <td colspan='5' align='center'>

    <!--css div popup start-->
    <a class="popup_link" onfocus='this.blur();' href="javascript:showTestDetail('div_pt1.3')" >
        pass</a>

    <div id='div_pt1.3' class="popup_window">
        <div style='text-align: right; color:red;cursor:pointer'>
        <a onfocus='this.blur();' onclick="document.getElementById('div_pt1.3').style.display = 'none' " >
           [x]</a>
        </div>
        <pre>

pt1.3: *** test_login_valid_user ***
test login with a valid user - Passed
*** test_select_a_project ***
08_12_1612_08_03
Selenium_Regression_Edit_Project_Test
*** Test verify_Lademo_CRM_DataPreview_is_present ***
aSelenium_LADEMO_CRM_DONOTCHANGE
File
498


        </pre>
    </div>
    <!--css div popup end-->

    </td>
</tr>
dblclik

The issue that you're having is that your code returns the items from extract_testcases_from_report_htmltestrunner() in a singular list that it then joins with the '\n' character. As a simple example, try this test code that replicates your code:

def test_yield(n):
    for i in range(n):
        yield str(i), str(i+1)
print '\n'.join([elem for seg in test_yield(5) for elem in seg])

That code should return the string: '0\n1\n1\n2\n2\n3\n3\n4\n4\n5'

You need to loop over the elements that come back from your function above and first join those with either a TAB (\t) character, or, better yet, feed those data elements into the framework of an HTML table array if your email message is HTML enabled. Here's a demo of the \t then \n approach, but you can add strings and formatting to make the HTML Table method work:

'\n'.join(elem for elem in ['\t'.join(e) for e in test_yield(5)])

This will first create a list of the elements of test_yield(), then separate those elements with a '\t', then separate those strings with a '\n'

Hope this helps.

(EDIT: Adding Commentary on Creating a 2-Column Effect)

Following comments asking about creating the effect of 2-columns w/o the ability to use HTML tables, you can do something like the following, which uses spacing instead of TABs for consistency and easier coding in your overall package:

from random import randint
def test_yield(n):
    for i in range(n):
        yield 'A'*(randint(1,10)), 'PASS'

test_lbls = [y for y in test_yield(10)]
max_len = max(len(i[0]) for i in test_lbls)
test_lbls = [(i[0]+' '*((max_len-len(i[0]))+1),i[1]) for i in test_lbls]

for l in test_lbls:
    print l[0]

For my test case, it generated output like this:

AAAAAA    PASS
AAAA      PASS
AAAAA     PASS
AAA       PASS
A         PASS
AAAAA     PASS
AAAAAAAA  PASS
AAA       PASS
AAAAAAAAA PASS
AAAAAAA   PASS

You'll have to modify this to work with your function, but the algorithm for space padding should work all the same! GL

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

BeautifulSoup Python printing out the extracted data from a table the 2nd column is dropping onto a new line. How ot keep it on same line

From Dev

How to subtract every adjacent line from the 2nd column and keep the first one

From Dev

How to skip printing (say) the 2nd line of a file?

From Dev

Python - Printing on Same Line

From Dev

Print out onto same line with ":" separating variables

From Dev

Printing out each line (from file) with line number python

From Dev

How to read CSV from 2nd Line in java?

From Dev

Printing out a specific line from a text file using python

From Dev

Data move to historical table whenever new data with same key appear during 2nd load In SSIS

From Dev

How to print new data on the same line?

From Dev

How to print new data on the same line?

From Dev

how could an int be extracted from a line of text?

From Dev

How to print a new line if a pattern from a column matches in 2 lines?

From Dev

How to avoid printing line numbers with data.table?

From Dev

Removing new line '\n' from the output of python BeautifulSoup

From Dev

how to insert the same id from 1st table to the 2nd table

From Dev

Dropping inline divs to a new line

From Dev

How to cut 2nd and 3rd column out of a textfile? python

From Dev

(Python)- How to store text extracted from HTML table using BeautifulSoup in a structured python list

From Dev

Why does my drop down navigation menu wrap and drop onto a 2nd line?

From Dev

Why does my drop down navigation menu wrap and drop onto a 2nd line?

From Dev

How to insert a new line between 2 specific characters on the same line?

From Dev

How to add data from one table into a 2nd table but only in a row that is a match

From Dev

How do I keep my Wifi from dropping out?

From Dev

How do I keep my Wifi from dropping out?

From Dev

Printing to same line using While loop in Python

From Dev

How can I the image start from a new line when using dita-ot-3.0 pdf plug outputting PDF?

From Dev

python printing each character in new line

From Dev

python printing each character in new line

Related Related

  1. 1

    BeautifulSoup Python printing out the extracted data from a table the 2nd column is dropping onto a new line. How ot keep it on same line

  2. 2

    How to subtract every adjacent line from the 2nd column and keep the first one

  3. 3

    How to skip printing (say) the 2nd line of a file?

  4. 4

    Python - Printing on Same Line

  5. 5

    Print out onto same line with ":" separating variables

  6. 6

    Printing out each line (from file) with line number python

  7. 7

    How to read CSV from 2nd Line in java?

  8. 8

    Printing out a specific line from a text file using python

  9. 9

    Data move to historical table whenever new data with same key appear during 2nd load In SSIS

  10. 10

    How to print new data on the same line?

  11. 11

    How to print new data on the same line?

  12. 12

    how could an int be extracted from a line of text?

  13. 13

    How to print a new line if a pattern from a column matches in 2 lines?

  14. 14

    How to avoid printing line numbers with data.table?

  15. 15

    Removing new line '\n' from the output of python BeautifulSoup

  16. 16

    how to insert the same id from 1st table to the 2nd table

  17. 17

    Dropping inline divs to a new line

  18. 18

    How to cut 2nd and 3rd column out of a textfile? python

  19. 19

    (Python)- How to store text extracted from HTML table using BeautifulSoup in a structured python list

  20. 20

    Why does my drop down navigation menu wrap and drop onto a 2nd line?

  21. 21

    Why does my drop down navigation menu wrap and drop onto a 2nd line?

  22. 22

    How to insert a new line between 2 specific characters on the same line?

  23. 23

    How to add data from one table into a 2nd table but only in a row that is a match

  24. 24

    How do I keep my Wifi from dropping out?

  25. 25

    How do I keep my Wifi from dropping out?

  26. 26

    Printing to same line using While loop in Python

  27. 27

    How can I the image start from a new line when using dita-ot-3.0 pdf plug outputting PDF?

  28. 28

    python printing each character in new line

  29. 29

    python printing each character in new line

HotTag

Archive