1. driver和基类封装
DriverBase:
package HelloMaven.base;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class DriverBase {
public WebDriver driver;
public DriverBase(String browser){
SelectDriver selectdriver= new SelectDriver();
this.driver = selectdriver.DriverSelect(browser);
}
public void stopDriver() {
System.out.println("stop the driver");
driver.close();
}
/**
* 定位element,封装
* */
public WebElement findElement(By by) {
return driver.findElement(by);
}
public void get(String url) {
driver.get(url);
}
}
SelectDriver 根据浏览器选择driver:
package HelloMaven.base;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class SelectDriver {
public WebDriver DriverSelect(String browser) {
if (browser.equalsIgnoreCase("firefox")) {
System.setProperty("webdriver.firefox.driver", "F:\\Java\\FireFoxdriver\\geckodriver.exe");
return new FirefoxDriver();
}else {
System.setProperty("webdriver.chrome", "F:\\Java\\ChromeDriver\\chromedriver.exe");
return new ChromeDriver();
}
}
}
元素定位的存放和读取:
package HelloMaven.base;
import org.openqa.selenium.By;
import HelloMaven.HelloMaven.ProUtil;
public class ElementProperties {
public By bystr(String element){
ProUtil property = new ProUtil("element.properities");
String location = property.GetProperies(element);
System.out.println("the file value get from properties file is: " + location);
String way = location.split(">")[0];
String value = location.split(">")[1];
System.out.println("value1 is " + way + ", and value2 is:" + value);
if(way.equals("id")) {
return By.id(value);
}else if(way.equals("name")){
return By.name(value);
}else if(way.equals("className")) {
return By.className(value);
}else if(way.equals("xpath")) {
return By.xpath(value);
}else
{return null;}
}
}
BasePage 基础页面的单元性操作行为:
package HelloMaven.page;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import HelloMaven.base.DriverBase;
public class BasePage {
public DriverBase driver;
public BasePage(DriverBase driver) {
this.driver = driver;
}
/**
* 定位element
* */
// public WebElement findElement(By by) {
// return driver.findElement(by);
// }
/**
* 封装点击
* */
public void click(WebElement element) {
if(element != null) {
element.click();
}else {
System.out.println("cannot find the element, failed to click");
}
}
/**
* 封装输入
* */
public void input(WebElement element, String value) {
if(element != null) {
element.sendKeys(value);
}else {
System.out.println("cannot find the element, failed to input value");
}
}
/**
* 封装元素是否存在
* */
public boolean IsDisplayed(WebElement element) {
return element.isDisplayed();
}
}
LoginPage 具体页面上元素的获得:
package HelloMaven.page;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import HelloMaven.base.DriverBase;
import HelloMaven.base.ElementProperties;
/**
* 获得login页面的元素
* */
public class LoginPage extends BasePage{
public LoginPage(DriverBase driver ) {
super(driver);
}
ElementProperties EP = new ElementProperties();
/**
* 获取username输入框
* */
public WebElement getUserName() {
return driver.findElement(EP.bystr("username"));
}
public WebElement getPassword() {
return driver.findElement(EP.bystr("password"));
}
public WebElement LoginButton() {
return driver.findElement(EP.bystr("loginbutton"));
}
}
LoginHandle 具体页面上的操作:
package HelloMaven.Handle;
import HelloMaven.base.DriverBase;
import HelloMaven.page.LoginPage;
/**
* 封装login页面所需的action,执行action就需要在调用的函数里new LoginHandle的对象
* */
public class LoginHandle {
public DriverBase driver;
public LoginPage loginpage;
public LoginHandle(DriverBase driver) {
this.driver = driver;
this.loginpage = new LoginPage(driver); //如果放在构造函数外面,就是全局变量的那个driver。
系统在调用LoginHandle的时候,首先会读取全局变量。所以放在外面,driver还没有传过来
}
/**
* 输入用户名
* */
public void InputUsername(String username) {
loginpage.input(loginpage.getUserName(), username);
}
public void InputPassword(String password) {
loginpage.input(loginpage.getPassword(), password);
}
public void clickLogin() {
loginpage.click(loginpage.LoginButton());
}
public boolean IsLoginPage() {
return loginpage.IsDisplayed(loginpage.getUserName());
}
}
LoginPro 具体页面的业务流程:
package HelloMaven.business;
import HelloMaven.Handle.LoginHandle;
import HelloMaven.base.DriverBase;
public class LoginPro {
public LoginHandle loginHandler; //用handler来具体操作页面
public LoginPro(DriverBase driver) {
loginHandler = new LoginHandle(driver); //划重点
}
public void Login(String username, String password) {
if (loginHandler.IsLoginPage()) {
loginHandler.InputUsername(username);
loginHandler.InputPassword(password);
loginHandler.clickLogin();
loginHandler.loginpage.getUserName();
}else {
System.out.println("current page is not Login Page, or not loaded");
}
}
}
创建test case
首先创建caseBase,用于testcase中初始化driver的操作,适用于所有testcase
package HelloMaven.testcase;
import HelloMaven.base.DriverBase;
/**
* 生成driver,所有的测试类都继承caseBase类,拿到driver
* */
public class caseBase {
public DriverBase InitialDriver(String browser) { //初始化driver,选择driver
return new DriverBase(browser);
}
}
创建具体case,testLogin
package HelloMaven.testcase;
import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.testng.annotations.Test;
import HelloMaven.base.DriverBase;
import HelloMaven.business.LoginPro;
public class testLogin extends caseBase{
public DriverBase driver;
public LoginPro Lpro;
static Logger logger = Logger.getLogger(testLogin.class);
public testLogin() {
this.driver = InitialDriver("Chrome");
this.Lpro = new LoginPro(driver);
}
@Test
public void getLoginHome() {
driver.get("http://www.imooc.com");
driver.findElement(By.id("js-signin-btn")).click();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test(dependsOnMethods= {"getLoginHome"})
public void Login() {
logger.debug("first time for logging");
Lpro.Login("13610056184", "MISSjj28*");
}
@Test(dependsOnMethods= {"Login"})
public void stop() {
driver.stopDriver();
}
}