Some tips about Selenium UI testing

1. 怎樣驗證「no scroll bar exist」 或者 「 no view all button exist」 ?css

SeleniumUtil.runJs(driver, "var obj = arguments[0]; obj.scrollTop = 1", new Object[] {rightTable});this

Assert.assertTrue((long)(SeleniumUtil.runJs(driver, "var obj = arguments[0]; return obj.scrollTop", new Object[] {rightTable})) > 0, "There're not any scroll bar");orm

Method 1 : ip

Assert.assertTrue(!SeleniumUtil.isElementPresent(driver, By.cssSelector("button.action-done")), "errormessage");element

Method 2 : get

try {input

List<WebElement> searchResultList = screenerPage.getSearchResultRowsInAdvancedSearch();it

Assert.fail("the above element should not be visible");io

}catch (Exception ex) {form

//Go to next step

}

2. 鍵盤操做怎麼automated, 例如ESC鍵, 向上向下鍵 ?

    Actions action = new Actions(driver);

    action.keyDown(Keys.valueOf("p")).keyUp(Keys.valueOf("p")).perform();

3. 怎樣定義使用一個List類的 MAP ?

    Map<String, List<WebElement>> map = new HashMap<String, List<WebElement>>();

    map.put("firstname", list);

4. 怎樣獲取hidden element的Text?

    Cannot get text of hidden element by element.gettext(), but this would works well:

   WebElement element = driver.findElement(By.cssSelector("input #username"));

   String actualDataPointName = (String) ((JavascriptExecutor) driver).executeScript(

                    "return jQuery(arguments[0]).text();", element);

5. How to click element under one hidden element ?

  • In order to click the element ,change it to visible firstly.When we operate manually,actually,we first move the mouse to the row ,and then click the element.
  • So, when automate the click action,we also need to move the mouse to the row firstly,then the element will be visible,this is the trick.
  • Hover to the row of DDL, move mouse to the front of the row, then click the DDL would work.

    WebElement theRow = this.findBlankBenchmarkRow();

      Assert.assertNotSame(theRow, null, "Should find out a blank benchmark row.");

      Actions action = new Actions(driver);

      System.out.println("Row size : "+theRow.getSize());

      System.out.println(("X offset : "+(theRow.getSize().width)/100));

      System.out.println(("Y offset : "+(theRow.getSize().height)/2));

      int locx=0;

      int locy=0;

      locx=(theRow.getSize().width)/100;

      locy=(theRow.getSize().height)/2;

      action.moveToElement(theRow, locx, locy).perform();

      SeleniumUtil.sleep(2);

      WebElement theDDL = theRow.findElement(By.cssSelector("td div.dash-bmk-ddl"));

      return theDDL;

相關文章
相關標籤/搜索