您的位置:軟件測試 > 開源軟件測試 > 開源功能測試工具 > Selenium
Selenium使用要點(diǎn)記錄
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2016/6/12 14:43:54 ] 推薦標(biāo)簽:功能測試工具 Selenium

  1.selenium處理下拉列表的各種方法
  a.通過option元素里面的value屬性值選中
  public void selectOptionByValue(WebElement select, String value, WebDriver driver) {
  //select = driver.findElement(By.id("id of select element")):
  List<WebElement> allOptions = select.findElements(By
  .tagName("option"));
  for (WebElement option : allOptions) {
  if (value.equals(option.getAttribute("value"))) {
  option.click();
  break;
  }
  }
  }
  b.通過option元素顯示值選中
  public void selectOptionByVisibleText(String elementId, String visibleText){
  WebElement ele = driver.findElement(By.id(elementId));
  Select select = new Select(ele);
  select.selectByVisibleText(visibleText);
  }
  c.通過opton在select中的index(從0開始)選中
  public void selectOptionByIndex(By by, String index){
  try{
  int ind = Integer.parseInt(index);
  WebElement ele = driver.findElement(by);
  this.selectOptionByIndex(ele, ind);
  }catch(Exception e){
  loggerContxt.error(String.format("Please configure a numeric as the index of the optioin for %s..",by.toString()));
  return;
  }
  }
  d.來個(gè)下拉列表選中多個(gè)選項(xiàng)的情況
  /**
  * @elementId id of the select element
  * @param periodArr is the array of the indexes of the options in the dropdown list.
  */
  public void selectMultipleOptionsInDropdownList(String elementId, String[] periodArr){
  //大家自行傳入這個(gè)driver對象
  Actions actions = new Actions(this.driver);
  //我這里單個(gè)的option選中是用的option的index通過xpath的方式定位的,大家可以嘗試上邊其他的方式
  for (int i = 0; i < periodArr.length; i++) {
  try{
  int num = Integer.parseInt(periodArr[i]);
  WebElement ele = driver.findElement(
  By.xpath(String.format(".//select[@id='%s']/option[%d]",elementId,num)));
  actions.moveToElement(ele);
  if (!ele.isSelected()) {
  actions.click();
  }
  }catch(Exception e){
  loggerContxt.info(String.format("Failed to parse the radia count::%s for Quater peroid.",periodArr[i]));
  continue;
  }
  }
  actions.perform();
  暫時(shí)列這么些關(guān)于下拉列表的處理吧,這個(gè)挺好google,實(shí)在想偷懶的可以給我留言。
  2.元素拖拽,感覺在自動化測試?yán)镞呥@種需求比較少,但是我還是碰到了的哈
  WebElement element1 = driver.findElement(By.id("element1"));
  WebElement element2 = driver.findElement(By.id("element2"));
  Actions actions = new Actions(driver);
  //選中需要拖動的元素,并且往x,y方向拖動一個(gè)像素的距離,這樣元素被鼠標(biāo)拉出來了,并且hold住
  actions.clickAndHold(element1).moveByOffset(1, 1);
  //把選中的元素拉倒目的元素上方,并且釋放鼠標(biāo)左鍵讓需拖動元素釋放下去
  actions.moveToElement(element2).release();
  //組織完這些一系列的步驟,然后開始真實(shí)執(zhí)行操作
  Action action = actions.build();
  action.perform();
  其實(shí)Actions里邊有public Actions dragAndDrop(WebElement source, WebElement target)這個(gè)方法能直接去拖拽元素,可能我的界面有點(diǎn)不太規(guī)范,用官方提供的這個(gè)一直不成功,所以我在這一系列的子操作之間加了一個(gè)小的步驟:選中source element之后往x,y放下拖動一個(gè)像素。
  大家稍微去看看Actions類里邊還能發(fā)現(xiàn)很多關(guān)于元素操作的方法,希望你可能在里邊能找到解決你需求的方法。

軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd