目标实现:
1.自动登录测试帐号
2.自动进行Single Sign On登录测试
3.登录失败的能够截图保存
4.测试报告中能够详尽展出测试数据以及测试结果
一、项目结构:
(1)base包:用来存放元素对象,将测试的元素对象插入数据库中保存,好用来做为测试用例维护。
ObjectTest.java
package com.annie.base;
import java.util.Date;
public class ObjectTest {
private int id;
private String website;
private String page;
private String area;
private String object_type;
private String object_key_type;
private String object_pagename;
private String object_key;
private String description;
private String status;
private Date createtime;
private String createuser;
private Date updatetime;
private String updateuser;
private Date deletetime;
private String deleteuser;
//后面省去get/set方法
}
(2)util包:用来存放工具类,比如ExelData这样的工具类
(注意1.exel测试数据文档的路径;2.exel测试数据文档名;3.exel测试数据文档的testcase名,即sheet名)
package com.annie.util;
import java.io.File;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import jxl.*;
public class ExcelData implements Iterator
(3)testng包:用来存放公共的方法实现“高内聚,低耦合”的初衷(需要后面完善)
package com.annie.testng;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import com.annie.util.ExcelData;
;
public class TestNG {
private WebDriver driver;
@Parameters({ "browser" })
@BeforeTest
public void setupBrowser(String browser) {
if (browser.equals("firefox")) {
driver = new FirefoxDriver();
} else {
driver = new ChromeDriver();
}
}
@Parameters({ "url", "keyword" })
@Test
public void search(String url, String keyword) {
driver.get(url);
WebElement element = driver.findElement(By.name("q"));
element.sendKeys(keyword);
element.submit();
Assert.assertTrue(driver.getTitle().contains(keyword),
"Title is wrong!");
}
// 传入登录用户名、密码,获取元素,验证空用户/密码登录
@Parameters({ "url", "login_name_txtUserName", "login_name_txtPwd",
"login_name_btnLogin" })
// 参数: URL、用户名name、密码name、登陆按钮name
@Test(groups = { "login" })
public void login_username_empty(String login_name_txtUserName,
String login_name_txtPwd, String login_name_btnLogin)
throws Exception {
System.out.print("login_username_empty");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.name(login_name_txtUserName)).clear();
driver.findElement(By.name(login_name_txtPwd)).clear();
driver.findElement(By.name(login_name_txtUserName)).sendKeys("");
driver.findElement(By.name(login_name_txtPwd)).sendKeys("");
driver.quit();
}
}
(4)testcase包:用来存放编写的测试用例
LoginTest.java
package com.annie.testcase;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.annie.testng.TestNG;
import com.annie.util.ExcelData;
import com.annie.util.ScreenShot;
public class LoginTest {
private WebDriver driver;
private String username = "";
private String password = "";
private String URL = "http://10.0.0.88:8080/bkredcoil/web/index.html";
TestNG tn = new TestNG();
@DataProvider(name = "accounts")
// 登录用户的账户Excel,返回的是Object数组
public Iterator
Single Sign On 只需更改testng.xml
LoginTestURL.java
package com.annie.testcase;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.annie.testng.TestNG;
import com.annie.util.ExcelData;
import com.annie.util.ScreenShot;
public class LoginTestURL {
private WebDriver driver;
// private String username, password;
private String sid,url;
TestNG tn = new TestNG();
@DataProvider(name = "accounts")
// 登录用户的账户Excel,返回的是Object数组
public Iterator data() throws Exception {
return (Iterator) new ExcelData("accounts_login",
"BKID");
}
public void prmap(Map arr) throws Exception {
driver = new FirefoxDriver();
// driver.get("http://redcoil.technologystudios.com/index.html");
// driver.get("http://10.0.0.88:8080/bkredcoil/index.html");
/*
* driver.findElement(By.id("username")).clear();
* driver.findElement(By.id("password")).clear();
*/
Set set = arr.keySet();
Iterator it = set.iterator();
while (it.hasNext()) {
String sid = (String) it.next();
/*
* if (s.equals("Email")) { username = arr.get(s); } else { password
* = arr.get(s); }
*/
System.out.println(arr.get(sid));
url="http://10.0.0.88:8080/bkredcoil/controller/security/signon?sid="+arr.get(sid);
System.out.println(url);
driver.get(url);
driver.manage().timeouts().implicitlyWait(500, TimeUnit.SECONDS);
/*
* if (driver.findElement(By.id("username")).getText().length()==0)
* { driver.findElement(By.id("username")).clear();
* driver.findElement(By.id("username")).sendKeys(username); } if
* (driver.findElement(By.id("password")).getText().length()==0) {
* driver.findElement(By.id("password")).clear();
*
*
*
* driver.findElement(By.id("btn-login")).click();
*/
driver.manage().timeouts().implicitlyWait(500, TimeUnit.SECONDS);
Thread.sleep(500);
driver.findElement(By.id("button-1056-btnInnerEl")).click();
driver.close();
// ScreenShot ss = new ScreenShot();
// try{
// ss.cutshot(driver);
// }
// catch(Exception e){
// e.printStackTrace();
//
// }
}
}
// 写一个类似prmap的方法,将Map类型的exel数据传入,并在指定的URL进行登录,将Map中的Email和password 做参数传入登陆即可
@Test(dataProvider = "accounts")
public void ts(Map data) throws Exception {
this.prmap(data);
// 每次打印一组account,则调用登录用例
System.out.println("=====over=====");
System.out.println("");
}
}
testng.xml
build.xml
则执行后,会不断打开页面进行exel表格中的账户登录accounts_login.xls(sheet name is testA )
测试报告在test-output文件夹中生成
打开默认生成的测试报告(后面会美化测试报告步骤)
screenpic文件夹下自动保存登录失败的截图:
未完待续
需要解决:
1.登录失败如何设计测试用例,以及在report中展现fail的个例
2.如何维护测试用例(面向对象的设计)