【问题标题】:Angular 4 Wait Condition Using Java Selenium使用 Java Selenium 的 Angular 4 等待条件
【发布时间】:2017-09-16 20:01:22
【问题描述】:

我在编写适用于 Angular 4 和 Angular 1 的 java selenium 等待条件时遇到问题。我目前有一个适用于 Angular 1 的解决方案,如下所示:

public static ExpectedCondition<Boolean> angularHasFinishedProcessing() {
    return new ExpectedCondition<Boolean>() {
        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());
        }
    };
}

public static void waitOnAngular(WebDriver driver){
    WebDriverWait wait = new WebDriverWait(driver, 15, 100);
    wait.until(AdditionalConditions.angularHasFinishedProcessing());
}

我正在寻找一些 JavaScript 来扩展这个 apply() 方法以使用 angular 4 或人们已经成功地让 selenium 等到 angular $http 请求与所有视图一起加载的不同解决方案。

查看下面的 pom.xml 文件,看看我是什么版本的 selenium-java 版本。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.ridebooker</groupId>
<artifactId>auto-tests</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Ridebooker Auto Tests</name>

<dependencies>
    <dependency>
        <groupId>com.codepine.api</groupId>
        <artifactId>testrail-api-java-client</artifactId>
        <version>RELEASE</version>
        <exclusions>
            <exclusion>
                <artifactId>guava</artifactId>
                <groupId>com.google.guava</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>1.2.0</version>

    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-testng</artifactId>
        <version>1.2.0</version>

    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>1.2.0</version>

    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.5.2</version>

    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.9</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.11-beta3</version>
    </dependency>
    <dependency>
        <groupId>xml-apis</groupId>
        <artifactId>xml-apis</artifactId>
        <version>2.0.2</version>
    </dependency>

    <dependency>
        <groupId>xerces</groupId>
        <artifactId>xercesImpl</artifactId>
        <version>2.8.0</version>
    </dependency>

    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-all</artifactId>
        <version>1.3</version>
        <scope>test</scope>
    </dependency>

</dependencies>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18</version>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.surefire</groupId>
                        <artifactId>surefire-junit47</artifactId>
                        <version>2.18</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <systemPropertyVariables>

                        <!--To use different properties when ran from inside your IDE change your run config to pass
                         the VM a parameter such as:
                         -Denv.HOME=local
                         -Denv.HOME=staging
                         -Denv.HOME=production
                        -->

                        <environment>${env.HOME}</environment>
                    </systemPropertyVariables>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

【问题讨论】:

    标签: javascript java angular maven selenium


    【解决方案1】:

    这是我之前在 SO 中发现的,并且非常成功地使用它来等待 Angular。但我不知道它是否适用于两者。

    final String script = "var callback = arguments[arguments.length - 1];\n" +
            "var rootSelector = \'body\';\n" +
            "var el = document.querySelector(rootSelector);\n" +
            "\n" +
            "try {\n" +
            "    if (angular) {\n" +
            "        window.angular.getTestability(el).whenStable(callback);\n" +
            "    }\n" +
            "    else {\n" +
            "        callback();\n" +
            "    }\n" +
            "} catch (err) {\n" +
            "    callback(err.message);\n" +
            "}";
    
    ((JavascriptExecutor) getDriver()).executeAsyncScript(script, new Object[0]);
    

    【讨论】:

    • 我对您提供的脚本不满意,您或其他人是否对 Angular 4 有类似的东西?
    • 很遗憾没有,这就是我发现和使用的全部
    • 它不适用于 angular 4+,因为 'angular' 变量不再可以从 javascript 访问
    【解决方案2】:

    window.getAllAngularTestabilities()

    是你想要的功能。这是一个例子:

    var testabilities = window.getAllAngularTestabilities();
    var count = testabilities.length;
    var decrement = function() {
        count--;
        if (count === 0) {
            return;
        }
    };
    var waitForAllAngular2 = function() {
        try {
            testabilities.forEach(function(testability) {
                testability.whenStable(decrement);
            });
        } catch (err) {
            console.log(err.message);
        }
    };
    waitForAllAngular2();
    

    这可以确保 Angular 在执行任何操作之前是稳定的。

    【讨论】:

      猜你喜欢
      • 2022-01-12
      • 2017-12-16
      • 1970-01-01
      • 2019-01-20
      • 2016-10-10
      • 2021-12-31
      • 2022-01-21
      • 1970-01-01
      • 2020-07-13
      相关资源
      最近更新 更多