我試圖使用REST Assured來檢查我的服務器返回的HTML文檔的一些屬性。一個SSCCE證明那問題就如下:使用REST確保檢查HTML文檔
import static com.jayway.restassured.path.xml.config.XmlPathConfig.xmlPathConfig;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Test;
import com.jayway.restassured.path.xml.XmlPath;
public class HtmlDocumentTest {
@Test
public void titleShouldBeHelloWorld() {
final XmlPath xml = new XmlPath("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"
+ "<html xmlns=\"http://www.w3.org/1999/xhtml\">"
+ "<head><title>Hello world</title></head><body></body></html>")
.using(xmlPathConfig().with().feature("http://apache.org/xml/features/disallow-doctype-decl", false));
assertThat(xml.get("//title[text()]"), is("Hello world"));
}
}
現在,這種嘗試有30秒左右的時間以後,結束com.jayway.restassured.path.xml.exception.XmlPathException: Failed to parse the XML document
所致,關閉所有可能出現的錯誤,java.net.ConnectException: Connection timed out
!
如果我用xmlPathConfig().with().feature(...)
刪除行,由於DOCTYPE is disallowed when the feature "http://apache.org/xml/features/disallow-doctype-decl" set to true.
,測試立即失敗。
如果我從文檔中刪除doctype行解析成功,但測試失敗的斷言錯誤,「Expected: is "Hello world" but: was <Hello worldnull>
」 - 但是,這是一個不同的問題,顯然(但也可以自由地給予指示...)。無論如何,刪除文檔類型對我來說都不是一種選擇。
所以,問題:你如何使用REST Assured檢查帶有文檔類型的HTML文檔的屬性?它說in the documentation「REST Assured providers預定義的解析器,例如HTML,XML和JSON。」,但我似乎無法找到關於如何激活和使用該HTML解析器的任何示例!沒有「HtmlPath
」類,例如XmlPath
,例如,超時異常非常令人費解......
酷,謝謝!似乎像一個魅力工作,我很高興獎勵你的賞金。 :)另外一個問題,但是,如果你不介意:使用屬性訪問語法,是否有可能以任何方式找到具有給定ID或類的元素,然後聲明其內容? – ZeroOne
@請參閱上面的更新答案。 –
好的,謝謝!在這裏,還有一個upvote。 :) – ZeroOne