import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.GetMethodWebRequest;
import org.apollo.test.util.LoginTestInf;
/**
* @author sixsun
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class LoginTest extends TestCase implements LoginTestInf {
private String username = "suibian";
private String password = "suibian";
private WebConversation browser;
private WebRequest requestIndex;
private WebRequest requestLogin;
private WebResponse responseIndex;
private WebResponse responseLogin;
private String urlSystem = "系统首页网址";
private String urlLogin = "登陆界面网址";
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
browser = new WebConversation();
requestIndex = new GetMethodWebRequest(urlSystem);
responseIndex = browser.getResponse(requestIndex);
requestLogin = new GetMethodWebRequest(urlLogin);
responseLogin = browser.getResponse(requestLogin);
}
//输入登陆地址的页面地址,验证该页面是否可被正常访问
public void testValidPage() throws Exception{
assertNotNull("zsonline在网络上不存在!",responseIndex);
}
//验证被访问的页面是否是登陆页面
public void testIsLoginPage() throws Exception{
URL currentUrl = responseLogin.getURL();
String currentUrlStr = currentUrl.getProtocol() + "://" +currentUrl.getHost() + currentUrl.getPath();
assertEquals("登陆页面不是zsonline首页!" ,currentUrlStr,urlLogin);
}
//输入非法用户名、密码,验证登陆失败
public void testBadLogin() throws Exception{
WebForm form = responseLogin.getForms()[0];
form.setParameter("username","badname");
form.setParameter("password","badpassword");
requestLogin = form.getRequest();
responseLogin = browser.getResponse(requestLogin);
assertTrue("用户名不存在,请确认用户名输入是否完全正确(区分大小写)!",
responseLogin.getText().indexOf("用户名不存在,请确认用户名输入是否完全正确(区分大小写)!") != -1);
}
//输入合法用户名、密码,验证登陆成功
public void testGoodLogin() throws Exception{
WebForm form = responseLogin.getForms()[0];
form.setParameter("username",username);
form.setParameter("password",password);//此处需要填写真实密码
requestLogin = form.getRequest();
responseLogin = browser.getResponse(requestLogin);
assertTrue("转到'zsonline'【suibian】用户首页失败!",responseLogin.getText().indexOf("用户测试用户_zsonline,您好!") != -1);
}
public static TestSuite suite(){
return new TestSuite(LoginTest.class);
}
public static void main(String args[]){
TestRunner.run(suite());
}
延伸阅读
文章来源于领测软件测试网 https://www.ltesting.net/