如何使用Java和Selenium WebDriver验证登录页面? [英] How to valid a login page using Java and Selenium WebDriver?
本文介绍了如何使用Java和Selenium WebDriver验证登录页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的数据库中,我的用户名= user@javachap.com,密码= javachap
In my database I have the username = user@javachap.com and password = javachap
如果我运行下面的代码,尽管用户名和密码在我的数据库中不存在,但它通过了测试.
If I run the code below, it passes the test although the username and password does not exist in my database.
@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
}
推荐答案
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.
这篇关于如何使用Java和Selenium WebDriver验证登录页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文