更新时间:2023-12-03 14:41:46
public class LoginPageTest extends IntegrationTest {
private HtmlUnitDriver driver;
@Before
public void setup() throws MalformedURLException, UnknownHostException{
driver = new HtmlUnitDriver(true);
driver.get(System.getProperty("login.url"));
}
@Test
public void testAuthenticationFailureWhenProvidingBadCredentials(){
driver.findElement(By.id("username")).sendKeys("fakeuser");
driver.findElement(By.id("password")).sendKeys("fakepassword");
driver.findElement(By.id("login")).click();
assertTrue(driver.getCurrentUrl().endsWith("failed"));
}
@Test
public void testAuthenticationSuccessWhenProvidingCorrectCredentials(){
driver.findElement(By.id("username")).sendKeys("validuser");
driver.findElement(By.id("password")).sendKeys("validpassword");
driver.findElement(By.id("login")).click();
assertTrue(driver.getCurrentUrl().endsWith("/<name_of_webapp>/"));
}
}
例如,我就是这样做的.
That's how I do it for example.
我刚刚注意到评论.无论如何,我的代码显示了如何使用Selenium测试实际的登录页面.
I just noticed comments. Anyway my code shows how you test the actual login page with Selenium.