簡體   English   中英

使用 selenium 和 python 自動化測試用例的發送電子郵件測試報告

[英]Automate the send email test report of test cases using selenium and python

我如何使用 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()

根據您的評論,您可以遵循以下方法來完成測試報告和電子郵件結果:

考慮利用unittest進行測試報告 - Selenium 在他們的文檔中有一個例子

然后,您可以使用smtplib發送電子郵件。 有無數的資源可以做到這一點; 這是一個關於smtplib和 Gmail 的教程


簡而言之:

  1. 使用unittest設置測試套件
  2. 通過unittest的 API 訪問測試結果
  3. 構建你想發送的任何字符串,然后用smtplib發送它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM