一、框架目录结构
二、代码
2.1page层代码
1 package com.mianshui.page; 2 3 import org.openqa.selenium.WebElement; 4 import org.openqa.selenium.support.FindBy; 5 6 7 public class LoginPage { 8 9 10 //用户名输入框 11 @FindBy(className="dlemail") 12 private WebElement uName; 13 //密码输入框 14 @FindBy(className="dlpwd") 15 private WebElement pwd; 16 //登录按钮 17 @FindBy(id="dologin") 18 private WebElement loginButton; 19 //继续登录按钮 20 @FindBy(className="u-btn") 21 private WebElement continueLogin; 22 23 //输入用户名 24 public void inputUsername(String userName){ 25 System.out.println(userName); 26 uName.clear(); 27 uName.sendKeys(userName); 28 29 } 30 //输入密码 31 public void inputPwd(String passWord){ 32 pwd.clear(); 33 pwd.sendKeys(passWord); 34 } 35 //单击登录 36 public void clickLoginButton(String string){ 37 loginButton.click(); 38 } 39 //单击继续登录 40 public void continueLogin(String string){ 41 continueLogin.click(); 42 } 43 }
2.2 case层
1 package com.mianshui.testScript; 2 3 import org.openqa.selenium.WebDriver; 4 import org.openqa.selenium.chrome.ChromeDriver; 5 import org.openqa.selenium.firefox.FirefoxDriver; 6 import org.openqa.selenium.support.PageFactory; 7 import org.testng.Assert; 8 import org.testng.annotations.AfterMethod; 9 import org.testng.annotations.BeforeMethod; 10 import org.testng.annotations.Test; 11 import com.mianshui.page.LoginPage; 12 import com.mianshui.page.MainPage; 13 import com.mianshui.util.ExcelUtil; 14 /* 15 * 定位语句和测试代码分离:封装在page类中 16 * 测试数据与测试代码分离:读取excel文件 17 */ 18 public class Login126Mail_PO { 19 public static WebDriver driver; 20 @Test 21 public static void login126Mail() throws Exception { 22 //定义用例路径 23 String excelPath= "D://MyJavaWorkSpace//TestProject//src//com//mianshui//data//126MailLoginCase.xlsx"; 24 //读取用例sheet页 25 ExcelUtil.setExcelFile(excelPath, "login"); 26 //打开浏览器 27 String BrowserName=ExcelUtil.getCellData(1, 4); 28 if (BrowserName.equalsIgnoreCase("firefox")) { 29 driver=new FirefoxDriver(); 30 }else if (BrowserName.equalsIgnoreCase("chrome")) { 31 driver=new ChromeDriver(); 32 } 33 34 //输入网址 35 driver.get(ExcelUtil.getCellData(2, 4)); 36 //切换frame 37 driver.switchTo().frame("x-URS-iframe"); 38 39 //初始化page页面(注意:要放在打开浏览器之后) 40 LoginPage loginPage=PageFactory.initElements(driver, LoginPage.class); 41 MainPage mainPage=PageFactory.initElements(driver, MainPage.class); 42 //输入账户 43 loginPage.inputUsername(ExcelUtil.getCellData(3, 4)); 44 //输入密码 45 loginPage.inputPwd(ExcelUtil.getCellData(4, 4)); 46 //单击登录 47 loginPage.clickLoginButton(null); 48 //单击继续登录 49 Thread.sleep(3000); 50 loginPage.continueLogin(null); 51 //断言登录是否成功 52 Thread.sleep(2000); 53 Assert.assertEquals(mainPage.getEmailAdd(), ExcelUtil.getCellData(3, 4)+"@126.com"); 54 } 55 56 @BeforeMethod 57 public static void beforeMethod(){ 58 59 } 60 61 @AfterMethod 62 public static void afterMethod(){ 63 64 } 65 }
2.3 工具类(excel操作、截图、等待方法.....)

1 package com.mianshui.util; 2 3 4 import java.io.FileInputStream; 5 import org.apache.poi.xssf.usermodel.XSSFCell; 6 import org.apache.poi.xssf.usermodel.XSSFSheet; 7 import org.apache.poi.xssf.usermodel.XSSFWorkbook;; 8 9 //此类事实现操作指定的excel文件中的指定sheet页、读取指定的单元格内容、获取sheet中最后一行的行号的功能 10 public class ExcelUtil { 11 12 private static XSSFSheet ExcelWSheet; 13 private static XSSFWorkbook ExcelWBook; 14 private static XSSFCell Cell; 15 // 设定要操作的excel的文件路径和sheet名称 16 // 在读、写excel文件时,均需先调用此方法,设定要操作的excel文件路径和要操作的sheet名称 17 public static void setExcelFile(String Path,String SheetName){ 18 19 FileInputStream ExcelFile; 20 21 22 try { 23 //实例化excel文件的FileInputStream 对象 24 ExcelFile = new FileInputStream(Path); 25 26 //实例化excel文件的 XSSFWorkbook 对象 27 ExcelWBook = new XSSFWorkbook(ExcelFile); 28 29 //实例化 XSSFCell 对象,指定excel文件中的sheet名称,后续用于sheet中行、列和单元格的操作 30 ExcelWSheet = ExcelWBook.getSheet(SheetName); 31 32 } catch (Exception e) { 33 // TODO Auto-generated catch block 34 e.printStackTrace(); 35 } 36 } 37 //读取excel文件中指定的单元格的函数,此函数只支持扩展名为 .xlsx 的excel文件 38 public static String getCellData (int RowNum, int ColNum) throws Exception { 39 40 try { 41 //通过函数参数指定的单元格的行号和列号,获取指定的单元格对象 42 Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum); 43 //如果单元格的内容为字符串类型,则使用getStringCellValue方法来获取单元格内容 44 //如果单元格的内容为数字类型, 则使用getNumericCellValue方法来获取单元格内容 45 46 String CellData = Cell.getCellType() == XSSFCell.CELL_TYPE_STRING ? Cell.getStringCellValue() + "" : String.valueOf(Math.round(Cell.getNumericCellValue())); 47 48 49 return CellData; 50 51 }catch (Exception e){ 52 e.printStackTrace(); 53 //读取遇到异常,则返回空字符串 54 return "错了"; 55 } 56 } 57 //获取excel文件的最后一行的行号 58 public static int getLastRowNum(){ 59 //函数返回sheet的最后一行的行号 60 return ExcelWSheet.getLastRowNum(); 61 } 62 63 }
2.4 data层