</pre><span style="font-family:Microsoft YaHei; font-weight:normal">页面等待的情况多为等待<span lang="EN-US">AJAX</span>异步加载</span></h1><p style="margin:3pt 0cm"><span style="font-size:14px"><span style="font-family:微软雅黑,sans-serif">处理等待<span lang="EN-US">ajax</span>加载通常有以下几种方式:</span><span lang="EN-US" style="font-family:Arial,sans-serif"></span></span></p><p style="margin:3pt 0cm"><span style="font-size:14px"><span lang="EN-US" style="font-family:微软雅黑,sans-serif">1.sleep(5000);</span><span style="font-family:微软雅黑,sans-serif">最省事的方法,给定固定的睡眠时间,在时间过后继续执行</span><span lang="EN-US" style="font-family:Arial,sans-serif"></span></span></p><p style="margin:3pt 0cm"><span style="font-size:14px"><span lang="EN-US" style="font-family:微软雅黑,sans-serif">2.</span><span style="font-family:微软雅黑,sans-serif">类似<span lang="EN-US">WaitforCondition()</span>的方法,在方法内加个判断条件,判断一个元素在新页面展现出来后继续执行</span><span lang="EN-US" style="font-family:Arial,sans-serif"></span></span></p><p style="margin:3pt 0cm"><span style="font-size:14px"><span lang="EN-US" style="font-family:微软雅黑,sans-serif">3.</span><span style="font-family:微软雅黑,sans-serif">直接判断页面<span lang="EN-US">ajax</span>加载完成后继续执行</span><span lang="EN-US" style="font-family:Arial,sans-serif"></span></span></p><p style="margin:3pt 0cm"><span lang="EN-US" style="font-family:Arial,sans-serif"><span style="font-size:14px"> </span></span></p><p style="margin:3pt 0cm"><span style="font-family:微软雅黑,sans-serif"><span style="font-size:14px">由于前两种方法存在各种弊端,以下将着重讲解第三种方法:</span></span></p><p style="margin:3pt 0cm"><span style="font-size:14px"><span style="font-family:'Microsoft YaHei'">直接上代码</span><span style="font-family:Arial,sans-serif">:</span></span></p><p style="margin:3pt 0cm"></p><pre name="code" class="java"> public static void waitForAjaxToComplete() { BrowserWindow currentWindow=Allocator.getInstance().currentWindow; WebDriver webDriver = (WebDriver) currentWindow.getDriver().getEngine(); JavascriptExecutor js = (JavascriptExecutor) webDriver; int count=0; //循环等待10次,一次等一秒 while(count++<10){ try { //jQuery.active用来判断jQuery类库的AJAX活动状态,当AJAX加载完成时,值为0 //Ajax.activeRequestCount用来判断prototype类库的AJAX活动状态,原理同上,想支持更多JS类库,请自行添加 Object AjaxActive = js.executeScript("if(typeof window.jQuery != 'undefined'){return jQuery.active;}else if(typeof window.Ajax!= 'undefined'){return Ajax.activeRequestCount;}"); if (AjaxActive.toString().equalsIgnoreCase("0")) { break; }else{ //有AJAX正在活动,就等待一秒后再次尝试 HolmosBaseTools.sleep(1000); System.out.println("=====================等待"+count+"次"); } } catch (Exception e) { System.out.println("等待加载异常"); break; } } }
相信大家看过代码就能明白,这是在HolmosWindow.java中定义的一个静态方法,需要在执行步骤中单独调用:
<span style="white-space:pre"> </span>testAJAXPage.step1.click(); testAJAXPage.step2.setText("渤海"); testAJAXPage.step3.click(); HolmosWindow.waitForAjaxToComplete(); testAJAXPage.step4.click(); HolmosWindow.closeAllWindows();
通过对业务的梳理,发现触发ajax的绝大部分为Click()方法,将ajax判断注入最底层的Element.java的Click()方法:
public void click(){ StringBuilder message=new StringBuilder(); BrowserWindow currentWindow=Allocator.getInstance().currentWindow; if(isExist()){ message.append(this.wholeComment+":"); if(currentWindow instanceof SeleniumBrowserWindow){ ((Selenium)currentWindow.getDriver().getEngine()).click(locator.getSeleniumCurrentLocator()); }else if(currentWindow instanceof WebDriverBrowserWindow){ element.click(); } message.append("click鼠标左键单击成功!"); logger.info(message); //开始判断是否有活动的ajax int count=0; //循环等待10次,一次等一秒 while(count++<10){ try { if (HolmosWindow.waitForAjaxToComplete()) { break; }else{ //有AJAX正在活动,就等待一秒后再次尝试 HolmosBaseTools.sleep(1000); System.out.println("=====================等待"+count+"次"); } } catch (Exception e) { System.out.println("等待加载异常"); break; } } }else{ message.append(this.wholeComment+":"); message.append("click鼠标左键单击失败!找不到元素!"); logger.error(message); } }
同时重写waitForAjaxToComplete()方法,只用来返回布尔型变量:
public static Boolean waitForAjaxToComplete() { BrowserWindow currentWindow=Allocator.getInstance().currentWindow; WebDriver webDriver = (WebDriver) currentWindow.getDriver().getEngine(); JavascriptExecutor js = (JavascriptExecutor) webDriver; Boolean isAJAXComplete=false; //jQuery.active用来判断jQuery类库的AJAX活动状态,当AJAX加载完成时,值为0 //Ajax.activeRequestCount用来判断prototype类库的AJAX活动状态,原理同上 Object AjaxActive = js.executeScript("if(typeof window.jQuery != 'undefined'){return jQuery.active;}else if(typeof window.Ajax!= 'undefined'){return Ajax.activeRequestCount;}"); if (AjaxActive.toString().equalsIgnoreCase("0")) { isAJAXComplete=true; } return isAJAXComplete; }
附赠我用来调试的测试代码,可供大家进行验证和继续深入探索:
Page类:
public class testAJAXPage extends Page { public testAJAXPage() { super(); this.comment =""; this.init(); } public Element step1 = new Element( "" ); { step1.addXpathLocator("/html/body/div/section/nav/ul/li[2]/a"); } public TextField step2 = new TextField( "" ); { step2.addCSSLocator("#search"); } public Element step3 = new Element( "" ); { step3.addIDLocator("//*[@id='search-btn']"); step3.addCSSLocator("#search-btn"); } public Element step4 = new Element( "" ); { step4.addIDLocator("//*[@id='oUl']/li/section/a"); step4.addCSSLocator("#oUl > li > section > a"); } }
public class testAJAX { public testAJAXPage testAJAXPage = new testAJAXPage(); protected static WebDriver webDriver; protected static InternetExplorerDriverService service; @Test public void attchtest() { HolmosWindow.openNewWindow(EngineType.WebDriverChrome, "http://10.16.57.66:8081/20141027Bank/index.html" ); testAJAXPage.step1.click(); testAJAXPage.step2.setText("渤海"); testAJAXPage.step3.click(); //HolmosWindow.waitForAjaxToComplete(); testAJAXPage.step4.click(); HolmosWindow.closeAllWindows(); } }