基础示例代码:

/**
* @author Richered
**/
package com.sample; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver; public class TestBaidu {
public static WebDriver driver = null; public static void main(String[] args) throws InterruptedException{
System.setProperty("webdriver.chrome.driver", "tools/chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.baidu.com");
Thread.sleep(2000);
driver.findElement(By.xpath("//*[@id=\"kw\"]")).sendKeys("桃李不言、下自成蹊 博客园");
Thread.sleep(5000);
// driver.findElement(By.xpath("//*[@id=\"su\"]")).click();
driver.close();
}
}  

本例基本上和官方示例代码一般无二,首先是获取浏览器driver,打开浏览器,将浏览器最大化,进入baidu,等待2s,找到百度输入框并传值"桃李不言、下自成蹊 博客园",等待5s,关闭浏览器。

代码都是被写死的,例如这块不想使用google浏览器,想使用firefox浏览器或者IE浏览器呢?不想将浏览器窗口最大化呢?想进入www.bing.com呢?不想使用xpath的定位方式呢?

问题抛出来了,这样写代码的方式也着实僵硬,那么如何解决呢?--封装关键字、动作

封装

  封装,即隐藏对象的属性和实现细节,仅对外公开接口,控制在程序中属性的读和修改的访问级别;将抽象得到的数据和行为(或功能)相结合,形成一个有机的整体,也就是

将数据与操作数据的源代码进行有机的结合,形成“类”,其中数据和函数都是类的成员。(copy百度百科)

  其实讲的通俗一点就是:封装提高了代码的重用性,不用关心具体的实现,当然,封装也是面向对象的三大特征之一。

  看看封装上方的代码是如何实现:

/**
* @author Richered
**/
package com.sample; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver; public class KeyWord {
public WebDriver driver;
public KeyWord() { }
//选择浏览器
public void openBrowser(String browserType) {
try {
switch (browserType) {
case "chrome":
GoogleDriver chrome = new GoogleDriver("tools/chromedriver.exe");
driver = chrome.getdriver();
break;
case "ff":
FFDriver firefox = new FFDriver("F:\\Firefox\\firefox.exe", "tools/geckodriver.exe");
driver = firefox.getdriver();
break;
default:
GoogleDriver c = new GoogleDriver("tools/chromedriver.exe");
driver = c.getdriver();
break;
}
} catch (Exception e) {
// TODO: handle exception
}
} //关闭浏览器
public void closeBrowser() {
try {
driver.quit();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("关闭浏览器出错!");
}
}
//浏览器大小显示
public void setWindow(){
driver.manage().window().maximize();
} //访问网站
public void getURL(String url) {
try {
driver.get(url);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//封装name定位元素并传值
public void inputByNameAndSubmit(String name,String content) {
try {
driver.findElement(By.name(name)).sendKeys(content);
driver.findElement(By.name(name)).submit();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//封装id定位元素并传值
public void inputById(String id, String content) {
try {
driver.findElement(By.id(id)).sendKeys(content);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//用xpath定位元素并传值
public void inputByxpath(String xpath, String content) {
try {
driver.findElement(By.xpath(xpath)).sendKeys(content);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/***********************************************************/
//用id定位元素并点击
public void clickByid(String id) {
try {
driver.findElement(By.id(id)).click();
} catch (Exception e) {
// TODO: handle exception
}
}
//用name定位元素并点击
public void clickByname(String name) {
try {
driver.findElement(By.name(name)).click();
} catch (Exception e) {
// TODO: handle exception
}
}
//用xpath定位元素并点击
public void clickByxpath(String xpath) {
try {
driver.findElement(By.xpath(xpath)).click();
} catch (Exception e) {
// TODO: handle exception
}
}
/***********************************************************/
//选择点击方法
public void clickSwitch(String method, String phase) {
try {
switch (method) {
case "id":
driver.findElement(By.id(phase)).click();
break;
case "classname":
driver.findElement(By.className(phase)).click();
break;
case "name":
driver.findElement(By.name(phase)).click();
break;
case "tagname":
driver.findElement(By.tagName(phase)).click();
break;
case "linktext":
driver.findElement(By.linkText(phase)).click();
break;
case "partialLinktext":
driver.findElement(By.partialLinkText(phase)).click();
break;
case "xpath":
driver.findElement(By.xpath(phase)).click();
break;
case "css":
driver.findElement(By.cssSelector(phase)).click();
break;
default:
driver.findElement(By.xpath(phase)).click();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//封装强制等待
public void halt(String time) {
int t = 1000;
try {
t = Integer.parseInt(time);
Thread.sleep(t);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

注释里边写的很清楚了,此处封装了选择浏览器,关闭浏览器,访问URL,name、id、xpath的定位元素(传值)方式【当然可以增加其他的定位方式,下同】,name、id、xpath的定位元素并点击,选择点击方法,等待。

同样是上方示例代码的目的,再看实现方式:

/**
* @author Richered
**/
package com.sample; public class TestBaidu {
public static void main(String[] args) { KeyWord sm = new KeyWord();
sm.openBrowser("chrome");
sm.setWindow();
sm.getURL("https://www.baidu.com");
sm.halt("2000");
sm.inputByxpath("//*[@id=\"kw\"]", "桃李不言、下自成蹊 博客园");
sm.halt("5000");
sm.closeBrowser();
}
}

嗯哼,是不是代码量简洁了许多呢?选择哪个浏览器可以灵活选择;定位元素的方式被单独伶了出来,传值即可;动作也被伶了出来,当然等待方式等等关键字、动作都可以进行封装。

后续~~~~~

 

 

UI“三重天”之selenium--封装(二)的更多相关文章

  1. UI“三重天”之Selenium(一)

    关注一下UI自动化,记一记笔记. UI自动化的优缺点: 关于UI自动化的优缺点想来大家都有了解,优点:解放人力(并不是完全解放),用机器(涵盖工具.脚本等)代替人工完成测试工作,将测试用例转化为脚本实 ...

  2. python+selenium封装UI自动化框架

    seleinum框架 框架的思想:  解决我们测试过程中的问题:大量的重复步骤,用自动化来实现    1)配置和程序的分离    2)测试数据和程序的分离    3)不懂编程的人员可以方便使用:使用的 ...

  3. python+selenium十:基于原生selenium的二次封装

    from selenium import webdriverfrom selenium.webdriver.support.wait import WebDriverWaitfrom selenium ...

  4. python+selenium十:selenium的二次封装

    python+selenium十:基于原生selenium的二次封装   from selenium import webdriverfrom selenium.webdriver.support.w ...

  5. iOS开发UI篇—核心动画(UIView封装动画)

    iOS开发UI篇—核心动画(UIView封装动画) 一.UIView动画(首尾) 1.简单说明 UIKit直接将动画集成到UIView类中,当内部的一些属性发生改变时,UIView将为这些改变提供动画 ...

  6. selenium 封装

    周末无聊 在家封装一个pyselenium.可能这些封装大家都会使用,但是我还是根据我自己的习惯去选择性的去封装一些在我工作中用的,这样的话,我就不用去看selenium的api的,我可以根据我自己的 ...

  7. 任务四十一:UI组件之日历组件(二)

    任务四十一:UI组件之日历组件(二) 面向人群: 有一定基础的同学 难度: 中 重要说明 百度前端技术学院的课程任务是由百度前端工程师专为对前端不同掌握程度的同学设计.我们尽力保证课程内容的质量以及学 ...

  8. 深入理解基于selenium的二次开发

    对于做web端自动化测试的人来说,可能接触selenium比QTP还要多,但是我们在做基于selenium的二次开发的时候,经常会说到二次开发是为了易于维护,很多人可能不懂得维护的价值是什么,和到底要 ...

  9. AngularJs的UI组件ui-Bootstrap分享(十二)——Rating

    Rating是一个用于打分或排名的控件.看一个最简单的例子: <!DOCTYPE html> <html ng-app="ui.bootstrap.demo" x ...

  10. 三重for循环实现对二维数组的按列排序(JavaScript)

    由C语言联想到的:三重for循环实现对二维数组的按列排序,并且牵扯到数据结构. 自己写的,水平有限,本文属于原创,可能存在错误,忘指正~ function circle() { var a = [ [ ...

随机推荐

  1. Python学习札记(三十四) 面向对象编程 Object Oriented Program 5

    参考:获取对象信息 NOTE 1.type()函数可以用来判断对象的类型: >>> type(123) <class 'int'> >>> type(' ...

  2. c++ 判断list是否为空(empty)

    #include <list> #include <iostream> using namespace std; int main() { list<int> nu ...

  3. java线程中的interrupt,isInterrupt,interrupted方法

    在java的线程Thread类中有三个方法,比较容易混淆,在这里解释一下 (1)interrupt:置线程的中断状态 (2)isInterrupt:线程是否中断 (3)interrupted:返回线程 ...

  4. angular 之路由

    1.用angular-cli建一个工程自带路由怎么做? 命令:ng new  项目名 --routing 2.怎么使用路由器和路由器的一些基本使用. //html页面 <a routerLink ...

  5. Bitwise Equations

    Problem Description You are given two positive integers X and K. Return the K-th smallest positive i ...

  6. 331. Verify Preorder Serialization of a Binary Tree -- 判断是否为合法的先序序列

    One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, ...

  7. PHP:第五章——字符串的分割与替换

    <?php header("Content-Type:text/html;charset=utf-8"); //字符串的截取与分割 //1.字符串截取类函数 //1)trim ...

  8. Can't create session svn: Unable to connect to a repository at URL “...”的解决方案

    Can't create sessionsvn: Unable to connect to a repository at URL '...' Cannot negotiate authenticat ...

  9. activity+fragment+listview+adapter+bean在同一个类中的套路

    1.xml activity_main.xml <?xml version="1.0" encoding="utf-8"?><FrameLay ...

  10. 9.2 Zynq嵌入式系统调试方法

    陆佳华书<嵌入式系统软硬件协同设计实战指南 第2版>这本书中的实例着实浪费了我不少时间.从本书第一个实例我就碰了一鼻子灰.当然显然是自己时新手的原因.首先第一个实验其实真的特别简单,为什么 ...