Testing AngularJS with Selenium

后端 未结 12 731
梦毁少年i
梦毁少年i 2020-11-27 04:56

I have a SPA application on stack ASP MVC + AngularJS and I\'d like to test the UI. For now I\'m trying Selenium with PhantomJS and WebKit drivers.

This is a sample

12条回答
  •  执念已碎
    2020-11-27 05:26

    Create a new class that lets you figure out whether your website using AngularJS has finished making AJAX calls, as follows:

    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.support.ui.ExpectedCondition;
    
    public class AdditionalConditions {
        public static ExpectedCondition angularHasFinishedProcessing() {
            return new ExpectedCondition() {
                @Override
                public Boolean apply(WebDriver driver) {
                    return Boolean.valueOf(((JavascriptExecutor) driver).executeScript("return (window.angular !== undefined) && (angular.element(document).injector() !== undefined) && (angular.element(document).injector().get('$http').pendingRequests.length === 0)").toString());
                }
            };
        }
    }
    

    You can use it anywhere in the your code by using the following code:

    WebDriverWait wait = new WebDriverWait(getDriver(), 15, 100);
    wait.until(AdditionalConditions.angularHasFinishedProcessing()));
    

提交回复
热议问题