Selenium学习_java/testNG编写测试用例实践1

上一篇 / 下一篇  2015-08-17 14:18:18 / 个人分类:selenium

对某个登录页面的测试,使用了testNG测试框架
包括两个测试用例
1、正确的用户名、密码,登录成功,验证title正确
2、错误的用户名、密码,登录失败,验证title正确

源码:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class Login {
    ProfilesIni allProfiles = new ProfilesIni();
    FirefoxProfile profile = allProfiles.getProfile("default");
    WebDriver driver;
    
    @BeforeMethod
    public void beforeLogin(){
        driver=new FirefoxDriver(profile);
        driver.get("http://***/auth/login");      
    }
       
    //用例1:正确用户名、密码,登录成功,title正确为验证方式
    @Test
    public void testCase1() throws InterruptedException {
        WebElement username= driver.findElement(By.id("username"));
        WebElement password= driver.findElement(By.id("password"));
        WebElement login = driver.findElement(By.linkText("登录"));
        username.sendKeys("******");  //输入用户名
        password.sendKeys("******");  //输入密码
        login.click();
        Thread.sleep(5000);
       
        driver.switchTo().window(driver.getWindowHandle());
        Assert.assertEquals(driver.getTitle(), "XXX");
    }
   
    //用例2:错误密码,登录失败,title正确为验证方式
    @Test
    public void testCase2() throws InterruptedException {
        WebElement username= driver.findElement(By.id("username"));
        WebElement password= driver.findElement(By.id("password"));
        WebElement login = driver.findElement(By.linkText("登录"));
        username.sendKeys("******");
        password.sendKeys("******");
        login.click();
        Thread.sleep(5000);   
       
        driver.switchTo().window(driver.getWindowHandle());
        Assert.assertEquals(driver.getTitle(), "登录 - XXX");       
    }
@AfterMethod
    public void afterLogin(){
        driver.close();     
    }

}
 
运行项目(run as testNG test)输出测试结果。
项目目录下生成test-output的文件夹,文件夹内的index.html页面是测试用例的运行结果报告。
 

TAG: java

 

评分:0

我来说两句

日历

« 2024-05-17  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 10615
  • 日志数: 7
  • 建立时间: 2015-08-17
  • 更新时间: 2015-11-09

RSS订阅

Open Toolbar