【问题标题】:Integrating windows desktop notifications with python unittests将 Windows 桌面通知与 python 单元测试集成
【发布时间】:2017-08-10 16:44:06
【问题描述】:

我正在尝试编写一个脚本来测试站点是否已启动,如果未启动则发送桌面通知。我计划自动化一个批处理文件,以便它每小时运行一次。 目前,我有两个脚本,一个测试站点是否启动,一个发送桌面通知。但是,我无法整合它们。 我的测试脚本如下所示:

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 Firefox\Firefox.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()

如果 name == "ma​​in": 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")

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

【问题讨论】:

    标签: python unit-testing winapi notifications balloon-tip


    【解决方案1】:

    只需在第一个脚本上方声明 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 Firefox\Firefox.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 函数。

    【讨论】:

    • 这似乎可以工作,只是当我运行它时,驱动程序/浏览器会打开,但没有其他反应。是否有某个函数应该被调用但不是?
    • 嗯,你需要有一个实际运行的主函数。该脚本尚不包含 main 函数,仅包含 PythonOrgSearch 类及其依赖项。主函数应该创建一个PythonOrgSearch 的实例并调用该实例的test_server() 函数。
    • 你能展示一个代码示例吗?我不确定你的意思。我在脚本末尾附加了这个: if name__== '__main': unittest.main() 。我错过了什么吗?
    • 该评论的格式很奇怪,我不确定为什么
    • 我已经编辑了我的评论以包含一个主要功能。它创建PythonOrgSearch 对象并从中调用test_server()
    猜你喜欢
    • 2014-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-05
    • 2012-01-01
    • 2010-10-23
    • 2010-09-21
    • 1970-01-01
    相关资源
    最近更新 更多