将 Windows 桌面通知与 python 单元测试集成



我正在尝试编写一个脚本来测试站点是否已启动,如果未启动,则发送桌面通知。我计划自动执行批处理文件,以便每小时运行一次。 目前,我有两个脚本,一个用于测试站点是否已启动,另一个用于发送桌面通知。但是,我在集成它们时遇到了问题。 我的测试脚本如下所示:

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import os
import time
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.action_chains import ActionChains
from urllib.request import urlopen
from html.parser import HTMLParser
import notification_script

gecko = os.path.normpath(os.path.join(os.path.dirname(__file__), 'geckodriver'))
binary = FirefoxBinary('C:Program Files (x86)Mozilla FirefoxFirefox.exe')
driver = webdriver.Firefox(firefox_binary=binary, executable_path=gecko+'.exe')

class PythonOrgSearch(unittest.TestCase):
#sets up driver to run tests
def setUp(self):
self.driver = driver
def test_server(self):
driver=self.driver
driver.get("http://website.com")
if self.assertIn("stuff", driver.title):
print(")
else:
print("site not up")
driver.close()

如果名称== ">主要": unittest.main((

我的通知脚本如下所示:

from win32api import *
from win32gui import *
import win32con
import sys, os
import struct
import time
class WindowsBalloonTip:
def __init__(self, title, msg):
message_map = {
win32con.WM_DESTROY: self.OnDestroy,
}
# Register the Window class.
wc = WNDCLASS()
hinst = wc.hInstance = GetModuleHandle(None)
wc.lpszClassName = "PythonTaskbar"
wc.lpfnWndProc = message_map # could also specify a wndproc.
classAtom = RegisterClass(wc)
# Create the Window.
style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
self.hwnd = CreateWindow( classAtom, "Taskbar", style, 
0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, 
0, 0, hinst, None)
UpdateWindow(self.hwnd)
iconPathName = os.path.abspath(os.path.join( sys.path[0], "balloontip.ico" ))
icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
try:
hicon = LoadImage(hinst, iconPathName, 
win32con.IMAGE_ICON, 0, 0, icon_flags)
except:
hicon = LoadIcon(0, win32con.IDI_APPLICATION)
flags = NIF_ICON | NIF_MESSAGE | NIF_TIP
nid = (self.hwnd, 0, flags, win32con.WM_USER+20, hicon, "tooltip")
Shell_NotifyIcon(NIM_ADD, nid)
Shell_NotifyIcon(NIM_MODIFY, 
(self.hwnd, 0, NIF_INFO, win32con.WM_USER+20,
hicon, "Balloon  tooltip",msg,200,title))
# self.show_balloon(title, msg)
time.sleep(30)
DestroyWindow(self.hwnd)
def OnDestroy(self, hwnd, msg, wparam, lparam):
nid = (self.hwnd, 0)
Shell_NotifyIcon(NIM_DELETE, nid)
PostQuitMessage(0) # Terminate the app.
def balloon_tip(title, msg):
w=WindowsBalloonTip(title, msg)
if __name__ == '__main__':
balloon_tip("Server Notification", "Server is down")

我的问题是我不知道如何将这些集成到一个脚本中,该脚本将发送有关站点状态/是否关闭的桌面通知。如果你知道如何做,请告诉我,我很迷茫。谢谢!

只需在第一个脚本上方声明 WindowsBalloonTips 的类和函数即可。

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import os
import time
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.action_chains import ActionChains
from urllib.request import urlopen
from html.parser import HTMLParser
import notification_script
from win32api import *
from win32gui import *
import win32con
import sys
import struct
import time

gecko = os.path.normpath(os.path.join(os.path.dirname(__file__), 'geckodriver'))
binary = FirefoxBinary('C:Program Files (x86)Mozilla FirefoxFirefox.exe')
driver = webdriver.Firefox(firefox_binary=binary, executable_path=gecko+'.exe')
class WindowsBalloonTip:
def __init__(self, title, msg):
message_map = {
win32con.WM_DESTROY: self.OnDestroy,
}
# Register the Window class.
wc = WNDCLASS()
hinst = wc.hInstance = GetModuleHandle(None)
wc.lpszClassName = "PythonTaskbar"
wc.lpfnWndProc = message_map # could also specify a wndproc.
classAtom = RegisterClass(wc)
# Create the Window.
style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
self.hwnd = CreateWindow( classAtom, "Taskbar", style, 
0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, 
0, 0, hinst, None)
UpdateWindow(self.hwnd)
iconPathName = os.path.abspath(os.path.join( sys.path[0], "balloontip.ico" ))
icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
try:
hicon = LoadImage(hinst, iconPathName, 
win32con.IMAGE_ICON, 0, 0, icon_flags)
except:
hicon = LoadIcon(0, win32con.IDI_APPLICATION)
flags = NIF_ICON | NIF_MESSAGE | NIF_TIP
nid = (self.hwnd, 0, flags, win32con.WM_USER+20, hicon, "tooltip")
Shell_NotifyIcon(NIM_ADD, nid)
Shell_NotifyIcon(NIM_MODIFY, 
(self.hwnd, 0, NIF_INFO, win32con.WM_USER+20,
hicon, "Balloon  tooltip",msg,200,title))
# self.show_balloon(title, msg)
time.sleep(30)
DestroyWindow(self.hwnd)
def OnDestroy(self, hwnd, msg, wparam, lparam):
nid = (self.hwnd, 0)
Shell_NotifyIcon(NIM_DELETE, nid)
PostQuitMessage(0) # Terminate the app.
def balloon_tip(title, msg):
w=WindowsBalloonTip(title, msg)
class PythonOrgSearch(unittest.TestCase):
#sets up driver to run tests
def setUp(self):
self.driver = driver
def test_server(self):
driver=self.driver
driver.get("http://website.com")
if self.assertIn("stuff", driver.title):
# Optionally notify the user if the site is up
balloon_tip("Server Notification", "Server is up")
else:
balloon_tip("Server Notification", "Server is down")
driver.close()

# The main function to acutally call the test_server command
def main():
driver = PythonOrgSearch()
driver.test_server()
# Call the main function to start the program
main()

然后根据需要从主脚本调用 balloon_tip 函数。

最新更新