Ahm*_*lem 0 java selenium selenium-webdriver
在我的数据库中,我有username = user@javachap.com和密码= javachap
如果我运行下面的代码,它会通过测试,尽管我的数据库中不存在用户名和密码.
@Test
public void testLogin()
{
String username="abc";
String password="123";
boolean valueFound=false;
// Check the db
try
{
pstmt=conn.prepareCall("select * from user where USR_EMAIL=? and USD_PASSWORD=?");
pstmt.setString(1,username);
pstmt.setString(2,password);
rs=pstmt.executeQuery();
valueFound = rs.next();
}
catch(Exception e)
{
// report some error
}
Run Code Online (Sandbox Code Playgroud)
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>/"));
}
}
Run Code Online (Sandbox Code Playgroud)
这就是我如何做到这一点.
编辑:我刚刚注意到了评论.无论如何,我的代码显示了如何使用Selenium测试实际的登录页面.
| 归档时间: |
|
| 查看次数: |
57193 次 |
| 最近记录: |