【发布时间】:2017-12-01 03:55:27
【问题描述】:
下面我尝试测试了1+7的加法运算;但不知道 如何获取属性“name”为“Input”的文本字段的结果输出。
任何指针将不胜感激。
package hw9;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class calculator {
private WebDriver driver;
private String baseUrl;
@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://www.math.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testCalculator() throws Exception {
driver.get(baseUrl + "/students/calculators/source/basic.htm");
driver.findElement(By.name("one")).click();
driver.findElement(By.name("plus")).click();
driver.findElement(By.name("seven")).click();
driver.findElement(By.name("DoIt")).click();
String output = driver.findElement(By.name("Input")).getText();
System.out.println("Output: " + output); // **<--- Empty output**
assertEquals(8,output);
}
@After
public void tearDown() throws Exception {
driver.quit();
}
}
下面列出了相关代码的 HTML:
<td>
<div align="center"> <font size="+1">
<input name="Input" type="text" size="16">
</font></div>
</td>
【问题讨论】:
-
尝试使用
By.id("Input") -
元素没有id;它只有名称和类型属性。
-
你在第一句话中说
text field whose id is "Input"。此外,如果您发布了您正在测试的页面的 HTML,也会有所帮助。 -
我已经编辑了原始问题。
-
即使得到答案,
assertEquals(8,output);也永远不会成功,因为int永远不会等于String。