使用WebDriver遇到的那些坑
2014-03-06 13:11
190 查看
在做web项目的自动化端到端测试时主要使用的是Selenium
WebDriver来驱动浏览器。Selenium
WebDriver的优点是支持的语言多,支持的浏览器多。主流的浏览器Chrome、Firefox、IE等都支持,手机上的浏览器Android、IPhone等也支持,甚至还支持PhantomJS(由于PhantomJS跑测试时并不需要渲染元素,所以执行速度快)。
但是我在使用Selenium
WebDriver时,遇到了很多坑。这些问题,有的是因为Selenium
WebDriver与浏览器不同版本之间兼容性的问题,有的是Selenium
WebDriver本身的bug,当然也不乏自己对Selenium
WebDriver某些功能理解不透彻。我花时间总结了一下,分享给大家,希望大家以后遇到问题可以避过这些坑,少走弯路。另外也总结了一些使用WebDriver的比较好的实践,也一并分享给大家。
WebDriver每次启动一个Firefox的实例时,会生成一个匿名的profile,并不会使用当前Firefox的profile。这点一定要注意。比如如果访问被测试的web服务需要通过代理,你想直接设置Firefox的代理是行不通的,因为WebDriver启动的Firefox实例并不会使用你这个profile,正确的做法是通过FirefoxProfile来设置。
1 2 3 4 5 6 7 8 9 10 11 | public WebDriver create() { FirefoxProfile firefoxProfile = new FirefoxProfile(); firefoxProfile.setPreference("network.proxy.type",1); firefoxProfile.setPreference("network.proxy.http",yourProxy); firefoxProfile.setPreference("network.proxy.http_port",yourPort); firefoxProfile.setPreference("network.proxy.no_proxies_on",""); return new FirefoxDriver(firefoxProfile); } |
使用WebDriver点击界面上Button元素时,如果当前Button元素被界面上其他元素遮住了,或没出现在界面中(比如Button在页面底部,但是屏幕只能显示页面上半部分),使用默认的WebElement.Click()可能会触发不了Click事件。
修正方案是找到该页面元素后直接发送一条Click的JavaScript指令。
1 | ((JavascriptExecutor)webDriver).executeScript("arguments[0].click();", webElement); |
1 2 3 4 5 6 7 8 9 10 1112 | protected Function<WebDriver, Boolean> isPageLoaded() { return new Function<WebDriver, Boolean>() { @Override public Boolean apply(WebDriver driver) { return ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete"); } }; } public void waitForPageLoad() { WebDriverWait wait = new WebDriverWait(webDriver, 30); wait.until(isPageLoaded()); } |
1 2 3 4 5 6 7 8 9 10 1112 | private Function<WebDriver, Boolean> haveMoreThanOneOption(final By element) { return new Function<WebDriver, Boolean>() { @Override public Boolean apply(WebDriver driver) { WebElement webElement = driver.findElement(element); if (webElement == null) { return false; } else { int size = webElement.findElements(By.tagName("option")).size(); return size >= 1; } } }; } public void waitForDropDownListLoaded() { WebDriverWait wait = new WebDriverWait(webDriver, 30); wait.until(isPageLoaded()); } |
如果网站使用了JQuery的动画效果,我们在运行测试的时候其实可以disable JQuery的animation,一方面可以加快测试的速度,另一方面可以加强测试的稳定性(如果启用了Animation,使用WebDriver驱动浏览器时可能会出现一些无法预料的异常)。
1 | ((JavascriptExecutor)driver).executeScript("jQuery.fx.off=true"); |
12 | <parallel>classes</parallel> <threadCount>3</threadCount> <perCoreThreadCount>false</perCoreThreadCount> |
12 | public void getScreenShot() { if (webDriver instanceof TakesScreenshot) { TakesScreenshot screenshotTaker = (TakesScreenshot) webDriver; File file = screenshotTaker.getScreenshotAs(savePath); } } |
WebDriver在点选次警告框时会偶发性失败。具体原因还未查明。解决方案是尽量不使用Alert方法的警告框,而是自己实现模式窗口(比如Jquery UI的模式窗口)来实现警告框效果。这样即保证了测试的稳定性,另外我们自己可以控制警告框的样式,给用户带来更好的体验。
经常更新Selenium的版本。注意经常上Selenium的官网看是否发布了新的版本,新的版本都修复了那些bug,如果包含你遇到的bug,就可以升级到目前的版本。
相关文章推荐
- 使用WebDriver遇到的那些坑
- 使用WebDriver遇到的那些坑
- 使用WebDriver遇到的那些坑
- 使用WebDriver遇到的那些坑
- 使用WebDriver遇到的那些坑
- 使用WebDriver遇到的那些坑(转)
- 使用WebDriver遇到的那些坑
- 使用WebDriver遇到的那些坑
- 使用WebDriver遇到的那些坑
- 转:使用WebDriver过程中遇到的那些问题
- 使用WebDriver遇到的那些坑
- 使用WebDriver遇到的一些问题汇总
- 使用 JUnit4参数化的特性来创建Selenium WebDriver的数据驱动测试中遇到的错误
- 使用webuploader遇到的那些事
- 使用WebDriver遇到的一些问题汇总
- webdriver:在eclipse中使用webdriver遇到:java.lang.NoClassDefFoundError: com/google/common/base/Charsets
- 使用`selenium-webdriver-webelement.send_keys`遇到的问题
- 使用selenium的webdriver动态爬取网页时遇到的问题
- 第一次使用appium web driver遇到的两个问题
- 使用WebDriver的过程中遇到的一个Bug