英文:
Selenium wait for specific XHR request to be complete
问题
你好,我正在进行一个Selenium项目,我所遇到的主要困难是等待XHR请求完成。我目前正在使用以下预期条件等待请求被发出,
public ExpectedCondition<Boolean> jQueryExpect(int expectedActive) {
ExpectedCondition<Boolean> jQLoad = new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver dr) {
try {
logger.log(Level.INFO, "Checking number of jQueries Active");
Long active = (Long) ((JavascriptExecutor) driver).executeScript("return jQuery.active");
logger.log(Level.INFO, "jQuery's active: {0}", active);
return (active >= expectedActive);
} catch (Exception e) {
logger.log(Level.WARNING, "Error executing script in jQueryLoad method");
// no jQuery present
return true;
}
}
};
return jQLoad;
}
然后,我使用以下预期条件等待jQuery加载完成:
public ExpectedCondition<Boolean> jQueryLoad(int expectedActive) {
ExpectedCondition<Boolean> jQLoad = new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver dr) {
try {
logger.log(Level.INFO, "Checking number of jQueries Active");
Long active = (Long) ((JavascriptExecutor) driver).executeScript("return jQuery.active");
logger.log(Level.INFO, "jQuery's active: {0}", active);
return (active <= expectedActive);
} catch (Exception e) {
logger.log(Level.WARNING, "Error executing script in jQueryLoad method");
// no jQuery present
return true;
}
}
};
return jQLoad;
}
这个方法目前效果还不错,因为我知道有多少个请求要等待。但正如您已经注意到的,由于某些原因请求的数量发生变化,这个方法未来很容易失效。
我一直在查阅Cypress文档,发现了这个。根据Cypress文档,它等待指定的请求被发出。
cy.wait(['@getUsers', '@getActivities', '@getComments']).then((xhrs) => {
// xhrs现在将是匹配的XHR数组
// xhrs[0] <-- getUsers
// xhrs[1] <-- getActivities
// xhrs[2] <-- getComments
})
Selenium中是否有类似的方法?或者是否有实现这个的方法?到目前为止,根据我的搜索,我什么都没有找到。所以,任何帮助将不胜感激。
英文:
Hi i am working on a selenium project and the top difficulty that i am having was waiting for XHR request to be completed. What i am currently doing is i wait for a request to be made using following expected condition,
public ExpectedCondition<Boolean> jQueryExpect (int expectedActive) {
ExpectedCondition<Boolean> jQLoad = new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver dr) {
try {
logger.log(Level.INFO,"Checking number of jQueries Active");
Long active = (Long) ((JavascriptExecutor) driver).executeScript("return jQuery.active");
logger.log(Level.INFO,"jQuery''s active: {0}",active);
return (active >= expectedActive);
}
catch (Exception e) {
logger.log(Level.WARNING,"Error executing script in jQueryLoad method");
// no jQuery present
return true;
}
}
};
return jQLoad;
}
And then i wait for the jQuery to load using this expected condition
public ExpectedCondition<Boolean> jQueryLoad (int expectedActive) {
ExpectedCondition<Boolean> jQLoad = new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver dr) {
try {
logger.log(Level.INFO,"Checking number of jQueries Active");
Long active = (Long) ((JavascriptExecutor) driver).executeScript("return jQuery.active");
logger.log(Level.INFO,"jQuery''s active: {0}",active);
return (active <= expectedActive);
}
catch (Exception e) {
logger.log(Level.WARNING,"Error executing script in jQueryLoad method");
// no jQuery present
return true;
}
}
};
return jQLoad;
}
This method is working pretty solid for now since i know how many requests to expect. But as you have already noticed it can easily break in future as number of requests made are changed for some reason.
I been looking at cypress documentation and found this. According to cypress documentation this waits for the specified requests to be made.
cy.wait(['@getUsers', '@getActivities', '@getComments']).then((xhrs) => {
// xhrs will now be an array of matching XHR's
// xhrs[0] <-- getUsers
// xhrs[1] <-- getActivities
// xhrs[2] <-- getComments
})
Is there any such method available in Selenium? or Is there any way this can be implemented? So far from what i have googled i got nothing. So any help will be appreciated.
答案1
得分: 1
你可以定位元素并等待元素。在Selenium中有隐式等待和显式等待。
你可以使用以下任一方法:
WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id<locator>));
或者
wait.until(ExpectedConditions.elementToBeClickable(By.id<locator>));
更多信息请参见:此答案链接
英文:
You can locate Element and wait for element
There are Implicit and Explicit waits in selenium.
You can use either
WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id<locator>));
or
wait.until(ExpectedConditions.elementToBeClickable(By.id<locator>));
More information: on this answer
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论