selenium更加高效的PageObject 对象操作代码
重新封装了的selenium代码,包括click事件,sendkeys事件,select事件,以及对readonly日期控件的处理
package com.common; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select; public class PageObject {
WebDriver driver; //Start the Chrome browser
public WebDriver startChrome(String urlString){
System.out.println("start Chrome browser...");
System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
driver = new ChromeDriver();
driver.get(urlString);
System.out.println("start Chrome browser succeed...");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().window().maximize();
return driver;
} // Web-related click events
public void click(String selector,String pathValue){
switch (selector) {
case "id":
driver.findElement(By.id(pathValue)).click();
System.out.println("This test click event is used-id: "+pathValue);
break;
case "name":
driver.findElement(By.name(pathValue)).click();
System.out.println("This test click event is used-name: "+pathValue);
break;
case "xpath":
driver.findElement(By.xpath(pathValue)).click();
System.out.println("This test click event is used-xpath: "+pathValue);
break;
case "cssSelector":
driver.findElement(By.cssSelector(pathValue)).click();
System.out.println("This test click event is used-cssSelector: "+pathValue);
break;
case "className":
driver.findElement(By.className(pathValue)).click();
System.out.println("This test click event is used-className: "+pathValue);
break;
case "tagName":
driver.findElement(By.tagName(pathValue)).click();
System.out.println("This test click event is used-tagName: "+pathValue);
break;
case "linkText":
driver.findElement(By.linkText(pathValue)).click();
System.out.println("This test click event is used-linkText: "+pathValue);
break;
case "partialLinkText":
driver.findElement(By.partialLinkText(pathValue)).click();
System.out.println("This test click event is used-partialLinkText: "+pathValue);
break;
default:
System.out.println("Illegal selector: "+selector+" !!!");
break;
} } //Web-related sendKeys events
public void sendKeys(String selector,String pathValue,String sendkeys){
switch (selector) {
case "id":
driver.findElement(By.id(pathValue)).clear();
driver.findElement(By.id(pathValue)).sendKeys(sendkeys);
System.out.println("This test sendKeys event is used-id: "+pathValue);
System.out.println("By id senKeys value: "+sendkeys);
break;
case "name":
driver.findElement(By.name(pathValue)).clear();
driver.findElement(By.name(pathValue)).sendKeys(sendkeys);
System.out.println("This test sendKeys event is used-name: "+pathValue);
System.out.println("By name senKeys value: "+sendkeys);
break;
case "xpath":
driver.findElement(By.xpath(pathValue)).clear();
driver.findElement(By.xpath(pathValue)).sendKeys(sendkeys);
System.out.println("This test sendKeys event is used-xpath: "+pathValue);
System.out.println("By xpath senKeys value: "+sendkeys);
break;
case "linkText":
driver.findElement(By.linkText(pathValue)).clear();
driver.findElement(By.linkText(pathValue)).sendKeys(sendkeys);
System.out.println("This test sendKeys event is used-linkText: "+pathValue);
System.out.println("By linkText senKeys value: "+sendkeys);
break;
case "className":
driver.findElement(By.className(pathValue)).clear();
driver.findElement(By.className(pathValue)).sendKeys(sendkeys);
System.out.println("This test sendKeys event is used-className: "+pathValue);
System.out.println("By className senKeys value: "+sendkeys);
break;
case "tagName":
driver.findElement(By.tagName(pathValue)).clear();
driver.findElement(By.tagName(pathValue)).sendKeys(sendkeys);
System.out.println("This test sendKeys event is used-tagName: "+pathValue);
System.out.println("By tagName senKeys value: "+sendkeys);
break;
case "partialLinkText":
driver.findElement(By.partialLinkText(pathValue)).clear();
driver.findElement(By.partialLinkText(pathValue)).sendKeys(sendkeys);
System.out.println("This test sendKeys event is used-partialLinkText: "+pathValue);
System.out.println("By partialLinkText senKeys value: "+sendkeys);
break;
case "cssSelector":
driver.findElement(By.cssSelector(pathValue)).clear();
driver.findElement(By.cssSelector(pathValue)).sendKeys(sendkeys);
System.out.println("This test sendKeys event is used-cssSelector: "+pathValue);
System.out.println("By cssSelector senKeys value: "+sendkeys);
break;
default:
System.out.println("Illegal selector: "+selector+" !!!");
break;
}
} //Web-related select events ,selectByVisibleText() method
public void select(String selector,String pathValue,String selectValue) {
switch (selector) {
case "id":
WebElement eId = driver.findElement(By.id(pathValue));
Select selectId = new Select(eId);
selectId.selectByVisibleText(selectValue);
System.out.println("The select value is: "+selectValue);
System.out.println("This test select event is used-id: "+pathValue);
break;
case "name":
WebElement eName = driver.findElement(By.id(pathValue));
Select selectName = new Select(eName);
selectName.selectByVisibleText(selectValue);
System.out.println("The select value is: "+selectValue);
System.out.println("This test select event is used-name: "+pathValue);
break;
case "xpath":
WebElement eXpath = driver.findElement(By.xpath(pathValue));
Select selectXpath = new Select(eXpath);
selectXpath.selectByVisibleText(selectValue);
System.out.println("The select value is: "+selectValue);
System.out.println("This test select event is used-xpath: "+pathValue);
break;
case "cssSelector":
WebElement eCss = driver.findElement(By.cssSelector(pathValue));
Select selectCss = new Select(eCss);
selectCss.selectByVisibleText(selectValue);
System.out.println("The select value is: "+selectValue);
System.out.println("This test select event is used-cssSelector: "+pathValue);
break;
case "className":
WebElement eClass = driver.findElement(By.className(pathValue));
Select selectClass = new Select(eClass);
selectClass.selectByVisibleText(selectValue);
System.out.println("The select value is: "+selectValue);
System.out.println("This test select event is used-className: "+pathValue);
break;
case "tagName":
WebElement eTagName = driver.findElement(By.tagName(pathValue));
Select selectTagName = new Select(eTagName);
selectTagName.selectByVisibleText(selectValue);
System.out.println("The select value is: "+selectValue);
System.out.println("This test select event is used-tagName: "+pathValue);
break;
case "linkText":
WebElement eLinkText = driver.findElement(By.linkText(pathValue));
Select selectLinkText = new Select(eLinkText);
selectLinkText.selectByVisibleText(selectValue);
System.out.println("The select value is: "+selectValue);
System.out.println("This test select event is used-linkText: "+pathValue);
break;
case "partialLinkText":
WebElement epart = driver.findElement(By.partialLinkText(pathValue));
Select selectPart = new Select(epart);
selectPart.selectByVisibleText(selectValue);
System.out.println("The select value is: "+selectValue);
System.out.println("This test select event is used-partialLinkText: "+pathValue);
break;
default:
System.out.println("Illegal selector: "+selector+" !!!");
break;
}
} // The web-related read-only date is set by id
public void selectDateById(String idpath,String date){
JavascriptExecutor removeAttribute = (JavascriptExecutor)driver;
//remove readonly attribute
removeAttribute.executeScript("var setDate=document.getElementById(\""+idpath+"\");setDate.removeAttribute('readonly');");
WebElement setDatElement=driver.findElement(By.id(idpath));
setDatElement.clear();
setDatElement.sendKeys(date);
} // The web-related read-only date is set by name
public void selectDateByName(String namepath,String date){
JavascriptExecutor removeAttribute = (JavascriptExecutor)driver;
//remove readonly attribute
removeAttribute.executeScript("var setDate=document.getElementByName(\""+namepath+"\");setDate.removeAttribute('readonly');");
WebElement setDatElement=driver.findElement(By.name(namepath));
setDatElement.clear();
setDatElement.sendKeys(date);
} // The web-related read-only date is set by ClassName
public void selectDateByClassName(String ClassNamepath,String date){
JavascriptExecutor removeAttribute = (JavascriptExecutor)driver;
//remove readonly attribute
removeAttribute.executeScript("var setDate=document.getElementsByClassName(\""+ClassNamepath+"\");setDate.removeAttribute('readonly');");
WebElement setDatElement=driver.findElement(By.className(ClassNamepath));
setDatElement.clear();
setDatElement.sendKeys(date);
} }
对上面的封装代码进行测试的小案例
package com.common; import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod; import com.common.PageObject; public class NewTest {
PageObject PO= new PageObject();
@Test
public void f() throws Exception {
PO.sendKeys("name", "username", "admin");
PO.sendKeys("name", "password", "1234");
Thread.sleep(10000);
PO.click("className", "login-btn");
Thread.sleep(5000);
PO.click("linkText", "用户管理");
Thread.sleep(5000);
PO.click("xpath", "//*[@id='mainPage']/div[3]/div[1]/div[2]/div/a[1]");
Thread.sleep(5000);
PO.select("xpath", "//*[@id='user-add-modal']/div/div/div[2]/form/div/div[3]/div[4]/div[1]/div/div/select", "集团公司");
String chenliriqi="addregisterDate";
String setdate="2015-5-25";
String dateString = "2018-5-15";
Thread.sleep(5000);
String qixian= "addbusinssAllotedTime";
PO.selectDateById(chenliriqi,setdate);
PO.selectDateById(qixian,dateString);
PO.click("xpath","//*[@id='user-add-modal']/div/div/div[3]/button[1]"); } @BeforeMethod
public void beforeMethod() {
PO.startChrome("http://124.193.90.194:8020/login.html"); } @AfterMethod
public void afterMethod() { } }
selenium更加高效的PageObject 对象操作代码的更多相关文章
- python+selenium的WebElement对象操作
webelement对象操作 webelement对象是selenium中所有元素的父类,也就是webelement对象拥有的方法,其它元素对象都会有: 只是不同的对象在调用特定方法时,效果是不一样的 ...
- python+selenium文本框对象以及按钮对象操作
文本框对象 from selenium import webdriverfrom time import sleep driver = webdriver.Firefox() # 指定和打开浏览器ur ...
- [小北De编程手记] : Lesson 02 - Selenium For C# 之 核心对象
从这一篇开始,开始正式的介绍Selenium 以及相关的组件,本文的将讨论如下问题: Selenium基本的概念以及在企业化测试框架中的位置 Selenium核心对象(浏览器驱动) Web Drive ...
- [python爬虫] Selenium常见元素定位方法和操作的学习介绍(转载)
转载地址:[python爬虫] Selenium常见元素定位方法和操作的学习介绍 一. 定位元素方法 官网地址:http://selenium-python.readthedocs.org/locat ...
- 免费高效实用的.NET操作Excel组件NPOI(.NET组件介绍之六)
很多的软件项目几乎都包含着对文档的操作,前面已经介绍过两款操作文档的组件,现在介绍一款文档操作的组件NPOI. NPOI可以生成没有安装在您的服务器上的Microsoft Office套件的Excel ...
- 原生JS中常用的Window和DOM对象操作汇总
一.常用的Window对象操作 Window对象中又包含了document.history.location.Navigator和screen几个对象,每个对象又有自己的属性方法,这里window可以 ...
- DOM、Window对象操作
一.DOM的基本概念 DOM是文档对象模型,这种模型为树模型:文档是指标签文档:对象是指文档中每个元素:模型是指抽象化的东西. 一.基本语法: 数据类型(字符串,小数,整数,布尔,时间) var, v ...
- Scala 深入浅出实战经典 第39讲:ListBuffer、ArrayBuffer、Queue、Stack操作代码实战
王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-64讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...
- js学习笔记-编写高效、规范的js代码-Tom
编写高效.规范的js代码: 1.变量命名空间问题,尽量使用局部变量,防止命名冲突(污染作用域中的全局变量):全局空间命名的变量可以在对应的文档域任意位置中使用window调用. 2.尽量使用单var定 ...
随机推荐
- javaScript事件绑定
事件绑定,就是要对某一个东西进行操作.(因为你想让他实现什么效果,所以就得绑定他,哈哈哈!) 要想让 JavaScript 对用户的操作作出响应,首先要对 DOM 元素绑定事件处理函数.所谓事件处理函 ...
- 详解连接SQL Server数据库的方法,并使用Statement接口实现对数据库的增删改操作
总结一下,连接SQL Server数据库需要以下几个步骤: 1. 导入驱动Jar包:sqljdbc.jar 2. 加载并注册驱动程序 3. 设置连接路径 4. 加载并注册驱动 5. 连接数据库 6. ...
- HashMap的存储原理
HashMap是java中相当重要的数据结构,使用HashMap的场景非常之多,因此,了解HashMap实现的过程和原理,是非常有必要的,在一些面试中也会经常被问到.好了,我们赶紧来研究java内部是 ...
- jvm学习006 jvm内存结构分配
主要内容如下: JVM启动流程 JVM基本结构 内存模型 编译和解释运行的概念 一.JVM启动流程: JVM启动时,是由java命令/javaw命令来启动的. 二.JVM基本结构: JVM基本结构图: ...
- jvm学习002 虚拟机类加载过程以及主动引用和被动引用
虚拟机把描述类的数据从class文件加载到内存,并对数据进行校验,转换解析和初始化,最终形成可以被虚拟机直接使用的Java类型,这就是虚拟机的类加载机制. 类从被加载到虚拟机内存中开始,到卸载出内存为 ...
- 流畅python学习笔记:第十一章:抽象基类
__getitem__实现可迭代对象.要将一个对象变成一个可迭代的对象,通常都要实现__iter__.但是如果没有__iter__的话,实现了__getitem__也可以实现迭代.我们还是用第一章扑克 ...
- ArrayList和LinkedList源码
1 ArrayList 1.1 父类 java.lang.Object 继承者 java.util.AbstractCollection<E> 继承者 java.util.Abstract ...
- Longest Palindromic Substring - 字符串中最长的回文字段
需求:Given a string S, find the longest palindromic substring in S. You may assume that the maximum le ...
- Hadoop之HDFS及NameNode单点故障解决方案
Hadoop之HDFS 版权声明:本文为yunshuxueyuan原创文章.如需转载请标明出处: http://www.cnblogs.com/sxt-zkys/QQ技术交流群:299142667 H ...
- NLP —— 图模型(零):EM算法简述及简单示例(三硬币模型)
最近接触了pLSA模型,该模型需要使用期望最大化(Expectation Maximization)算法求解. 本文简述了以下内容: 为什么需要EM算法 EM算法的推导与流程 EM算法的收敛性定理 使 ...