Some tips about Selenium UI testing
2017-02-24 00:00
525 查看
摘要: 用Selenium做UI automation testing时候的一些小经验
1. 怎样验证“no scroll bar exist” 或者 “ no view all button exist” ?
SeleniumUtil.runJs(driver, "var obj = arguments[0]; obj.scrollTop = 1", new Object[] {rightTable});
Assert.assertTrue((long)(SeleniumUtil.runJs(driver, "var obj = arguments[0]; return obj.scrollTop", new Object[] {rightTable})) > 0, "There're not any scroll bar");
Method 1 :
Assert.assertTrue(!SeleniumUtil.isElementPresent(driver, By.cssSelector("button.action-done")), "errormessage");
Method 2 :
try {
List<WebElement> searchResultList = screenerPage.getSearchResultRowsInAdvancedSearch();
Assert.fail("the above element should not be visible");
}catch (Exception ex) {
//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;
1. 怎样验证“no scroll bar exist” 或者 “ no view all button exist” ?
SeleniumUtil.runJs(driver, "var obj = arguments[0]; obj.scrollTop = 1", new Object[] {rightTable});
Assert.assertTrue((long)(SeleniumUtil.runJs(driver, "var obj = arguments[0]; return obj.scrollTop", new Object[] {rightTable})) > 0, "There're not any scroll bar");
Method 1 :
Assert.assertTrue(!SeleniumUtil.isElementPresent(driver, By.cssSelector("button.action-done")), "errormessage");
Method 2 :
try {
List<WebElement> searchResultList = screenerPage.getSearchResultRowsInAdvancedSearch();
Assert.fail("the above element should not be visible");
}catch (Exception ex) {
//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;
相关文章推荐
- Some Tips and Tricks about Qt
- Some tips about Control Flow in Python
- some tips about web hacking
- Tips: Save some typing when binding values to UI in WPF/Silverlight
- Some Tips About Layout Resource
- Some tips about python
- some tips about python
- some tips about python default value....
- About GNIT some tips
- some tips about vector in C++ STL
- Some Tips about Interbase
- Some notes about CRM Roles Concept in Web UI
- some tips about python One
- Some tips about the C++(自己看的,很乱)
- some questions and tips about java
- some tips about python Six
- Some tips about Class in Python
- Some tips about argument in python
- Some tips about looping statement
- Some tips about crawling large external data with bcs connector