02 脚本重构by封装
02 脚本重构by封装

  1. 定位元素和值分开定义
    02 脚本重构by封装
  2. 封装By方法
    02 脚本重构by封装
    则之前的定位写法:
    02 脚本重构by封装
    可更改为:
    02 脚本重构by封装
    ok,整个禅道代码更改如下:
    package com.aliyun.chandao;

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;
import org.openqa.selenium.interactions.Actions;

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("XXX/zentao");
}

/*
 * 登录脚本
 */
public void loginScript() throws InterruptedException {
	this.InitDriver();
	String username = "XXX";
	String userElement = "account";
	String userBy = "id";
	String userPwd = "XXX";
	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));
user.isDisplayed();
WebElement password = driver.findElement(this.byStr(pwdBy, pwdElement));
password.isDisplayed();
WebElement loginButton = driver.findElement(this.byStr(loginButtonBy, loginButtonElement));
loginButton.isDisplayed();
user.sendKeys(username);
password.sendKeys(userPwd);
loginButton.click();
Thread.sleep(1000);
// 获取左上角登录用户名的文本并却所有空格,包括首尾、中间
String userInfo = driver.findElement(By.id(“myname”)).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 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 {
		return By.xpath(local);
	}
}

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

}

相关文章:

  • 2022-12-23
  • 2021-12-18
  • 2021-05-30
  • 2021-11-28
  • 2021-05-30
  • 2021-11-02
  • 2022-12-23
  • 2021-06-28
猜你喜欢
  • 2021-12-15
  • 2022-01-09
  • 2022-02-05
  • 2022-12-23
  • 2022-12-23
  • 2021-09-22
  • 2021-12-11
相关资源
相似解决方案