0

こんにちは、Java の Eclipse で phantomjs を起動しているときに次のエラーが発生します PhantomJS は GhostDriver を起動しています... ここに画像の説明を入力

次の手順を実行して、phantomjs を eclipse に追加しました。

  1. phantomjs.exe をダウンロード

  2. phantomjs-1.8.x-windows.zip フォルダーを抽出し、phantomjs.exe ファイルを C:/ フォルダーに配置します。

  3. 次のインポートをコードに追加します。

org.openqa.selenium.phantomjs.PhantomJSDriver をインポートします。org.openqa.selenium.phantomjs.PhantomJSDriverService をインポートします。org.openqa.selenium.remote.DesiredCapabilities をインポートします。

「FirefoxDriver」を指定するオブジェクト「driver」を「PhantomJSDriver」に置き換えます。

コードを置き換えます。WebDriver ドライバー = 新しい FirefoxDriver

DesiredCapabilities caps = 新しい DesiredCapabilities(); caps.setJavascriptEnabled(true); caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "C:/phantomjs.exe"); WebDriver ドライバー = 新しい PhantomJSDriver(caps);

テストを実行します。

助けてください !!

4

1 に答える 1

2

以下の手順に従って、GhostDriver を適切に動作させるには:

                      PREREQUISITES

前提条件ステップ 1:

必要なものがすべてダウンロードされたら、次のステップは、セレン ハブ (セレン サーバー) と、起動されたハブに接続され、phantomJs に基づくセレン ノードを起動することです。

                   LAUNCHING HUB AND NODES
  1. セレンハブの開始

    java -jar selenium-server-standalone-2.41.0.jar -role ハブ

ハブを起動したら、ブラウザでローカルに入力http://localhost:4444/grid/console して確認します => http://gyazo.com/9435772d76044cf273d6b567584c0532

  1. GhostDriver ノードの起動

    phantomjs --webdriver=8080 --webdriver-selenium-grid-hub=http:// localhost:4444

再度確認してください localhost/grid/console => http://gyazo.com/06fd2fc6d740c18e3d0925e180de150f

それが終わったら、次のセットアップを行います。

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

}

アプローチ 2

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>

適切なコードのセットアップ:

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

    }

これがうまくいくことを願っています

于 2014-07-06T17:11:33.033 に答える