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

时间:2017-03-19 21:16:18

标签: python-3.x selenium webdriver cherrypy web-development-server

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

我希望它用cherrypy.Autoreload重新加载页面,所以我不必使用鼠标。

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

我需要一个after_server_start事件或其他东西。 有什么建议??

2 个答案:

答案 0 :(得分:0)

您可以使用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

答案 1 :(得分:0)

所以我想了一下。 我使用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自由工作。这不是完美的代码,我将不胜感激任何建议。干杯