首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Remotewebdriver上的executePhantomJS

Remotewebdriver上的executePhantomJS
EN

Stack Overflow用户
提问于 2015-07-28 07:36:28
回答 2查看 1.4K关注 0票数 0

如果我使用web驱动程序,那么它工作得非常完美。

代码语言:javascript
复制
driver = new PhantomJSDriver(capabilities);
driver.executePhantomJS( "var page = this;");

我怎么才能让它起作用?

代码语言:javascript
复制
driver = new RemoteWebDriver(capabilities);
driver.executePhantomJS( "var page = this;");

更新

我的代码

代码语言:javascript
复制
capabilities = DesiredCapabilities.phantomjs();
driver = new RemoteWebDriver(capabilities);


driver.executePhantomJS( "var page = this; binary =0;mimetype=''; count = 0;id=0; bla = '{';"
                                +"page.onResourceReceived = function(request) {"
                                    + "if(id !== request.id){"
                                        +"bla += '\"'+count+ '\":'+JSON.stringify(request, undefined, 4)+',';"
                                        +"if(request.contentType.substring(0, 11) =='application'){"
                                            +"console.log(request.contentType);"
                                            + "mimetype = request.contentType;"
                                            + "binary++;"
                                        + "}"
                                        +"count++;"
                                        + "id = request.id;"
                                    + "}"                       
                                +"};");

Java给出错误:方法executePhantomJS(String)未定义为RemoteWebDriver类型。

如果我使用executeScript,它将无法工作。

我需要运行100测试并行,我不能使用网络驱动程序。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-07-28 08:26:28

我猜你想在你的Se网格上运行PhantomJSDriver。这就是它对我的工作方式(C#工厂实现):

代码语言:javascript
复制
public IWebDriver CreateWebDriver(string identifier)
    {
     if (identifier.ToLower().Contains("ghostdriver"))
        {
            return new RemoteWebDriver(new Uri(ConfigurationManager.AppSettings["Selenium.grid.Url"]), DesiredCapabilities.PhantomJS());
        }
    }

或者试试这个

代码语言:javascript
复制
   Console.WriteLine("Creating GhostDriver (PhantomJS) driver.");
   //Temporary commented for testing purposes
   IWebDriver ghostDriver = new PhantomJSDriver("..\\..\\..\\MyFramework\\Drivers");
                ghostDriver.Manage().Window.Maximize();
                //ghostDriver.Manage().Window.Size = new Size(1920, 1080);
                ghostDriver.Manage()
                    .Timeouts()
                    .SetPageLoadTimeout(new TimeSpan(0, 0, 0,
                        Convert.ToInt32(ConfigurationManager.AppSettings["Driver.page.load.time.sec"])));
                return ghostDriver;

如果您想知道为什么会有ConfigurationManager -我避免硬编码的值,所以从App.config文件中提取它们。

票数 1
EN

Stack Overflow用户

发布于 2016-10-25 09:31:00

如果您想使用PhantomJS运行RemoteWebDriver脚本(对于使用Selenium ),我使用了以下解决方案(不幸的是只有C# ):

  1. 我必须扩展RemoteWebDriver,以便它能够运行PhantomJS命令: 公共类RemotePhantomJsDriver : RemoteWebDriver { public RemotePhantomJsDriver(Uri remoteAddress,ICapabilities desiredCapabilities):base(remoteAddress,desiredCapabilities) {RemoteWebDriver new CommandInfo("POST",$"/session/{this.SessionId.ToString()}/phantom/execute"));)}公共响应ExecutePhantomJSScript(字符串脚本,params object[] args) {返回base.Execute("executePhantomScript",新Dictionary() {“脚本”,脚本},{ " args ",args });}
  2. 在此之后,您可以使用ExecutePhantomJSScript方法运行任何希望与PhantomJS API交互的JavaScript代码。下面的示例通过PhantomJS API (网页模块)获取页面标题: RemotePhantomJsDriver driver =新RemotePhantomJsDriver(新Uri("port/wd/hub"),DesiredCapabilities.PhantomJS()“;driver.Navigate().GoToUrl(”http://stackoverflow.com"“);var result = driver.ExecutePhantomJSScript("var page = this;返回page.title");Console.WriteLine(result.Value);driver.Quit();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31670125

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档