Daniil Pimenau
Daniil Pimenau

Reputation: 41

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;");

UPDATE

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

Upvotes: 0

Views: 1417

Answers (2)

wata
wata

Reputation: 142

If you want to run PhantomJS scripts with RemoteWebDriver (for using the Selenium Grid), I used the following solution (only C# unfortunately):

  1. I had to extend the RemoteWebDriver so it can run PhantomJS commands:

    public class RemotePhantomJsDriver : RemoteWebDriver
    {
        public RemotePhantomJsDriver(Uri remoteAddress, ICapabilities desiredCapabilities) : base(remoteAddress, desiredCapabilities)
        {
            this.CommandExecutor.CommandInfoRepository.TryAddCommand("executePhantomScript", new CommandInfo("POST", $"/session/{this.SessionId.ToString()}/phantom/execute"));
        }
    
        public Response ExecutePhantomJSScript(string script, params object[] args)
        {
            return base.Execute("executePhantomScript", new Dictionary<string, object>() { { "script", script }, { "args", args } });
        }
    }
    
  2. After this you can use the ExecutePhantomJSScript method to run any JavaScript code that wants to interact with the PhantomJS API. The following example gets the page title trough the PhantomJS API (Web Page Module):

    RemotePhantomJsDriver driver = new RemotePhantomJsDriver(new Uri("http://hub_host:hub_port/wd/hub"), DesiredCapabilities.PhantomJS());
    driver.Navigate().GoToUrl("http://stackoverflow.com");
    var result = driver.ExecutePhantomJSScript("var page = this; return page.title");
    Console.WriteLine(result.Value);
    driver.Quit();
    

Upvotes: 0

ekostadinov
ekostadinov

Reputation: 6940

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.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;

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

Upvotes: 1

Related Questions