【发布时间】:2016-12-21 11:17:32
【问题描述】:
我有一个代码 sn-p 可以用 Python 发送电子邮件。我收到错误 NameError: global name 'msg' is not defined:
Traceback (most recent call last):
File "E:/test_runners 2 edit project in progress add more tests/selenium_regression_test_5_1_1/Email/email_selenium_report.py", line 32, in <module>
report.send_report_summary_from_htmltestrunner_selenium_report()
File "E:\test_runners 2 edit project in progress add more tests\selenium_regression_test_5_1_1\Email\report.py", line 516, in send_report_summary_from_htmltestrunner_selenium_report
msg['Subject'] = "ClearCore 5_1_1 Automated GUI Test"
NameError: global name 'msg' is not defined
我的代码是:
def send_report_summary_from_htmltestrunner_selenium_report():
print extract_only_header_from_summary_from_report_htmltestrunner()
print extract_header_count__from_summary_from_report_htmltestrunner()
all_testcases = list(extract_testcases_from_report_htmltestrunner())
# print all_data
pprint.pprint(all_testcases)
msg['Subject'] = "ClearCore 5_1_1 Automated GUI Test"
msg['body'] = "\n ClearCore 5_1_1 Automated GUI Test_IE11_Selenium_VM \n " + extract_only_header_from_summary_from_report_htmltestrunner() + "\n extract_header_count__from_summary_from_report_htmltestrunner() \n" + list(
extract_testcases_from_report_htmltestrunner()) + "\n Report location = : \\storage-1\Testing\Selenium_Test_Report_Results\ClearCore_5_1_1\Selenium VM\IE11 \n"
msg['to'] = "cc4_server_dev@company.onmicrosoft.com", "riazladhani@company.com"
msg['From'] = "system@company.com"
s = smtplib.SMTP()
s.connect(host=SMTP_SERVER)
s.sendmail(msg['From'], msg['To'], msg['body'], msg.as_string())
s.close()
def extract_only_header_from_summary_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")
table = soup.select_one("#result_table")
#Create list here...
results = []
headers = [td.text for td in table.select_one("#header_row").find_all("td")[1:-1]]
# print(" ".join(headers))
#Don't forget to append header (if you want)
results.append(headers)
return results
def extract_header_count__from_summary_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")
table = soup.select_one("#result_table")
#Create list here...
results = []
for row in table.select("tr.passClass"):
#Store row string in variable and append before printing
row_str = " ".join([td.text for td in row.find_all("td")[1:-1]])
results.append(row_str)
# print(row_str)
return results
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')
如何解决此错误? 我试图将 msg = MIMEText(text, "html") 放在 msg['Subject'] 上面,它不喜欢那样。
谢谢,里亚兹
【问题讨论】:
-
不喜欢这不是一个精确的问题陈述。您正在尝试在
msg对象上设置一些值,但该对象并未在任何地方创建。您需要为您的消息创建一个容器(MIMEText、MIMEMultipart等 - 取决于您究竟需要什么)。如果您对此有疑问,则不需要大多数有问题的代码。了解有关创建 minimal reproducible example 的更多信息。
标签: python python-2.7