1

我犯了一个成功硒测试用例登录页面,该用户输入正确的用户名和密码,然后点击登录转发到主页,问题是,当我在测试类更改密码,测试总是成功,我不知道为什么。如何在selenium测试用例中声明登录成功?

这里的测试类:

public class LoginTest extends TestCase { 

    private WebDriver driver; 
    private String baseUrl; 
    private StringBuffer verificationErrors = new StringBuffer(); 

    @Before 
    public void setUp() throws Exception { 
     driver = new FirefoxDriver(); 
     baseUrl = "http://localhost:8080"; 
    } 

    @Test 
    public void testLoginClass() throws Exception { 
     driver.get(baseUrl + "/MyAPP/login"); 
     driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); 

     driver.findElement(By.id("j_username")).clear(); 
     driver.findElement(By.id("j_username")).sendKeys("1"); 
     driver.findElement(By.id("j_password")).clear(); 
     driver.findElement(By.id("j_password")).sendKeys("wrong password"); 
     driver.findElement(By.id("loginBtn")).click(); 

    } 

    @After 
    public void tearDown() throws Exception { 
     driver.quit(); 
     String verificationErrorString = verificationErrors.toString(); 
     if (!"".equals(verificationErrorString)) { 
      fail(verificationErrorString); 
     } 
    } 

    @SuppressWarnings("unused") 
    private boolean isElementPresent(By by) { 
     try { 
      driver.findElement(by); 
      return true; 
     } catch (NoSuchElementException e) { 
      return false; 
     } 
    } 

} 

请告知如何处理测试案例的失败,让我们假设一段时间之后,数据库中更改,有没有这样的用户1,它是成功的现在。

回答

8

你不检查,看看是否错了密码登录的用户。

你需要把一个测试用例登录后进行检查,如果出现一些特殊的文本。

例如,如果用户成功登录,欢迎屏幕将显示“Hi UserName”。所以,你需要assert如果文本存在的硒脚本击中的loginBtn

目前,所有你做的是发送“一些”文字作为密码,硒显然不会知道,如果密码不正确后,除非您验证输入错误密码的结果。

编辑


在RUBY-

assert(@driver.find_element(:tag_name => "body").text.include?("Welcome UserName"),"The User successfully logs in") 
+0

代码片断请这样断言? – 2012-01-17 15:47:20

+0

我已经做了一个编辑,包括断言,它在红宝石 – Amey 2012-01-17 16:11:42

+0

那么为你工作吗? – Amey 2012-01-17 19:49:54

0

的代码段为了验证您的情况,您的应用程序应该显示一条消息,如 “输入的密码无效。”

然后在登录按钮单击后尝试此操作。

try { 
    assertEquals(driver.findElement(By.id("Your Id for the message")).getText(), "Invalid UserID or Password Entered"); 

    //If the message is displayed 

    System.out.println("PASS"); 

} catch (Exception e) { 

    //If the message is not displayed 

    System.out.println("FAIL"); 

    verificationErrors.append(e.toString()); 

}