我有下面的代碼,這段代碼應該通過測試,但如果我使用錯誤的憑據,它仍然通過測試,即使憑據不正確。登錄驗證?
我是新來硒webdriver的,我不知道什麼做錯了。
public class LoginTest {
String url ="jdbc:sqlserver://CODESV3;databaseName=Codes;integratedSecurity=true";
String DBdriver ="com.microsoft.sqlserver.jdbc.SQLServerDriver";
Connection conn = null;
WebDriver driver = null;
PreparedStatement pstmt=null;
ResultSet rs=null;
@BeforeTest
public void establishConn()
{
driver = new FirefoxDriver();
driver.get("https://10.10.10.50/");
// establish connection
try{
Class.forName(DBdriver).newInstance();
conn = DriverManager.getConnection(url);
}catch(Exception e){
System.out.println("Database failed to connect ");
}
}
@Test
public void testLogin()
{
String forceID="1234";
String username="ayaslem";
String password="Delpiero10+";
boolean valueFound=true;
// Check the db
try{
pstmt=conn.prepareCall("select * from Login where ForceID=?, and Username=? and Password=?");
pstmt.setString(1,forceID);
pstmt.setString(2,username);
pstmt.setString(3,password);
rs=pstmt.executeQuery();
valueFound = rs.next();
}catch(Exception e){
// report some error
}
System.out.println(valueFound);
// login into app
driver.findElement(By.xpath("//*[@id='LogonModel_OrganisationName']")).sendKeys(forceID);
driver.findElement(By.xpath("//*[@id='LogonModel_UserId']")).sendKeys(username);
driver.findElement(By.xpath("//*[@id='LogonModel_Password']")).sendKeys(password);
driver.findElement(By.xpath("//*[@id='maincontent']/form/div/fieldset/div[4]/div[2]/input")).click();
String actual_title = driver.getTitle();
System.out.println(valueFound);
if(valueFound){
Assert.assertEquals(actual_title,"Dashboard");
}else{
Assert.assertEquals(actual_title,"Logon");
}
}
@AfterTest
public void closeConn() throws SQLException
{
if(conn!=null && !conn.isClosed())
{
conn.close();
}
}
}
valueFound = rs.next();你確定rs.next()在未找到的查詢上返回false?在我的經驗,它將返回true上沒有發現 –
rs.next檢查下一行,如果在數據庫中存在的憑據valueFound應該返回true,如果他們不存在valueFound應該返回false查詢一組0。 –
我知道你是「想」做什麼,但rs.next()只有在查詢寫得不好(即語法錯誤)返回false它總是返回true,如果查詢是正確的(即使WHERE條件不滿意)。你的SELECT語句編寫正確,因此rs.next()返回true –