我一直在閱讀關於如何處理Selenium webdriver中的AJAX。有很多解決方案。有沒有最好的和正確的解決方案?selenium webdriver - 如何處理ajax調用
的解決方案,我看了一下,到目前爲止有:
1)使用線程睡眠 2)WAITFOR方法 3)ExpectedCondition 4)FluentWait 5)PresenceOfElementLocated
謝謝!
我一直在閱讀關於如何處理Selenium webdriver中的AJAX。有很多解決方案。有沒有最好的和正確的解決方案?selenium webdriver - 如何處理ajax調用
的解決方案,我看了一下,到目前爲止有:
1)使用線程睡眠 2)WAITFOR方法 3)ExpectedCondition 4)FluentWait 5)PresenceOfElementLocated
謝謝!
處理ajax組件的可靠解決方案(用於我的情況)是等待元素在頁面上使用webdriver的waitUntil()API調用顯示。
否則threadsleep()類似的解決方案根本不推薦用於處理Ajax。
如果您想從測試中執行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)
}
}
}
}
我已經使用這個,它本身等待正常工作。
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
謝謝你試試看。