PhantomJS正在启动GhostDriver,但在创建新会话后将其关闭

时间:2014-02-16 22:21:20

标签: eclipse selenium phantomjs ghostdriver

嗨我在eclipse中为java启动phantomjs时遇到以下错误 PhantomJS正在推出GhostDriver ...... enter image description here

我已经完成了以下步骤将phantomjs添加到eclipse:

  1. 下载phantomjs.exe

  2. 解压缩phantomjs-1.8.x-windows.zip文件夹并将phantomjs.exe文件定位到C:/ folder

  3. 将以下导入添加到您的代码中:
  4. import org.openqa.selenium.phantomjs.PhantomJSDriver; import org.openqa.selenium.phantomjs.PhantomJSDriverService; import org.openqa.selenium.remote.DesiredCapabilities;

    用“PhantomJSDriver”替换指定“FirefoxDriver”的对象“driver”。

    替换代码, WebDriver驱动程序=新的FirefoxDriver

    DesiredCapabilities caps = new DesiredCapabilities(); caps.setJavascriptEnabled(真); caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,“C:/phantomjs.exe”); WebDriver driver = new PhantomJSDriver(caps);

    运行测试。

    请帮助!!

1 个答案:

答案 0 :(得分:1)

为了使GhostDriver按照以下步骤正常工作:

                      PREREQUISITES

先决条件步骤1:

在您需要的所有内容下载后,下一步 - 启动selenium hub(selenium服务器)和selenium节点,它将连接到已启动的集线器,并将基于phantomJs。

                   LAUNCHING HUB AND NODES
  1. 硒中心启动

    java -jar selenium-server-standalone-2.41.0.jar -role hub

  2. 启动集线器后 - 在浏览器中键入http://localhost:4444/grid/console并在浏览器中输入> http://gyazo.com/9435772d76044cf273d6b567584c0532

    1. GhostDriver节点启动

      phantomjs --webdriver = 8080 --webdriver-selenium-grid-hub = http:// l o c a l h o s t:4444

    2. 再次检查localhost / grid / console => http://gyazo.com/06fd2fc6d740c18e3d0925e180de150f

      完成后,请进行以下setUp。

                                     CODE SETUP
      

      接近一个

      我的项目是基于maven的,所以我建议您遵循PhantomJs跨平台解决方案

      在POM.XML中添加以下内容

               <dependency>
                   <groupId>org.seleniumhq.selenium</groupId>
                   <artifactId>selenium-java</artifactId>
                   <version>2.41.0</version>
               </dependency>
      
               <dependency>
                   <groupId>org.seleniumhq.selenium</groupId>
                   <artifactId>selenium-support</artifactId>
                   <version>2.41.0</version>
               </dependency>
      
      
      <!--substituting   phanbedder  with local Phanbedder implementation-->
                   <dependency>
               <groupId>net.anthavio</groupId>
               <artifactId>phanbedder-1.9.7</artifactId>
               <version>1.0.0</version>
           </dependency>
      
      
      
           <dependency>
               <groupId>com.github.detro.ghostdriver</groupId>
               <artifactId>phantomjsdriver</artifactId>
               <version>1.1.0</version>
           </dependency>
      

      添加这些依赖项后,切换到测试类(例如SeleniumTest.java)

       private WebDriver driver;
      
          @BeforeClass
          public void seleniumGrridUponGhostDriver() throws MalformedURLException {
      
      
             File phantomjs = Phanbedder.unpack(); // cross platform solution. Maven will provide //appropriate phantomJs instance
      
              DesiredCapabilities dcaps = new DesiredCapabilities();
              dcaps.setCapability("takesScreenshot", true);
      
      
              dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, phantomjs.getAbsolutePath());
      
      // NOTE: as we launched node locally, that is why we pass
      // 127.0.0.1  IP as parameter
              this.driver = new RemoteWebDriver(new URL("http://127.0.0.1:8080"), dcaps);
      
              driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
              driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
          }
      
      @Test
      public void myTest(){
      driver.get("http://www.google.com");
      .....
      
      }
      

      接近两个

      在POM.xml中添加以下依赖项

          <dependency>
                   <groupId>org.seleniumhq.selenium</groupId>
                   <artifactId>selenium-java</artifactId>
                   <version>2.41.0</version>
               </dependency>
      
               <dependency>
                   <groupId>org.seleniumhq.selenium</groupId>
                   <artifactId>selenium-support</artifactId>
                   <version>2.41.0</version>
               </dependency>
      

      适当的代码setUP:

        private WebDriver driver;
      
          @BeforeClass
          public void seleniumGrridUponGhostDriver() throws MalformedURLException {
      
              File phantomjs = new File("C:\\Selenium\\phantomjs.exe");
      
      
              DesiredCapabilities dcaps = new DesiredCapabilities();
              dcaps.setCapability("takesScreenshot", true);
      
      
              dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, phantomjs.getAbsolutePath());
      
              this.driver = new RemoteWebDriver(new URL("http://127.0.0.1:8080"), dcaps);
      
      
              driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
              driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
      
      
          }
      
          @Test
          public void myTest(){
          driver.get("http://www.google.com");
          .....
      
          }
      

      希望这适合你