Selenium 常用操作
表格包含不同的列
需求 读取表格的数据 对表格的没行数据进行验证 但是表格的列是不一样的
table1: colum1 colum2 colum3 colum4 colum5
table2: colum1 colum4 colum5
header设置为table 1 header 比较多的
读取table数据为List<Map<String, String>> map.get(column3) == null ? 'default vaule' : map.get(column3)
Example 2
需求1:
在 ConstantProject.java 定义常量Map 存放mapping 关系, 或者说测试数据
public static final Map<String, String> TypeMapping = ImmutableMap.<String, String>builder()
.put("001", "TCKR")
.put("002", "SH")
.put("003", "AH")
...
.build();
需求2:
通过excel 管理测试数据, 只配置name, 自动匹配的type不用配置 因为期望系统会自动匹配
// 创建list map存放已有的items
List<Map<String, String>> expeItems = UIDataReader.readUIDataGrid();
// 遍历测试数据添加item, 同时把新的item放到已有的list中
for(Map<String, String> item : itemList) {
addOneItem(item);
expeItems.put("Type", TypeMapping.get(item.get("id")));
}
// 从UI DataGrid 获取实际的items
List<Map<String, String>> actualItems = UIDataReader.readUIDataGrid();
// 比较两个list map是否相等
return expeItems.equals(actualItems);
Drag And Drop An Element
1 import java.util.concurrent.TimeUnit; 2 import org.openqa.selenium.By; 3 import org.openqa.selenium.WebDriver; 4 import org.openqa.selenium.WebElement; 5 import org.openqa.selenium.firefox.FirefoxDriver; 6 import org.openqa.selenium.interactions.Action; 7 import org.openqa.selenium.interactions.Actions; 8 import org.testng.annotations.BeforeTest; 9 import org.testng.annotations.Test; 10 11 public class DragAndDrop { 12 13 WebDriver driver = null; 14 15 @BeforeTest 16 public void setup() throws Exception { 17 driver = new FirefoxDriver(); 18 driver.manage().window().maximize(); 19 driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); 20 driver.get("http://only-testing-blog.blogspot.in/2014/09/drag-and-drop.html"); 21 } 22 23 @Test 24 public void dragAndDrop() { 25 //Locate element which you wants to drag. 26 WebElement dragElementFrom = driver.findElement(By.xpath("//div[@id='dragdiv']")); 27 //Locate element where you wants to drop. 28 WebElement dropElementTo = driver.findElement(By.xpath("//div[@id='dropdiv']")); 29 30 //Use Actions class and Its members of WebDriver API to perform drag and drop operation. 31 Actions builder = new Actions(driver); 32 Action dragAndDrop = builder.clickAndHold(dragElementFrom) 33 .moveToElement(dropElementTo) 34 .release(dropElementTo) 35 .build(); 36 dragAndDrop.perform(); 37 } 38 }
In above example, We have written total 6 lines to drag and drop an element. If you wants convert those 6 lines In only 1 line then you can do It as bellow. Replace bellow given 1 line with 6 lines of above example. It will do same thing.
1 new Actions(driver).dragAndDrop(dragElementFrom, dropElementTo).build().perform();
So this Is the way to perform drag and drop an element Using selenium WebDriver.


浙公网安备 33010602011771号