2012-05-29 80 views
2

我一直在阅读关于如何处理Selenium webdriver中的AJAX。有很多解决方案。有没有最好的和正确的解决方案?selenium webdriver - 如何处理ajax调用

的解决方案,我看了一下,到目前为止有:

1)使用线程睡眠 2)WAITFOR方法 3)ExpectedCondition 4)FluentWait 5)PresenceOfElementLocated

谢谢!

回答

0

处理ajax组件的可靠解决方案(用于我的情况)是等待元素在页面上使用webdriver的waitUntil()API调用显示。

否则threadsleep()类似的解决方案根本不推荐用于处理Ajax。

-1

如果您想从测试中执行ajax请求,您可能需要尝试Apache Http Client。这是一些Groovy代码。使用Groovy的可能性并不高,但这应该仍然是关于获取& Post与客户端的一般信息。

import groovy.util.Expando 
import org.apache.commons.httpclient.HttpClient 
import org.apache.commons.httpclient.HttpStatus 
import org.apache.commons.httpclient.methods.PostMethod 
import org.apache.commons.httpclient.methods.GetMethod 
import java.io.BufferedReader 
import java.io.InputStreamReader 
import org.apache.commons.httpclient.Header 
import java.net.URLDecoder 
import com.auto.utils.crypto.Crypto 

class ClientHttps { 
private HttpClient client = null 
private BufferedReader br = null 
private String cookieString = "" 
private crypto = new Crypto() 
def log 
public ClientHttps(log) { 
    this.log = log 
    client = new HttpClient(); 
    client.getParams().setParameter("http.useragent", "Mozilla/5.0 (Windows NT 6.1; rv:10.0.2) Gecko/20100101 Firefox/10.0.2") 
} 
public Expando get(String url) { 
    def startTime = System.nanoTime() 
    GetMethod method = new GetMethod(url) 
    Expando returnData = new Expando() 
    try { 
     log.info("cookieString = " + cookieString) 
     method.addRequestHeader("Cookie", cookieString) 
     method.addRequestHeader("Accept", "application/json") 
     int returnCode = client.executeMethod(method) 
     log.info("returnCode = " + returnCode) 
     if (returnCode == HttpStatus.SC_NOT_IMPLEMENTED) { 
      log.error("The Get method is not implemented by this URI") 
     } else { 
      if ((returnCode != HttpStatus.SC_OK) && (returnCode != HttpStatus.SC_MOVED_PERMANENTLY)) 
       assert false, "Bad Response Code" 
      br = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream())) 
      String readLine; 
      while(((readLine = br.readLine()) != null)) { 
       log.info(readLine) 
      } 
      Header [] respHeaders = method.getResponseHeaders() 
      respHeaders.each() { 
       log.info(it.getName() + " = " + it.getValue()) 
       returnData.setProperty(it.getName(), it.getValue()) 
      } 
     } 
     def endTime = System.nanoTime() 
     def duration = endTime - startTime; 
     def seconds = (double)duration/1000000000.0; 
     log.info("Get took = " + seconds + " seconds (Get url = " + url + ")") 
     return returnData; 
    } catch (Exception e) { 
     log.error(e.message, e) 
     return null 
    } finally { 
     method.releaseConnection() 
     if(br != null) try { 
      br.close() 
     } catch (Exception fe) { 
      log.info(fe.message, fe) 
     } 
    } 
} 
public Expando post(Expando postData) { 
    def startTime = System.nanoTime() 
    PostMethod method = new PostMethod(postData.getProperty("url")) 
    postData.getProperty("params").each() {method.addParameter(it.key, it.value)} 
    Expando returnData = new Expando() 
    try { 
     int returnCode = client.executeMethod(method) 
     log.info(returnCode) 
     if(returnCode == HttpStatus.SC_NOT_IMPLEMENTED) { 
      log.error("The Post method is not implemented by this URI") 
     } else { 
      if ((returnCode != HttpStatus.SC_OK) && (returnCode != HttpStatus.SC_MOVED_TEMPORARILY)) 
       assert false, "Bad Response Code" 
      br = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream())) 
      String readLine 
      while(((readLine = br.readLine()) != null)) { 
       log.info("Response Data = " + readLine) 
      } 
      Header [] respHeaders = method.getResponseHeaders() 
      respHeaders.each() { 
       log.info(it.getName() + " = " + it.getValue()) 
       try { 
        returnData.setProperty(it.value.split("=")[0], it.value.split("=")[1]) 
       } 
       catch (Exception exc) { 
        log.info("Could not split on equals sign = " + it.value) 
       } 
      } 
     } 
     def endTime = System.nanoTime() 
     def duration = endTime - startTime; 
     def seconds = (double)duration/1000000000.0; 
     log.info("Post took = " + seconds + " seconds (Post url = " + postData.getProperty("url") + ")") 
     return returnData 
    } catch (Exception exc) { 
     log.info(exc.message, exc) 
     return null 
    } finally { 
     method.releaseConnection() 
     if(br != null) try { 
      br.close() 
     } catch (Exception fe) { 
      log.info(fe.message, fe) 
     } 
    } 
} 
} 
0

我已经使用这个,它本身等待正常工作。

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 

谢谢你试试看。

相关问题