zoukankan      html  css  js  c++  java
  • 读取配置文件-重构封装

    项目结构图


    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>email
    userpass=name>password
    loginbutton=className>btn-red
    header=id>header-avator
    nameInfo=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 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 "";
    		}
    	}
    	
    
    }







  • 相关阅读:
    通过AIR Native Extension在AIR应用中加入iAd广告(一) —— Flash Builder篇
    使用MonoTouch.SQLite简化用户界面开发
    常见的几种分支开发方式
    给对象增加一个简单的自定义事件机制
    WCF 4.5:配置文件更小,对ASP.NET的支持更好
    MongoDB中的Group By
    SQL Server 2012大幅增强了TSQL
    编程珠玑:对DAO层的一点修改
    《The Elements of User Experience》读书笔记
    ORM工具LLBLGen Pro 3.5发布
  • 原文地址:https://www.cnblogs.com/igubai/p/7426146.html
Copyright © 2011-2022 走看看