06 登陆账号参数化代码实战

06 登陆账号参数化代码实战_第1张图片
整个代码如下:
package com.aliyun.chandao;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

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.chrome.ChromeOptions;

public class LoginV2 {
public WebDriver driver;

public void InitDriver() {
	// 指定chrome driver的获取地址
	System.setProperty("webdriver.chrome.driver", "D:\\BrowserDriver\\chromedriver.exe");

	// 去掉chrome正受到自动测试软件控制信息栏显示
	ChromeOptions options = new ChromeOptions();
	options.addArguments("disable-infobars");

	// 实例化webdriver的对象,启动谷歌浏览器
	driver = new ChromeDriver(options);
	driver.manage().window().maximize();

	// 打开阿里云禅道地址
	driver.get("http://XXX/zentao");
}

/*
 * 登录脚本
 */
public void loginScript(String username,String userElement) throws InterruptedException {
	this.InitDriver();
	String userBy = "id";
	String userPwd = XXXX";
	String pwdElement = "password";
	String pwdBy = "name";
	String loginButtonElement = "submit";
	String loginButtonBy = "id";

// WebElement user = driver.findElement(By.id(“account”));
// WebElement user = driver.findElement(this.byStr(userBy, userElement));
// WebElement user = this.element(this.byStr(userBy, userElement));
WebElement user = this.element(this.byStr(“username”));
user.isDisplayed();
WebElement password = this.element(this.byStr(“password”));
password.isDisplayed();
WebElement loginButton = this.element(this.byStr(“loginButton”));
loginButton.isDisplayed();
user.sendKeys(username);
password.sendKeys(userPwd);
loginButton.click();
Thread.sleep(1000);
// 获取左上角登录用户名的文本并却所有空格,包括首尾、中间
String userInfoElement = “myname”;
String userInfoBy = “id”;
String userInfo = this.element(this.byStr(“userInfo”)).getText().replace(" “, “”);;
System.out.println(”>>>" + userInfo);
if(userInfo.equals(“刘斌宇”)){
System.out.println(userInfo + " 登陆成功");
}else{
System.out.println(userInfo + " 登陆失败");

	}
	driver.close();
}

/*
 * 封装By
 */
public By byStr(String localMethod) {
	//实例化 ProUtil类
	ProUtil properties = new ProUtil("element.properties");
	//获取用户名输入框的定位方式
	String locator = properties.getPro(localMethod);
	String localtorType = locator.split(">")[0];
	String localtorValue = locator.split(">")[1];
	if(localtorType.equals("id")) {
		return By.id(localtorValue);
	}else if(localtorType.equals("name")) {
		return By.name(localtorValue);
	}else if(localtorType.equals("className")) {
		return By.className(localtorValue);
	}else {
		return By.xpath(localtorValue);
	}
}

/*
 * 封装Element
 */
public WebElement element(By by) {
	WebElement ele = driver.findElement(by);
	return ele;
}

public static void main(String[] args) throws InterruptedException {
	LoginV2 login = new LoginV2();

// login.loginScript(“XXX”,“123456”);
/*
* key-value
* username-password
*/
HashMap user = new HashMap();
user.put(“XXX”, “123456”);
user.put(“XXX”, “123456”);
user.put(“XXX”, “123456”);
Iterator us = user.entrySet().iterator();
while(us.hasNext()) {
Map.Entry entry = (Map.Entry)us.next();
String username = entry.getKey().toString();
String password = entry.getValue().toString();
login.loginScript(username,password);

	}
}

}

你可能感兴趣的:(UI自动化高阶用法,--领略编程之美)