使用 selenium.webdriver 获得响应更快的 Cherrypy 开发环境

Using selenium.webdriver for a more responsive Cherrypy development environment

我一直在尝试在 cherrypy 服务器启动时使用 selenium 模块打开浏览器。

我希望它用 cherrypy.Autoreload 重新加载页面,这样我就不必使用鼠标了。

作为一个 cherrypy 插件,如果它启动得太早,服务器将不会响应,并在结束会话时抛出错误。

我需要 after_server_start 活动之类的。 有什么建议吗??

您可以使用 cherrypy 引擎(总线)的 start 阶段。

import webbrowser                                                               

import cherrypy                                                                 
from cherrypy.process.plugins import SimplePlugin                               

class Root:                                                                     

    @cherrypy.expose                                                            
    def default(self):                                                          
        return "Hello World!"                                                   

class OpenBrowser(SimplePlugin):                                                

    def start(self):                                                            
        self.bus.log("Opening browser")                                         
        webbrowser.open('http://localhost:8080/')                               

OpenBrowser(cherrypy.engine).subscribe()                                        
cherrypy.quickstart(Root()) 

更多信息:http://docs.cherrypy.org/en/latest/extend.html#engine-as-a-pubsub-bus

所以我有点明白了。 我使用 cherryd 创建了一个守护进程服务器:

from selenium import webdriver
import cherrypy,time,os,signal

class CpIde():
    def __init__(self):pass

    @cherrypy.expose
    def index(self):return "hello there"

    @cherrypy.expose
    def open(self):
        if hasattr(self,'driver'):
            try:self.driver.quit()
            except:
                self.driver.service.process.send_signal(signal.SIGTERM)
        self.driver=webdriver.Firefox()
        return "Opening FF"

    @cherrypy.expose 
    def start(self):
        try:
            self.driver.get('http://127.0.0.1:8080')
        except:pass
        finally:
            self.driver.execute_script("""
            document.body.innerHTML="Just one second"; 
            window.setTimeout(function(){window.location.reload(false)},300) """
)
        return "starting FF"

    @cherrypy.expose
    def close(self):
        self.driver.quit()
        del self.driver
        return 'Closing FF'

    @cherrypy.expose 
    def refresh(self):
        self.driver.execute_script("""
            document.body.innerHTML="Just one second"; 
            window.setTimeout(function(){window.location.reload(false)},300) """)
        return "restarting"

cherrypy.tree.mount(CpIde(),'/')

然后我制作了一个向它发出请求的插件:

import cherrypy,time,os
from cherrypy.process import wspbus, plugins
import requests as r


def command(method):
    addr='http://127.0.0.1:3131/'
    r.get(addr+method)

class CpIdePlugin(plugins.SimplePlugin):
    def __init__(self,bus):
        plugins.SimplePlugin.__init__(self,bus)
        self.bus.log('Init plug')
        DriveId=os.getenv('CherryDrive')
        self.running=False
        if DriveId:
            self.running=True
        else:
            command('open')
            os.putenv('CherryDrive','True')


    def start(self):
        self.bus.log('Running FF plugin')
        if self.running:
            self.refresh()
        else:
            command('start')

    def refresh(self):#need to make a channel i think
        self.bus.log("Reload via bus")
       command('refresh')

它与我的主应用相关联:

import cpideplugin
class root:pass

cherrypy.tree.mount(root(),"/")

cpideplugin.CpIdePlugin(cherrypy.engine).subscribe()
cherrypy.engine.start()#the pubsub engine
cherrypy.server.start()#an html server channel on the engine
cherrypy.engine.block()
cpideplugin.command('close')

这样 selenium 驱动程序就不会使用 Autoreload 进行刷新,我可以在 vim 中自由工作。这不是完美的代码,我将不胜感激任何建议。干杯