Daniil Pimenau Daniil Pimenau - 11 months ago 183
Java Question

executePhantomJS on Remotewebdriver

if I use a web driver then it works perfectly

driver = new PhantomJSDriver(capabilities);
driver.executePhantomJS( "var page = this;");

How can I make it work?

driver = new RemoteWebDriver(capabilities);
driver.executePhantomJS( "var page = this;");


My code

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'){"
+ "mimetype = request.contentType;"
+ "binary++;"
+ "}"
+ "id = request.id;"
+ "}"

Java gives error: The method executePhantomJS(String) is undefined for the type RemoteWebDriver.

If i use executeScript it will not work.

I need run 100 test parallel, i can't use webdriver.

Answer Source

I guess that you wanna run PhantomJSDriver on your Se Grid. This is how it works for me (C# Factory implementation):

public IWebDriver CreateWebDriver(string identifier)
     if (identifier.ToLower().Contains("ghostdriver"))
            return new RemoteWebDriver(new Uri(ConfigurationManager.AppSettings["Selenium.grid.Url"]), DesiredCapabilities.PhantomJS());

or try this one

   Console.WriteLine("Creating GhostDriver (PhantomJS) driver.");
   //Temporary commented for testing purposes
   IWebDriver ghostDriver = new PhantomJSDriver("..\\..\\..\\MyFramework\\Drivers");
                //ghostDriver.Manage().Window.Size = new Size(1920, 1080);
                    .SetPageLoadTimeout(new TimeSpan(0, 0, 0,
                return ghostDriver;

In case that you wonder why there is ConfigurationManager - I avoid the hard-coded values, so they are extracted from the App.config file.