📄 testsforcalculation.java
字号:
package com.ttdev.tddcalc;
import java.net.*;
import java.util.*;
import junit.framework.*;
import com.gargoylesoftware.htmlunit.*;
import com.gargoylesoftware.htmlunit.html.*;
public class TestsForCalculation extends TestCase {
private WebClient client;
private HtmlPage page;
private HtmlForm form;
protected void setUp() throws Exception {
client = new WebClient();
URL url = new URL("http://localhost:8080/TDDCalc/app");
page = (HtmlPage) client.getPage(url);
form = page.getFormByName("calcForm");
}
public void testCanInputTwoIntegers() throws Exception {
assertTrue(form.getInputByName("num1") instanceof HtmlTextInput);
assertTrue(form.getInputByName("num2") instanceof HtmlTextInput);
}
public void testAdd() throws Exception {
HtmlTextInput num1 = (HtmlTextInput) form.getInputByName("num1");
num1.setValueAttribute("1");
HtmlTextInput num2 = (HtmlTextInput) form.getInputByName("num2");
num2.setValueAttribute("2");
HtmlSelect operator = form.getSelectByName("operator");
operator.getOption(0).setSelected(true);
HtmlSubmitInput ok = (HtmlSubmitInput) form.getInputByName("OK");
HtmlPage resultPage = (HtmlPage) ok.click();
HtmlElement result = resultPage.getHtmlElementById("result");
assertEquals(result.asText(), "Result: 3");
}
public void testOperatorList() throws Exception {
HtmlSelect operator = form.getSelectByName("operator");
assertEquals(operator.getOptionSize(), 4);
assertEquals(operator.getOption(0).asText(), "Add");
assertEquals(operator.getOption(1).asText(), "Minus");
assertEquals(operator.getOption(2).asText(), "Multiply");
assertEquals(operator.getOption(3).asText(), "Divide");
List selectedOptions = operator.getSelectedOptions();
assertEquals(selectedOptions.size(), 1);
assertEquals(((HtmlOption)selectedOptions.get(0)).asText(), "Add");
}
public void testMinus() throws Exception {
HtmlTextInput num1 = (HtmlTextInput) form.getInputByName("num1");
num1.setValueAttribute("1");
HtmlTextInput num2 = (HtmlTextInput) form.getInputByName("num2");
num2.setValueAttribute("2");
HtmlSelect operator = form.getSelectByName("operator");
operator.getOption(1).setSelected(true);
HtmlSubmitInput ok = (HtmlSubmitInput) form.getInputByName("OK");
HtmlPage resultPage = (HtmlPage) ok.click();
HtmlElement result = resultPage.getHtmlElementById("result");
assertEquals(result.asText(), "Result: -1");
}
public void testOKLabel() throws Exception {
assertEquals(form.getInputByName("OK").getValueAttribute(), "OK");
}
public void testBadInt() throws Exception {
HtmlTextInput num1 = (HtmlTextInput) form.getInputByName("num1");
num1.setValueAttribute("abc");
HtmlTextInput num2 = (HtmlTextInput) form.getInputByName("num2");
num2.setValueAttribute("2");
HtmlSelect operator = form.getSelectByName("operator");
operator.getOption(0).setSelected(true);
HtmlSubmitInput ok = (HtmlSubmitInput) form.getInputByName("OK");
List alerts = new ArrayList();
client.setAlertHandler(new CollectingAlertHandler(alerts));
ok.click();
assertEquals(alerts.size(), 1);
assertTrue(((String) alerts.get(0)).indexOf("number 1") != -1);
}
public HtmlPage getPage() {
return page;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -