我有以下代码,此代码应通过测试,但如果我使用了错误的凭据,即使凭据不正确,它仍会通过测试。
我是Selenium 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();
}
}
}
答案 0 :(得分:0)
只有2个断言可能会使您的测试失败(当然,除了抛出的任何异常)。所以,让我们只关注它们。
当您提供虚假登录名和密码时,您的valueFound
变量将设置为false,并且它将跟随到else块,其中JUnit断言webdriver是否仍在登录站点上。由于您使用虚假凭据登录后,您正在测试的Web应用程序最有可能停留在登录页面上,因此断言通过(因为它检查它是否仍在登录页面上,通过检查页面'标题)。
当您提供正确的登录名和密码时,if
条件通过,因为在数据库中找到此类用户后valueFound
设置为true,并继续断言Webapp是否将您重定向到Dashboard页面
我认为您的测试很好地涵盖了这两种情况,检查应用程序是否使用虚假数据保留在当前页面上,并检查它是否导航到具有真实数据的仪表板页面。在这两种情况下,它都应该通过我的意见。
P.S。这是一个提示 - 您不需要在任何地方使用xpath。有一些专门的方法,如By#id(int)
,可以让您的自动化测试工程师更轻松:)
另外,使用我称之为弱xpath的方法是一种非常糟糕的做法(//*[@id='maincontent']/form/div/fieldset/div[4]/div[2]/input
)。如果页面上的任何排序发生变化,或者任何元素被包含在中间div中,则xpath将无法找到您正在寻找的元素。向Webapp的开发人员询问ID或名称,或在表单容器元素上使用By#tagName("button")
。