读取配置文件

项目结构图


代码:

1、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() throws Exception {
		this.InitDriver();
		String username = "18513199586";
		String userpass = "111111";
		String passBy = "name";
		String passwordElement = "password";
		String buttonElement = "btn-red";
		String buttonBy = "className";
		String headerElement = "header-avator";
		String nameElement = "name";
		String headerBy = "id"; 
		String nameinfo = "className";
		
		ProUtil properties = new ProUtil("element.properties");
		String locator = properties.getPro("username");
		String locatorType =locator.split(">")[0];
		String locatorValue = locator.split(">")[1];
		
		Thread.sleep(3000);
		WebElement user = this.elemnet(this.byStr(locatorType, locatorValue));
		//this.elemnet(this.byStr(userBy, emailElement));
		user.isDisplayed();
		WebElement password = this.elemnet(this.byStr(passBy,passwordElement));
		password.isDisplayed();
		WebElement loginButton = this.elemnet(this.byStr(buttonBy,buttonElement));
		loginButton.isDisplayed();
		user.sendKeys(username);
		password.sendKeys(userpass);
		loginButton.click();
		Thread.sleep(3000);
		WebElement header = this.elemnet(this.byStr(headerBy, headerElement));
		header.isDisplayed();
		Actions actions = new Actions(driver);
		actions.moveToElement(header).perform();;
		String userInfo = this.elemnet(this.byStr(nameinfo, nameElement)).getText();
		System.out.println(userInfo);
		if(userInfo.equals("mushishi_xu1")) {
			System.out.print("登陆成功");
		}else {
			System.out.print("登录失败");
		}
	}
    /**封装By by **/
    public By byStr(String by, String local){
    	if(by.equals("id")) {
    		return By.id(local);
    	}else if(by.equals("name")){
    		return By.name(local);
    		}else if(by.equals("className")){
    			return By.className(local);
    		}else if(by.equals("xpath")){
    			return By.className(local);
    		}else{
    			return By.xpath(local);
    		}
    }
   /**
    * 封装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();
}
}
2、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 block
			e.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 "";
		}
	}
	

}
3、element.properties

username=name>email






posted @ 2017-08-15 00:16  谷白  阅读(177)  评论(0编辑  收藏  举报