使用selenium和python自动发送测试用例的email测试报告

2024-06-14 15:35:25 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用python自动从selenium webdriver向收件人发送电子邮件测试报告。我目前正在使用pycharmide来自动化测试用例。 当这个测试用例由我运行时,我想给某人发电子邮件。邮件应该有成功和失败的计数。我试图找到解决办法但是失败了。这个是我的测试用例。请给我答案。在

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()

Tags: fromimportformexecutetimedriverscript测试用例
1条回答
网友
1楼 · 发布于 2024-06-14 15:35:25

根据您的意见,您可以遵循以下方法来完成测试报告和电子邮件结果:

考虑利用unittest进行测试报告—Selenium的文档中有an example of this。在

然后,您可以使用smtplib发送电子邮件。这方面有很多资源;这里是one tutorial on ^{} with Gmail。在


简而言之:

  1. 使用unittest设置测试套件
  2. 通过unittest的API访问测试结果
  3. 生成您要发送的任何字符串,并用smtplib发送它

相关问题 更多 >