package selenium.test;
import org.openqa.selenium.server.SeleniumServer;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
import junit.framework.TestCase;
public class SwiftFundTest extends TestCase {
private Selenium user;
private Selenium page;
protected void setUp() throws Exception {
super.setUp();
user = new DefaultSelenium("localhost",SeleniumServer.DEFAULT_PORT,"*iexplore","http://localhost:8080/swfund");
page = user;
user.start();
}
protected void tearDown() throws Exception {
user.stop();
super.tearDown();
}
private void userLogin(String loginame,String password){
user.type("loginame", loginame);
user.type("password", password);
user.click("//input[@value='Confirm']");
user.waitForPageToLoad("5000");
}
private void isLoginPage() {
assertEquals("User Login",page.getTitle());
}
public void testUserLogin(){
user.open("http://localhost:8080/swfund/");
isLoginPage();
userLogin("","password");
assertTrue(page.isTextPresent("please enter loginame"));
userLogin("admin","passw0rd");
isLoginPage();
assertTrue(page.isTextPresent("login failed"));
userLogin("admini","password");
assertTrue(page.isTextPresent("user is not exist"));
userLogin("admin","password");
assertEquals("SWIFTNet Fund",page.getTitle());
user.selectFrame("top");
assertTrue(page.isTextPresent("SWIFTNet Fund"));
user.selectWindow("left");
assertTrue(page.isTextPresent("Welcome you"));
assertTrue(page.isTextPresent("admin user"));
user.selectWindow("right");
//...
}
}
对于isTextPresent方法,如果字符串前没有任何前缀,则表示使用glob pattern,比如:
page.isTextPresent("login failed*");
如果要使用正则表达式,则要在字符串前面显式的增加【regexp:】前缀,如:
page.isTextPresent("regexp:login failed.*");//注意,必需是javascript支持的正则表达式。