首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用Java Selenium的Angular 4等待条件

使用Java Selenium的Angular 4等待条件
EN

Stack Overflow用户
提问于 2017-09-17 04:01:23
回答 1查看 556关注 0票数 1

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

代码语言:javascript
复制
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的版本。

代码语言:javascript
复制
<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>

EN

回答 1

Stack Overflow用户

发布于 2017-09-17 23:07:27

这是我之前在SO中发现的,并成功地使用它来等待angular。但我不知道它是否对两个都有效。

代码语言:javascript
复制
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]);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46257970

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档