Remotewebdriver上的executePhantomJS

时间:2015-07-28 07:36:28

标签: java selenium phantomjs remotewebdriver

如果我使用网络驱动程序,那么它可以完美运行

<sch:pattern id="mixed">
    <sch:rule context="def[child::text()][child::*]">
        <sch:report test="tokenize(child::text(), '\s+')" sqf:fix="mix_in_def">
            Element has mixed content
            <!-- the above this gives me the error: a sequence of more than one item is not allowed as the first argument of tokenize-->
        </sch:report>
        <sqf:fix id="mix_in_def">
            <sqf:description>
                <sqf:title>Wrap words in w</sqf:title>
                <sqf:p>Fixes the mixed content in def by treating each non-tagged string as w.</sqf:p>
            </sqf:description>
            <sqf:replace match="." node-type="element" target="w">
                <!--how do i represent the content of the matched token?-->
            </sqf:replace>
            <!-- also do i create an altogether separate rule for punctuation?-->
        </sqf:fix>
    </sch:rule>
</sch:pattern>

我怎样才能让它发挥作用?

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

更新

我的代码

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

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

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

我需要并行运行100个测试,我不能使用webdriver。

2 个答案:

答案 0 :(得分:1)

我猜你想在Se Grid上运行PhantomJSDriver。这就是我的工作方式(C#Factory实现):

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

或试试这个

   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 :(得分:0)

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

  1. 我必须扩展RemoteWebDriver才能运行PhantomJS命令:

    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. 在此之后,您可以使用ExecutePhantomJSScript方法运行任何想要与PhantomJS API交互的JavaScript代码。以下示例通过PhantomJS API(网页模块)获取页面标题:

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