2016-11-04 8 views
0

を使用してGWTアプリケーションのJavaScriptを有効にすることはできません。最初に私は次のように、HtmlUnitDriverを使用して、ページの内容を取得しようとしました:は、セレンのWebClientまたは私は<strong>ヘッドレスブラウザのテスト</strong>を使用して<strong>GWTアプリケーション</strong>のためのGUIをテストするために、HTMLページのソースを取得しようとしていますHtmlUnitDriver

DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit(); 

//set an userId header 
capabilities.setCapability("id", "userId"); 

HtmlUnitDriver unitDriver = new HtmlUnitDriver(capabilities); 

//enabled javascript 
unitDriver.setJavascriptEnabled(true); 

unitDriver.get("http://localhost:portNo/example/"); 

String pageSource = unitDriver.getPageSource(); 

それを行うための第二の方法は、Webクライアントを使用していた:で、

WebClient webClient = new WebClient(); 

//enabled javascript 
webClient.getOptions().setJavaScriptEnabled(true); 

//set an userId header 
webClient.addRequestHeader("id", "userId"); 

HtmlPage page = webClient.getPage("http://localhost:portNo/example/"); 
String contentAsString = page.getWebResponse().getContentAsString(); 

しかし、どちらの場合も同じ結果が得られます。結果ページの実際のコンテンツが含まれていない、それは次のように表示されます。

</iframe> 
    <!-- RECOMMENDED if your web app will not function without JavaScript enabled --> <noscript> 

     &lt;div style="width: 22em; position: absolute; left: 50%; margin-left: -11em; color: red; background-color: white; border: 1px solid red; padding: 4px; font-family: sans-serif"&gt; 
     Your web browser must have JavaScript enabled 
     in order for this application to display correctly. 
     &lt;/div&gt; 

    </noscript> 
    <iframe src="javascript:''" id="frameId" style="position:absolute;width:0;height:0;border:none" tabindex="-1"> 
    </iframe> 

は、私が何かできることはあります/ページの実際のコンテンツを取得するために有効にしますか?ありがとうございました!

+0

unitDriver.getPafeSource()とpage.getWebReponse()の両方がホストページコンテンツを返しています(http://www.gwtproject.org/doc/latest/DevGuideOrganizingProjects.html#DevGuideHostPageを参照)。これは実際のコンテンツです。しかし、ブートストラップ後にいくつかの要素をチェックしたいと思う。この場合、HtmlUnit/WebClientのDOM関連APIを使用する必要があります(例:page.asText、page.getByXpath) –

+0

Alexanderさん、ありがとうございます。あなたは正しいです、私はページ内のDOM要素を探していますが、問題はunitDriver.findElementByClassName( "listGrid")などを呼び出すときに何も得られないということです。また、page.asText()メソッドはページタイトルのみを提供するので、あまり有用ではありません。 –

答えて

0

javascriptファイルを読み込むためにタイムアウトを設定することで問題を解決しました。

ので、これを実行した後:

HtmlPage page = webClient.getPage("http://localhost:portNo/example/"); 
webClient.waitForBackgroundJavaScript(10000); //time in milliseconds 

ページがコンテンツを持っていたと私は予想DOM要素を見つけることができます。

関連する問題

 関連する問題