如何使用 Selenium 和 Scalatest 验证纯文本字段的内容
How can I verify the content of a plain text field with Selenium and Scalatest
我有一个 PHP 脚本的结果
<html>
<head>
<title>Greetings</title>
</head>
<body>
<h1 id="header1">Hello</h1>
<input id="input1" value="Hi" />
</body>
并想测试是否所有元素都被正确填充,将 Selenium 与 Scalatest (http://www.scalatest.org/user_guide/using_selenium) 结合使用。
class TrialSpec extends FlatSpec with MustMatchers with HtmlUnit {
val host = "http://localhost:8000/"
go to (host + "index.html")
"The homepage" should "have the correct title" in {
pageTitle must be ("Greetings")
}
"The main input1" should "have the correct value" in {
val mainInput = textField("input1").value
mainInput must be ("Hi")
}
"The main header" should "have the correct content" in {
id("header1") must be ("Hello")
}
quit()
}
前两个测试成功,但我无法访问 <h1>
元素的 .firstChild
或 .innerHTML
。我还尝试了段落和跨度。
我能做什么?
最佳
亚历克斯
id
是一个 returns 查询而不是元素的函数。
您可以改用这个:
find(id("header1")).map(_.text) shouldBe Some("Hello")
find
returns Option[Element]
可以映射到文本值。
HtmlUnit trait 参考资料是您最好的信息来源。
我有一个 PHP 脚本的结果
<html>
<head>
<title>Greetings</title>
</head>
<body>
<h1 id="header1">Hello</h1>
<input id="input1" value="Hi" />
</body>
并想测试是否所有元素都被正确填充,将 Selenium 与 Scalatest (http://www.scalatest.org/user_guide/using_selenium) 结合使用。
class TrialSpec extends FlatSpec with MustMatchers with HtmlUnit {
val host = "http://localhost:8000/"
go to (host + "index.html")
"The homepage" should "have the correct title" in {
pageTitle must be ("Greetings")
}
"The main input1" should "have the correct value" in {
val mainInput = textField("input1").value
mainInput must be ("Hi")
}
"The main header" should "have the correct content" in {
id("header1") must be ("Hello")
}
quit()
}
前两个测试成功,但我无法访问 <h1>
元素的 .firstChild
或 .innerHTML
。我还尝试了段落和跨度。
我能做什么?
最佳 亚历克斯
id
是一个 returns 查询而不是元素的函数。
您可以改用这个:
find(id("header1")).map(_.text) shouldBe Some("Hello")
find
returns Option[Element]
可以映射到文本值。
HtmlUnit trait 参考资料是您最好的信息来源。