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>
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.
Comments