2

こんにちは、Firefox プロファイルの Webdriver を使用して自律テストを作成しようとしています。Driver オブジェクトの作成中に、javascript を true に設定しました。

一部のビューではjqueryの応答が遅いため、Webdriverコードに1つのチェックを入れて、JQuery処理を待機しようとしました

waitForJQueryProcessing のコード スニペット:

public static boolean waitForJQueryProcessing(WebDriver driver,
        int timeOutInSeconds) {
    boolean jQcondition = false;
    try {
        new WebDriverWait(driver, timeOutInSeconds) {
        }.until(new ExpectedCondition<Boolean>() {

            @Override
            public Boolean apply(WebDriver driverObject) {
                return (Boolean) ((JavascriptExecutor) driverObject)
                        .executeScript("return jQuery.active == 0");
            }
        });
        jQcondition = (Boolean) ((JavascriptExecutor) driver)
                .executeScript("return jQuery.active == 0");
        return jQcondition;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return jQcondition;
}

しかし、上記のコードは例外が発生しています

スタックトレース


org.openqa.selenium.WebDriverException: ReferenceError: jQuery is not defined
Command duration or timeout: 10 milliseconds
Build info: version: '2.32.0', revision: '6c40c187d01409a5dc3b7f8251859150c8af0bcb', time: '2013-04-09 10:39:28'
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_17'
Session ID: 58ad81d0-53f9-4862-a916-a1900efdc9c0
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, browserName=firefox, rotatable=false, locationContextEnabled=true, version=21.0, cssSelectorsEnabled=true, databaseEnabled=true, handlesAlerts=true, browserConnectionEnabled=true, nativeEvents=true, webStorageEnabled=true, applicationCacheEnabled=true, takesScreenshot=true}]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:187)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
    at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:463)
    at com.iclinica.utils.WaitTool$9.apply(WaitTool.java:309)
    at com.iclinica.utils.WaitTool$9.apply(WaitTool.java:1)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:208)
    at com.iclinica.utils.WaitTool.waitForJQueryProcessing(WaitTool.java:304)
    at com.iclinica.globals.FirefoxCustomWebdriver.findElement(FirefoxCustomWebdriver.java:14)
    at com.iclinica.page.studyconfig.studydetails.StudyDetailsPage.studydetails(StudyDetailsPage.java:20)
    at com.iclinica.studyconfig.AddPatients.teststudycreation(AddPatients.java:168)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

webdriverオブジェクトにjqueryファイルパスを設定するためにグーグル検索しましたが、結果が見つかりませんでした

それが理にかなっていることを願っています。

ありがとうガウラフ

4

2 に答える 2

3

代わりにこれを使用してください:

public static boolean waitForJQueryProcessing(WebDriver driver,
        int timeOutInSeconds) {
    boolean jQcondition = false;
    try {
        new WebDriverWait(driver, timeOutInSeconds) {
        }.until(new ExpectedCondition<Boolean>() {

            @Override
            public Boolean apply(WebDriver driverObject) {
                return (Boolean) ((JavascriptExecutor) driverObject)
                        .executeScript("return jQuery.active == 0");
            }
        });
        jQcondition = (Boolean) ((JavascriptExecutor) driver)
                .executeScript("return window.jQuery != undefined && jQuery.active === 0");
        return jQcondition;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return jQcondition;
}

元のコード スニペットからの変更点は次のとおりです。

.executeScript("return window.jQuery != undefined && jQuery.active === 0");

これにより、アクティブな jQuery プロセスがあるかどうかを確認する前に、jQuery オブジェクトが定義されていることが確認されます。Selenium は高速に動作し、テストしているページに読み込まれる前に jQuery にクエリを実行することがあります。

于 2013-06-26T09:34:22.843 に答える