一、测试登录功能实现

以慕课网的登录为例,分析登录的功能需求,编写测试用例,找到要定位的元素以及需要的操作,编写登录功能的测试代码。代码实现如下:

 public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.gecko.driver","c:\\Program Files\\Mozilla Firefox\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.get("https://www.imooc.com");
driver.manage().window().setSize(new Dimension(1456,876));//设置窗口的尺寸
driver.findElement(By.id("js-signin-btn")).click();//点击登录按钮
Thread.sleep(2000); driver.findElement(By.name("email")).sendKeys("*********");
driver.findElement(By.name("password")).sendKeys("******"); driver.findElement(By.cssSelector(".moco-btn")).click();
Thread.sleep(2000); Actions a=new Actions(driver);//鼠标移动,perform()执行所有Actions中存储的行为,可以理解成对整个操作事件的提交动作。
a.moveToElement(driver.findElement(By.cssSelector("#header-avator>img"))).perform(); String s=driver.findElement(By.className("name")).getText();//获取标签属性
String t=driver.getTitle();
String v=driver.getCurrentUrl();
System.out.println(s+t+v);
assertEquals(s,"搁浅的青木"); //验证失败时该测试将停止。vertify:失败时,该测试将继续执行,并将错误记入日显示屏。Waitfor用于等待某些条件变为真。可用于AJAX应用程序的测试。
if(s.equals("搁浅的青木")) {
System.out.println("登陆成功,请退出");
driver.findElement(By.linkText("安全退出")).click();
}else {
System.out.println("登录失败");
}
driver.quit();
}

二、代码重构之封装findElement()方法

1.提取参数,包括:输入信息、定位属性、定位属性值

2.封装findElement()方法

 public class moocloginOne {
public WebDriver driver;
public void initDriver() throws InterruptedException {
System.setProperty("webdriver.gecko.driver","c:\\Program Files\\Mozilla Firefox\\geckodriver.exe");
driver=new FirefoxDriver();
driver.get("https://www.imooc.com");
driver.manage().window().setSize(new Dimension(1456,876));//设置窗口的尺寸
Thread.sleep(3000); driver.findElement(By.id("js-signin-btn")).click();
} public void loginScript() throws InterruptedException {
this.initDriver();
Thread.sleep(3000);
//1.提取定位参数,提高代码可用性
String userBy="name";
String userLocal="email";
String passowrdBy="name";
String PasswordLocal="password";
String loginbtnBy="cssSelector";
String loginbtnLocal=".moco-btn";
String userInfo="17839196010";
String passwordInfo="duhui619";
String userImgBy="cssSelector";
String userImgLocal="#header-avator>img";
String usernameBy="className";
String usernameLocal="name";
String usernameInfo="搁浅的青木";
WebElement user=element(userBy,userLocal);
user.isDisplayed();
WebElement password=element(passowrdBy,PasswordLocal);
password.isDisplayed();
WebElement button=element(loginbtnBy,loginbtnLocal);
button.isDisplayed();
user.sendKeys(userInfo);
password.sendKeys(passwordInfo);
button.click();
Thread.sleep(2000);
WebElement header=element(userImgBy,userImgLocal);
header.isDisplayed();
Actions a=new Actions(driver);
a.moveToElement(header).perform();
String s=element(usernameBy,usernameLocal).getText();
if(s.equals(usernameInfo)) {
System.out.println("登陆成功,请退出");
}else {
System.out.println("登录失败");
}
driver.quit();
} //2.封装driver.findElement( By.id(local))
public WebElement element(String by,String local) {
WebElement element=null;
if(by.equals("id")) {
element=driver.findElement( By.id(local));
}else if(by.equals("name")) {
element=driver.findElement( By.name(local));
}else if(by.equals("className")) {
element=driver.findElement( By.className(local));
}else if(by.equals("linkText")) {
element=driver.findElement( By.linkText(local));
}else if(by.equals("xpath")) {
element=driver.findElement( By.xpath(local));
}else if(by.equals("cssSelector")) {
element=driver.findElement( By.cssSelector(local));
}else if(by.equals("tageName")){
element=driver.findElement( By.tagName(local));
}else {
element=driver.findElement( By.partialLinkText(local));
}
return element;
} public static void main(String[] args) throws InterruptedException {
moocloginOne login=new moocloginOne();
Thread.sleep(2000);
login.loginScript();
}

三、代码重构之封装读取配置文件的方法

1.拆分findElement方法为by方法和element方法

2.修改byStr函数中的代码,只返回by的方式 。
   3.将变量放到配置文件中,并编写读取文件类及方法-----ProUtil类。
   4.编写读取配置文件函数-----调用ProUtil类,读取配置文件。
   5.在byStr函数中调用读取文件函数--getproperties(),并修改byStr传入参数。

主函数:

 public class moocloginTwo {
public WebDriver driver;
public void initDriver() throws InterruptedException {
System.setProperty("webdriver.gecko.driver","c:\\Program Files\\Mozilla Firefox\\geckodriver.exe");
driver=new FirefoxDriver();
driver.get(this.getproperties("Url"));
driver.manage().window().setSize(new Dimension(1456,876));//设置窗口的尺寸
Thread.sleep(3000);
element(this.byStr("closeLocal")).click();
element(this.byStr("loginLocal")).click();
} public void loginScript() throws InterruptedException {
this.initDriver();
Thread.sleep(3000);
//3.将变量放到配置文件中,并编写读取文件类及方法-----ProUtil类
WebElement user=this.element(this.byStr("userLocal"));
user.isDisplayed(); WebElement password=this.element(this.byStr("PasswordLocal"));
password.isDisplayed(); WebElement button=this.element(this.byStr("loginbtnLocal"));
button.isDisplayed();
user.sendKeys(this.getproperties("userInfo"));
password.sendKeys(this.getproperties("passwordInfo"));
button.click();
Thread.sleep(3000); WebElement header=this.element(this.byStr("userImgLocal"));
header.isDisplayed();
Actions a=new Actions(driver);
a.moveToElement(header).perform();
Thread.sleep(3000); String s=this.element(this.byStr("usernameLocal")).getText();//获取标签属性
System.out.println(s); if(s.equals(this.getproperties("usernameInfo"))) {
System.out.println("登陆成功,请退出");
this.element(this.byStr("logOut")).click();
}else {
System.out.println("登录失败");
}
//driver.quit();
} public By byStr(String local) {
//5.在byStr函数中调用读取文件函数--getproperties(),并修改byStr传入参数
String locator=this.getproperties(local);
String locatorType=locator.split(">")[0];
String locatorValue=locator.split(">")[1];
//2.修改byStr函数中的代码,只返回by的方式
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("linkText")) {
return By.linkText(locatorValue);
}else if(locatorType.equals("xpath")) {
return By.xpath(locatorValue);
}else if(locatorType.equals("cssSelector")) {
return By.cssSelector(locatorValue);
}else if(locatorType.equals("tageName")){
return By.tagName(locatorValue);
}else {
return By.partialLinkText(locatorValue);
} }
//1.封装 driver.findElenment(),编写element函数————将byStr函数中的 driver.findElenment()与by.方式()拆开
public WebElement element(By location) {
return driver.findElement(location);
}
//4.编写读取配置文件函数-----调用ProUtil类,读取配置文件
public String getproperties(String local) {
ProUtil properties=new ProUtil("E:\\eclipse\\workspace\\mypro\\src\\mooc\\day01\\element.properties");
String locator=properties.getPro(local);
return locator;
}
public static void main(String[] args) throws InterruptedException { moocloginTwo login=new moocloginTwo();
login.loginScript();
}
}

读取配置文件函数:

 public class ProUtil {
private Properties properties;
private String filePath;
public ProUtil(String filePath) {
this.filePath=filePath;
this.properties=readProperties();
} public Properties readProperties() {
Properties pro=new Properties();
try {
InputStreamReader inputstream=new InputStreamReader(new FileInputStream(filePath),"UTF-8" );//考虑到编码格式
BufferedReader in=new BufferedReader(inputstream);
pro.load(in);
} catch (IOException e) {
e.printStackTrace();
}
return pro; } public String getPro(String key) { if(properties.containsKey(key)) {
String value=properties.getProperty(key);
return value;
}else {
System.out.println("获取的键值不对");
return " ";
} }
}

配置文件:

 #-----------登录页面元素部分:----------
#登录网址
Url=https://www.imooc.com
#登录连接的位置
loginLinkLocal=id>js-signin-btn
#输入登录账号
userLocal=name>email
#输入登录密码
PasswordLocal=name>password
#点击登录按钮
loginbtnLocal=cssSelector>.moco-btn
#个人头像
userImgLocal=cssSelector>#header-avator
#个人用户名
usernameLocal=className>name
#安全退出
logOut=linkText>安全退出
#--------登录参数部分:--------
#登录账号
userInfo=*********
#登录密码
passwordInfo=*******
#用于比对的用户名
usernameInfo=搁浅的青木

最新文章

  1. 20.(转)Android的样式(Style)和主题(Theme)
  2. 页面静态化2 --- 使用PHP缓存机制来完成页面静态化(上)(ob_flush和flush函数区别用法)
  3. Mix and Build(简单DP)
  4. java.util.Date转java.sql.Date丢失时间问题
  5. CCapture directshow 视频捕获类
  6. java.lang.String内部结构的变化
  7. Boa服务器在ARM+Linux上的移植
  8. docker-compose 工具安装
  9. MFC渐入渐出框实现方式二
  10. UISwitch 开关控件
  11. nodejs笔记1 ----关于express不是本地命令
  12. Ubuntu 18.04 安装MySQL
  13. Spark SQL官网阅读笔记
  14. testng+maven一些坑
  15. [UWP/WPF]在应用开发中安全使用文件资源
  16. 封了1000多个IP地址段,服务器现在坚如磐石,对付几个小毛贼还是很轻松的
  17. Java基础——ArrayList与LinkedList(一)
  18. MySQL纯透明的分库分表技术还没有
  19. 【python】实例-答题系统
  20. 【354】Numpy 相关函数应用

热门文章

  1. hdu2093 考试排名(还需完善)
  2. Jmeter基础-HTTP请求
  3. python+selenium 自动化测试框架-学习记录
  4. Java 泛型与集合
  5. indetityserver4-implicit-grant-types-请求流程叙述-下篇
  6. 微信小程序开发 -- 通过云函数下载任意文件
  7. 二刷Redux笔记
  8. [Python基础]005.语法(4)
  9. [批处理教程之Shell]002.Linux 常用命令大全
  10. sku算法介绍及实现