Selenium2学习-028-WebUI自动化实战实例-026-获取页面元素值或者元素属性值
2015-08-03 15:56
681 查看
在自动化脚本编写过程中,经常需要获取页面元素的文本进行判断,以便对于不同的文本进行不同的处理。比如:很多的购物网站,加入购物车的按钮是有多个状态的(加入购物车、到货通知、暂不销售等),那么在实际的操作过程中,需要对此按钮对应的不同的值,执行相应的逻辑。
代码相对比较简单,在此不再详细说明了,直接上码,敬请各位小主参阅,若有不足之处,敬请大神指正,非常感谢!
获取元素值的源码如下所示:
获取元素属性值的源码如下所示:
至此,WebUI 自动化功能测试脚本第 026-获取页面元素值或者元素属性值 顺利完结,希望此文能够给初学 Selenium 的您一份参考。
最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢! ^_^
代码相对比较简单,在此不再详细说明了,直接上码,敬请各位小主参阅,若有不足之处,敬请大神指正,非常感谢!
获取元素值的源码如下所示:
/** * @function Get text of element. It will be return null when the element not exist or By is null * * @author Aaron.ffp * @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java getElementText, 2015-1-25 20:48:04 Exp $ * * @param by : By * * @return String */ public String getElementText(By by){ String elementText = ""; if (by == null) { return ""; } try { elementText = this.getElement(by).getText().toString().trim(); } catch (NoSuchElementException nsee) { this.logger.error(nsee); nsee.printStackTrace(); } return elementText; } /** * @function Get text of element. It will be return null when the element not exist or locator is empty * * @author Aaron.ffp * @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java getElementText, 2015-1-25 20:47:40 Exp $ * * @param locator : (ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName) * * @return String */ public String getElementText(String locator){ String elementText = ""; locator = ((locator == null)|| "".equals(locator)) ? "" : locator; try { elementText = this.getElement(locator).getText().toString().trim(); } catch (NoSuchElementException nsee) { this.logger.error(nsee); nsee.printStackTrace(); } return elementText; } /** * @function Get text of element. It will be return empty when the element not exist or locator is empty * * @author Aaron.ffp * @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java getElementText, 2015-1-25 19:10:42 Exp $ * * @param webdriver : WebDriver * @param by : By * * @return String */ public String getElementText(WebDriver webdriver, By by){ String elementText = ""; try { elementText = this.getElement(webdriver, by).getText().toString().trim(); } catch (NoSuchElementException nsee) { this.logger.error(nsee); nsee.printStackTrace(); } return elementText; } /** * @function Get text of element. It will be return empty when the element not exist or locator is empty * * @author Aaron.ffp * @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java getElementText, 2015-1-25 19:01:03 Exp $ * * @param webdriver : WebDriver * @param locator : (ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName) * * @return String */ public String getElementText(WebDriver webdriver, String locator){ String elementText = ""; try { elementText = this.getElement(webdriver, locator).getText().toString().trim(); } catch (NoSuchElementException nsee) { this.logger.error(nsee); nsee.printStackTrace(); } return elementText; } /** * @function Get text of element. It will be return empty when the element not exist or locator is empty * * @author Aaron.ffp * @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java getElementTextByXpath, 2015-1-25 20:47:03 Exp $ * * @param locator : XPath * * @return String */ public String getElementTextByXpath(String locator){ String elementText = ""; try { elementText = this.webdriver.findElement(By.xpath(locator)).getText().toString().trim(); } catch (NoSuchElementException nsee) { this.logger.error(nsee); nsee.printStackTrace(); } return elementText; } /** * @function Get text of element. It will be return empty when the element not exist or locator is empty * * @author Aaron.ffp * @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java getElementText, 2015-1-25 20:47:03 Exp $ * * @param webdriver : WebDriver * @param locator : XPath * * @return String */ public String getElementTextByXpath(WebDriver webdriver, String locator){ String elementText = ""; try { elementText = this.webdriver.findElement(By.xpath(locator)).getText().toString().trim(); } catch (NoSuchElementException nsee) { this.logger.error(nsee); nsee.printStackTrace(); } return elementText; }
获取元素属性值的源码如下所示:
/** * @function Get text of attribute to the element. It will be return empty when the attribute not exist. * * @author Aaron.ffp * @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java getElementValue, 2015-1-25 20:42:13 Exp $ * * @param locator : (ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName) * @param attribute : attribute of element * * @return String */ public String getElementValue(By by, String attribute){ String attrbuteValue = ""; try { attrbuteValue = this.getElement(by).getAttribute(attribute); } catch (NoSuchElementException nsee) { this.logger.error(nsee); nsee.printStackTrace(); } return attrbuteValue; } /** * @function Get text of attribute to the element. It will be return empty when the attribute not exist. * * @author Aaron.ffp * @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java getElementValue, 2015-1-25 20:37:02 Exp $ * * @param locator : (ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName) * @param attribute : attribute of element * * @return String */ public String getElementValue(String locator, String attribute){ String attrbuteValue = ""; try { attrbuteValue = this.getElement(locator).getAttribute(attribute); } catch (NoSuchElementException nsee) { this.logger.error(nsee); nsee.printStackTrace(); } return attrbuteValue; } /** * @function Get text of attribute to the element. It will be return empty when the attribute not exist. * * @author Aaron.ffp * @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java getElementValue, 2015-1-25 19:41:17 Exp $ * * @param webdriver : WebDriver * @param locator : (ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName) * @param attribute : attribute of element * * @return String */ public String getElementValue(WebDriver webdriver, By by, String attribute){ String attrbuteValue = ""; try { attrbuteValue = this.getElement(webdriver, by).getAttribute(attribute); } catch (NoSuchElementException nsee) { this.logger.error(nsee); nsee.printStackTrace(); } return attrbuteValue; } /** * @function Get text of attribute to the element. It will be return empty when the attribute not exist. * * @author Aaron.ffp * @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java getElementValue, 2015-1-25 19:36:23 Exp $ * * @param webdriver : WebDriver * @param locator : (ID, name, cssSelector, xpath, linkText, className, partialLinkText, tagName) * @param attribute : attribute of element * * @return String */ public String getElementValue(WebDriver webdriver, String locator, String attribute){ String attrbuteValue = ""; try { attrbuteValue = this.getElement(webdriver, locator).getAttribute(attribute); } catch (NoSuchElementException nsee) { this.logger.error(nsee); nsee.printStackTrace(); } return attrbuteValue; } /** * @function Get text of attribute to the element. It will be return empty when the attribute not exist. * * @author Aaron.ffp * @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java getElementValueByXpath, 2015-1-25 20:47:03 Exp $ * * @param locator : XPath * @param attribute : attribute of element * * @return String */ public String getElementValueByXpath(String locator, String attribute){ String attrbuteValue = ""; try { attrbuteValue = this.webdriver.findElement(By.xpath(locator)).getAttribute(attribute); } catch (NoSuchElementException nsee) { this.logger.error(nsee); nsee.printStackTrace(); } return attrbuteValue; } /** * @function Get text of attribute to the element. It will be return empty when the attribute not exist. * * @author Aaron.ffp * @version V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java getElementValueByXpath, 2015-1-25 20:47:03 Exp $ * * @param webdriver : WebDriver * @param locator : XPath * @param attribute : attribute of element * * @return String */ public String getElementValueByXpath(WebDriver webdriver, String locator, String attribute){ String attrbuteValue = ""; try { attrbuteValue = this.webdriver.findElement(By.xpath(locator)).getAttribute(attribute); } catch (NoSuchElementException nsee) { this.logger.error(nsee); nsee.printStackTrace(); } return attrbuteValue; }
至此,WebUI 自动化功能测试脚本第 026-获取页面元素值或者元素属性值 顺利完结,希望此文能够给初学 Selenium 的您一份参考。
最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢! ^_^
相关文章推荐
- UIScrollView 与 Use Auto Layout
- 运用UI制作简单的 登陆系统
- C# 获取URL参数 NameValueCollection
- ViewPager,java.lang.UnsupportedOperationException: Required method destroyItem was not overridden
- iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(二)
- Ultra-QuickSort
- Ultra-QuickSort 分类: POJ 排序 2015-08-03 15:39 2人阅读 评论(0) 收藏
- iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一)
- E437: terminal capability "cm" required 解决办法
- poj2251+求最佳为BFS+方向数组简化+queue要有作用域
- Unique Binary Search Trees
- [折半枚举] poj 2785 4 Values whose Sum is 0
- 改变UISearchBar的细节问题
- hdu 4027 Can you answer these queries?
- POJ 2442 Sequence(堆的应用)
- HDU 4343 Interval query 倍增思想, DP
- UILabel自适应
- Flash Builder常见菊紧问题集锦
- 再谈select, iocp, epoll,kqueue及各种I/O复用机制
- iOS开发UI篇—简单介绍静态单元格的使用