如何使用python自动从selenium webdriver向收件人发送电子邮件测试报告。我目前正在使用pycharm IDE来自动化测试用例。 我希望在我运行此测试用例时,最后发送电子邮件给某人。邮件应该成功并且计数失败。我试图找到解决方案但失败了。这是我的测试用例。请给我解决方案。
from selenium import webdriver
from generic_functions.FindElement import HandyWrappers
from generic_functions.takescreenshots import Screenshot
from generic_functions.error_handle import CatchExceptions
import os
import time
import unittest
class TestHome(unittest.TestCase):
driverLocation = "C:\\Users\\Sales\\Desktop\\Automation\\ABCD\\libs\\chromedriver.exe"
os.environ["webdriver.chrome.driver"] = driverLocation
driver = webdriver.Chrome(driverLocation)
driver.maximize_window()
driver.implicitly_wait(10)
def test_home(self):
try:
baseURL = "https://portal.abcd.com"
driver = self.driver
driver.get(baseURL)
hw = HandyWrappers(driver)
# regionLogin
username = hw.getElement(".//form[@id='login-form']/fieldset/section[1]/label[2]/input[@name='email']",
locatorType="xpath")
username.send_keys("abcd@live.com")
time.sleep(2)
password = hw.getElement(".//form[@id='login-form']/fieldset/section[2]/label[2]/input[@name='password']",
locatorType="xpath")
password.send_keys("abcd")
signIn = hw.getElement(".//form[@id='login-form']/footer/button[contains(text(),'Sign in')]",
locatorType="xpath")
signIn.click()
Screenshot.takeScreenshot(driver)
# endregion
time.sleep(2)
driver.execute_script("window.scrollBy(0,300);")
time.sleep(1)
# Switch to Iframe from main page
driver.switch_to.frame("ifrmClaimSummary")
# regionScrolling Right and left
driver.execute_script("window.scrollBy(100,0);")
time.sleep(1)
driver.execute_script("window.scrollBy(100,0);")
time.sleep(1)
driver.execute_script("window.scrollBy(100,0);")
time.sleep(1)
# Scrolling Left
driver.execute_script("window.scrollBy(-100,0);")
time.sleep(1)
driver.execute_script("window.scrollBy(-100,0);")
time.sleep(1)
driver.execute_script("window.scrollBy(-100,0);")
time.sleep(1)
# endregion
driver.switch_to.default_content()
driver.execute_script("window.scrollBy(0,-300);")
time.sleep(2)
driver.quit()
except Exception:
CatchExceptions.PrintException()
Screenshot.takeScreenshot(driver)
if __name__ == '__main__':
unittest.main()
答案 0 :(得分:0)
根据您的评论,您可以按照以下方法完成测试报告和电子邮件结果:
考虑利用unittest
进行测试报告 - Selenium在其文档中有an example of this。
然后,您可以使用smtplib
发送电子邮件。这有无数的资源;这里one tutorial on smtplib
with Gmail。
简而言之:
unittest
设置测试套件unittest
的API访问测试结果smtplib