效果图就是慕课网登录的一个全过程
运行条件:
1.maven工程
2.testNg
3.properties配置文件
pom.xml截图:
properties配置文件:
userName=name>email
loginButton=id>js-signin-btn
userPass=name>password
buttonElement=className>btn-red
header=id>header-avator
nameInfo=className>name
autoSigin=id>auto-signin
项目目录:
base包下的DriverBase与SelectDriver类
SelectDriver类:
package com.selenium.wushuaiTest.base
import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.firefox.FirefoxDriver
//选择浏览器类型的类
public class SelectDriver {
public WebDriver driverName(String browser) {
//equalsIgnoreCase代表不区分大小写
//判断浏览器的类型是"firefox"或者"chrome"又或者是"IE"
if(browser.equalsIgnoreCase("fireFox"))
{
System.setProperty("webdriver.firefox.marionette", "D:\\\\java\\\\geckodriver\\\\geckodriver.exe")
return new FirefoxDriver()
}else{
System.setProperty("webdriver.chrome.driver","D:\\\\java\\\\chromedriver-32\\\\chromedriver.exe")
return new ChromeDriver()
}
}
}
SelectDriver作用:
判断并且选择自动化运行的浏览器类型
DriverBase类
package com.selenium.wushuaiTest.base;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import com.selenium.wushuaiTest.page.LoginPage;
public class DriverBase {
public WebDriver driver;
public DriverBase(String browser) {
SelectDriver selectDriver=new SelectDriver();
this.driver=selectDriver.driverName(browser);
}
public WebDriver getDriver() {
return driver;
}
public void stopDriver() {
System.out.println("Stop Driver!");
driver.close();
}
public WebElement findElement(By by) {
WebElement element=driver.findElement(by);
return element;
}
public void get(String url) {
driver.get(url);
}
}
DriverBase类作用:
1.确定当前的浏览器类型
2.封装浏览器的关闭,查找元素,跳转页面等功能
3.实例化SelectDriver类,并且传入浏览器驱动类型
util包下的GetByLocator与ProUtil类
GetByLocator类:
package com.selenium.wushuaiTest.util;
import org.openqa.selenium.By;
public class GetByLocator {
public static By getLocator(String key) {
ProUtil proUtil=new ProUtil("element.properties");
String locator=proUtil.getPro(key);
String locatorType=locator.split(">")[0];
String locatorValue=locator.split(">")[1];
if(locatorType.equals("id"))
{
return By.id(locatorValue);
}else if(locatorType.equals("name"))
{
return By.name(locatorValue);
}else if(locatorType.equals("className"))
{
return By.className(locatorValue);
}else
{
return By.xpath(locatorValue);
}
}
}
GetByLocator类作用:
1.确定properties配置文件,并且识别并读取配置文件的信息
2.根据配置文件的信息返回特定的By类型,By.id或name,xpath等
ProUtil类
package com.selenium.wushuaiTest.util;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class ProUtil {
private Properties prop;
private String filePath;
public ProUtil(String filePath)
{
this.filePath=filePath;
this.prop=readProperties();
}
private Properties readProperties(){
Properties properties=new Properties();
try {
InputStream inputStream=new FileInputStream(filePath);
BufferedInputStream bis=new BufferedInputStream(inputStream);
properties.load(bis);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return properties;
}
public String getPro(String key){
if(prop.containsKey(key))
{
String username=prop.getProperty(key);
return username;
}else{
System.out.println("你获取的值不存在");
return "";
}
}
}
ProUtil类作用:
1.确定并读取配置文件
2.封装getProperty方法
page包下的BasePage与LoginPage类:
BasePage类:
package com.selenium.wushuaiTest.page;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import com.selenium.wushuaiTest.base.DriverBase;
public class BasePage {
public DriverBase driverBase;
public BasePage(DriverBase driverBase) {
this.driverBase=driverBase;
}
public WebElement element(By by) {
WebElement ele=driverBase.findElement(by);
return ele;
}
public void click(WebElement element) {
if(element!=null) {
element.click();
}else {
System.out.println("元素未定位到,定位失败");
}
}
public void sendKeys(WebElement element,String value) {
if(element!=null) {
element.sendKeys(value);
}else {
System.out.println(element+"元素没有定位到,输入失败"+value);
}
}
public boolean assertElementIs(WebElement element) {
return element.isDisplayed();
}
}
BasePage类作用:
1.充当测试类的基类,比如登录,购物车等功能,参数是一个DriverBase类对象
2.封装浏览器驱动的查找,点击,输入,是否显示等方法
LoginPage类:–继承BasePage类
package com.selenium.wushuaiTest.page;
import org.openqa.selenium.WebElement;
import com.selenium.wushuaiTest.base.DriverBase;
import com.selenium.wushuaiTest.util.GetByLocator;
public class LoginPage extends BasePage{
public LoginPage(DriverBase driverBase) {
super(driverBase);
}
public WebElement getUserElement() {
return element(GetByLocator.getLocator("userName"));
}
public WebElement getPasswordElement() {
return element(GetByLocator.getLocator("userPass"));
}
public WebElement getLoginButtonElement() {
return element(GetByLocator.getLocator("buttonElement"));
}
public WebElement getAutoLoginElement() {
return element(GetByLocator.getLocator("autoSigin"));
}
}
LoginPage类作用:
1.得到properties类中的变量,并且根据变量得到相应的值,拿到该元素,如:用户名输入框,密码输入框,自动登录,登录等按钮
handle包下的LoginPageHandle类:
LoginPageHandle类:
package com.selenium.wushuaiTest.handle;
import com.selenium.wushuaiTest.base.DriverBase;
import com.selenium.wushuaiTest.page.LoginPage;
public class LoginPageHandle {
public DriverBase driverBase;
public LoginPage loginPage;
public LoginPageHandle(DriverBase driverBase) {
this.driverBase=driverBase;
loginPage=new LoginPage(driverBase);
}
public void sendKeysUserName(String userName) {
loginPage.sendKeys(loginPage.getUserElement(), userName);
}
public void sendKeysPassword(String password) {
loginPage.sendKeys(loginPage.getPasswordElement(),password);
}
public void clickLoginButton() {
loginPage.click(loginPage.getLoginButtonElement());
}
public void clickAutoSigin() {
loginPage.click(loginPage.getAutoLoginElement());
}
public boolean assertLoginPage() {
return loginPage.assertElementIs(loginPage.getUserElement());
}
}
LoginPageHandle类作用:
1.操作LoginPage中得到的元素,比如:用户名输入框,密码输入框,登录,自动登录等控件元素
2.参数是DriverBase,并且构造方法实例化了LoginPage类
business包下的LoginPro类:
LoginPro类:
package com.selenium.wushuaiTest.business;
import com.selenium.wushuaiTest.base.DriverBase;
import com.selenium.wushuaiTest.handle.LoginPageHandle;
public class LoginPro {
public LoginPageHandle loginPageHandle;
public LoginPro(DriverBase driverBase) {
loginPageHandle=new LoginPageHandle(driverBase);
}
public void login(String userName,String password) {
if(loginPageHandle.assertLoginPage()) {
loginPageHandle.sendKeysUserName(userName);
loginPageHandle.sendKeysPassword(password);
loginPageHandle.clickAutoSigin();
loginPageHandle.clickLoginButton();
}else {
System.out.println("页面不存在或者状态不正确");
}
}
}
LoginPro类作用:
1.进行逻辑判断,比如这里识别的是如果能够识别用户名输入框,那么就代表登录页面存在,就可以进行输入用户名,密码等操作
2.构造方法参数是DriverBase,并且实例化LoginPageHandle类
testCase包下的CaseBase与Login类:
CaseBase类:
package com.selenium.wushuaiTest.testCase;
import com.selenium.wushuaiTest.base.DriverBase;
public class CaseBase {
public DriverBase InitDriverBase(String browser) {
return new DriverBase(browser);
}
}
CaseBase类作用:
1.初始化DriverBase类
2.内部定义一个InitDriverBase方法,返回类型为DriverBase类型,参数类型是浏览器驱动名称
Login类:
package com.selenium.wushuaiTest.testCase;
import org.openqa.selenium.By;
import org.testng.annotations.Test;
import com.selenium.wushuaiTest.base.DriverBase;
import com.selenium.wushuaiTest.business.LoginPro;
public class Login extends CaseBase{
public DriverBase driverBase;
public LoginPro loginPro;
public Login() {
this.driverBase=InitDriverBase("chrome");
loginPro=new LoginPro(driverBase);
}
@Test
public void getLoginHome() {
driverBase.get("http://www.imooc.com");
driverBase.getDriver().manage().window().maximize();
driverBase.findElement(By.id("js-signin-btn")).click();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Test(dependsOnMethods={"getLoginHome"})
public void testLogin() {
loginPro.login("[email protected]", "1111");
}
}
Login类作用:
1.构造方法中确定DriverBase的类型为chrome,并且把它赋值给了实例化的LoginPro类
2.testLogin方法依赖getLoginHome方法运行
综上已经大致分析完毕
总结:
1.我们浏览器的类型一直是呈现一个层级传递的过程:
LoginPage-LoginPageHandle-LoginPro-Login-InitDriverBase
就是我们的LoginPage类先向LoginPageHandle
索要DriverBase的浏览器驱动,然而LoginPageHandle并没有
,所以LoginPageHandle向LoginPro索要,
之后LoginPro又向Login索要,
而Login调用父类的InitDriverBase
并且传入"浏览器名称"才最终确定浏览器驱动的类型
,之后InitDriverBase又先传给
Login-LoginPro-LoginPageHandle-LoginPage,
最终LoginPage拿到了DriverBase的驱动类型是Chrome