9.1封装操作表格的公用类

  目的:能够使自己编写操作表格的公用类,并基于公用类进行表格中的元素的各类操作

  被测网页的网址的HTML代码:

  

<html>
<body>
<table width="400" border="1" id="table">
<tr>
<td align="left"><p>第一行第一列</p><input type="text"></input></td>
<td align="left"><p>第一行第二列</p><input type="text"></input></td>
<td align="left"><p>第一行第三列</p><input type="text"></input></td>
</tr>
<tr>
<td align="left"><p>第二行第一列</p><input type="text"></input></td>
<td align="left"><p>第二行第二列</p><input type="text"></input></td>
<td align="left"><p>第二行第三列</p><input type="text"></input></td>
</tr>
<tr>
<td align="left"><p>第三行第一列</p><input type="text"></input></td>
<td align="left"><p>第三行第二列</p><input type="text"></input></td>
<td align="left"><p>第三行第三列</p><input type="text"></input></td>
</tr>
</table>
</body>
</html>

  Java语言版本的API实例代码 

  Table类为封装了各种表格操作方法的公用类内容如下

package cn.table;

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement; public class Table {
// 声明一个WebEelment对象,用于存储页面的表格元素对象
private WebElement _table; // 为构造函数传入页面表格元素对象参数,调用table的set_table方法,将页面表格元素复制给_table
public Table(WebElement table) {
set_table(table);
}
//获取页面表格的对象方法
public WebElement get_table() {
return _table;
} public void set_table(WebElement _table) {
this._table = _table;
} // 返回表格的行数
public int getRowCount() {
List<WebElement> tableRow = _table.findElements(By.tagName("tr"));
return tableRow.size();
}
//获取表格的列数
public int getColumnCount() {
List<WebElement> tableRows = _table.findElements(By.tagName("tr"));
return tableRows.get(0).findElements(By.tagName("td")).size();
} // 获取表格中某行某列的单元格对象
public WebElement getCell(int rowNo, int colNo) {
try {
List<WebElement> tableRows = _table.findElements(By.tagName("tr"));
System.out.println("行总数:" + tableRows.size());
System.out.println("行号:" + rowNo); List<WebElement> tableColumns = tableRows.get(rowNo - 1).findElements(By.tagName("td"));
System.out.println("列总数:" + tableColumns.size());
System.out.println("列号:" + colNo); return tableColumns.get(colNo - 1);
} catch (NoSuchElementException e) {
// TODO: handle exception
throw new NoSuchElementException("没有找到相关的元素");
}
} // 获取表格中某行某列的单元格中的某个页面元素对象,by参数用于定位某个表
public WebElement getWebElementInCell(int rowNo, int colNo, By by) {
try {
List<WebElement> tableRows = _table.findElements(By.tagName("tr"));
WebElement currentRow = tableRows.get(rowNo - 1);
List<WebElement> tablecols = currentRow.findElements(By.tagName("td"));
WebElement cell = tablecols.get(colNo - 1);
return cell.findElement(by);
} catch (NoSuchElementException e) { throw new NoSuchElementException("没有找到相关的元素");
}
}
}

测试类:调用封装的Table类进行基于表格元素的各类操作

package cn.table;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
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.testng.Assert;
import org.testng.annotations.AfterMethod; public class testTable {
WebDriver driver;
String url = "file:///E:/%E6%9D%90%E6%96%99/selenium/table.html";
@Test
public void testTableDome() {
WebElement webTable=driver.findElement(By.tagName("table"));
Table table=new Table(webTable);
WebElement cell=table.getCell(3, 2);
Assert.assertEquals(cell.getText(), "第三行第二列");
WebElement cellInut=table.getWebElementInCell(3, 2, By.tagName("input"));
cellInut.sendKeys("第三行的第二列表格被找到"); }
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "D:\\WebDriver\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.navigate().to(url);
} @AfterMethod
public void afterMethod() {
driver.quit();
} }

最新文章

  1. Pyqt 国际化多语言支持
  2. PC-PC-单片机(Arduino)通信实例
  3. C#ASP.NET 通用扩展函数之 LogicSugar 简单好用
  4. Error &amp;&amp; MFC
  5. viewmodel
  6. jQuery使用之(二)设置元素的样式
  7. Apache实现Web Server负载均衡
  8. Android中如何判断是否联网
  9. Appium 的安装启动
  10. IE浏览器上传文件时本地路径变成”C:\fakepath\”的问题
  11. 人脸对齐ASM-AAM-CLM的一些总结
  12. 【Spring】基于注解的实现SpringMVC+MySQL
  13. js添加删除元素内容
  14. 【Python3爬虫】常见反爬虫措施及解决办法(二)
  15. 用C++对C++语法格式进行分析
  16. python测试开发django-55.xadmin使用markdown文档编辑器(django-mdeditor)
  17. 【HANA系列】SAP HANA XS使用服务器JavaScript Libraries详解
  18. Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.0.2:resources 在我的myeclipse中新建maven工程总出现这个问题
  19. 【 js 基础 】【读书笔记】作用域和闭包
  20. 数据库 —— mySQL相关

热门文章

  1. Codeforces Round #519 by Botan Investments F. Make It One
  2. openstack之安全组管理
  3. MySQL处理表字段小技巧
  4. Mybatis-Plus 实战完整学习笔记(十)------条件构造器核心用法大全(下)
  5. UVa 11542 Square (高斯消元)
  6. Calendar 得到前一天当前时间
  7. DDR中常用概念
  8. [置顶] AngularJS实战之路由ui-sref-active使用
  9. 20155205 2016-2017-2 《Java程序设计》第9周学习总结
  10. winSocket编程(一)WSAStartup