读取配置文件-重构封装

来源:互联网 发布:mac 4k 字体太小 编辑:程序博客网 时间:2024/06/04 23:27

项目结构图


Login.java

package com.gubai.selenium;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.interactions.Actions;public class Login {public WebDriver driver;public void InitDriver() {String url = "http://www.imooc.com/";System.setProperty("webdriver.chrome.driver", "C:\\driver\\chromedriver.exe");driver = new ChromeDriver();driver.get(url);driver.manage().window().maximize();this.elemnet(By.id("js-signin-btn")).click();}public void loginscript(String username,String userpass) throws Exception {this.InitDriver();Thread.sleep(3000);WebElement user = this.elemnet(this.byStr("username"));user.isDisplayed();WebElement password = this.elemnet(this.byStr("userpass"));password.isDisplayed();WebElement loginButton = this.elemnet(this.byStr("loginbutton"));loginButton.isDisplayed();user.sendKeys(username);password.sendKeys(userpass);loginButton.click();Thread.sleep(3000);WebElement header = this.elemnet(this.byStr("header"));header.isDisplayed();Actions actions = new Actions(driver);actions.moveToElement(header).perform();;String userInfo = this.elemnet(this.byStr("nameInfo")).getText();System.out.println(userInfo);if(userInfo.equals("mushishi_xu1")) {System.out.print("登陆成功");}else {System.out.print("登录失败");}}    /**封装By by **/    public By byStr(String username){ProUtil properties = new ProUtil("element.properties");String locator = properties.getPro(username);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 if(locatorType.equals("xpath")){    return By.className(locatorValue);    }else{    return By.xpath(locatorValue);    }    }   /**    * 封装Element     * **/    public WebElement elemnet(By by) {    WebElement ele = driver.findElement(by);    return ele;    }public static void main(String[] args) throws Exception{    Login login = new Login();    login.loginscript("账号","密码");}}
element.properties

username=name>emailuserpass=name>passwordloginbutton=className>btn-redheader=id>header-avatornameInfo=className>name
ProUtil.java(此部分代码未改动)

package com.gubai.selenium;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.util.Properties;public class ProUtil {private Properties prop;private String filepaht;/** * 构造方法 * **/public ProUtil(String filepath) {this.filepaht = filepath;this.prop = readProperties();}/** * 读取配置文件 * **/private Properties readProperties() {Properties properties = new Properties();try {InputStream inputstream = new FileInputStream(filepaht);InputStream in = new BufferedInputStream(inputstream);properties.load(in);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return properties;}public String getPro(String key)  {if(prop.containsKey(key)) {String username = prop.getProperty(key);return username;}else {System.out.println("你获取key值不对");return "";}}}







原创粉丝点击