1

うなり声の通知をテストに追加する機能を調査しようとしています。これにより、テスト実行中に画面にメッセージを追加できます。

http://elementalselenium.com/tips/53-growlで指定されている手順に従って、このアプローチを試みています。

マシン: Windows 10 Selenium バージョン: 2.53 ブラウザ: Firefox 49

以下は私が使用しているスクリプトです:

public class GrowlTest {

    static String JGROWL_SCRIPT = "http://cdnjs.cloudflare.com/ajax/libs/jquery-jgrowl/1.2.12/jquery.jgrowl.min.js";
    static String JQUERY_SCRIPT = "http://code.jquery.com/jquery-1.11.1.min.js";
    static String JGROWL_STYLE = "http://cdnjs.cloudflare.com/ajax/libs/jquery-jgrowl/1.2.12/jquery.jgrowl.min.css";
    static FirefoxDriver driver;
    public static void main(String [] args) throws InterruptedException
    {   


        driver =new FirefoxDriver();
        driver.manage().window().maximize();
        //waitForJStoLoad();

          driver.manage().deleteAllCookies();
          growlNotification(driver,"hi this is inital test..", "try");
          driver.get("http://www.amazon.com");
          Thread.sleep(10000);
          System.out.println("waiting for popup to come..");
          driver.findElement(By.xpath("//*[contains(text(),'Stay')]")).click();
          System.out.println("clicked on pop up..now waiting for notification...");
          Thread.sleep(10000);
          System.out.println("wait is completed..");

          growlNotification(driver,"Hi First try","first:");    
          Thread.sleep(2000);
          growlNotification(driver,"Hi second try","second:");
          Thread.sleep(2000);
          growlNotification(driver,"Hi third try","third:");
          Thread.sleep(2000);
          growlNotification(driver,"Hi fourth try","fourth:");
          Thread.sleep(2000);
          driver.quit();


        try {
            Thread.sleep(5000);
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }   
    }
    private static void growlNotification(WebDriver driver, String msg, String header) throws InterruptedException 
    {
        FirefoxDriver js=(FirefoxDriver) driver;
        js.executeScript("if (!window.jQuery) {var jquery = document.createElement('script'); jquery.type = 'text/javascript';jquery.src = 'https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js';document.getElementsByTagName('head')[0].appendChild(jquery)}");
        //TODO Add check for existing jQuery on page
        js.executeScript( "var jq = document.createElement('script'); jq.type = 'text/javascript'; jq.src = '" +
        JQUERY_SCRIPT + "'; document.getElementsByTagName('head')[0].appendChild(jq);" );

        js.executeScript( "$.getScript(\"" + JGROWL_SCRIPT + "\");" );

        js.executeScript( "var lnk = document.createElement('link'); lnk.rel = 'stylesheet'; lnk.href = '" +
        JGROWL_STYLE + "'; lnk.type = 'text/css'; document.getElementsByTagName('head')[0].appendChild(lnk);" );
        js.executeScript( "$.jGrowl('" + msg + "', { header: '" + header + "' });" );
    }
}

これを実行すると、以下のエラーが発生します。

ポップアップが来るのを待っています..ポップアップをクリックしました..通知を待っています...待機が完了しました..スレッド「メイン」org.openqa.selenium.WebDriverExceptionでの例外: $.jGrowlは関数ではありませんコマンド期間またはタイムアウト: 17 ミリ秒 ビルド情報: バージョン: '2.53.0'、リビジョン: '35ae25b'、時刻: '2016-03-15 16:57:40' システム情報: ホスト: 'mkarthik-WX-1'、ip: ' 192.168.0.106'、os.name: 'Windows 10'、os.arch: 'amd64'、os.version: '10.0'、java.version: '1.8.0_73' ドライバー情報: org.openqa.selenium.firefox. FirefoxDriver の機能 [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=40.0, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true,browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}] セッション ID: 8249259c-1211-4472-b52f-fc0471061816 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(ネイティブ メソッド) at sun.reflect.NativeConstructorAccessorImpl.newInstance( NativeConstructorAccessorImpl.java:62) で sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) で java.lang.reflect.Constructor.newInstance(Constructor.java:422) で org.openqa.selenium.remote.ErrorHandler.createThrowable (ErrorHandler.java:206) org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158) で org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678) で org.openqa. selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:577) com.tests.GrowlTest.growlNotification(GrowlTest.java:74) で com.tests.GrowlTest.main(GrowlTest.java:38) で 原因: org.openqa.selenium.WebDriverException: $.jGrowl はありません関数 ビルド情報: バージョン: '2.53.0'、リビジョン: '35ae25b'、時間: '2016-03-15 16:57:40' システム情報: ホスト: 'mkarthik-WX-1'、ip: '192.168 .0.106'、os.name: 'Windows 10'、os.arch: 'amd64'、os.version: '10.0'、java.version: '1.8.0_73'mkarthik-WX-1'、ip:'192.168.0.106'、os.name:'Windows 10'、os.arch:'amd64'、os.version:'10.0'、java.version:'1.8.0_73'mkarthik-WX-1'、ip:'192.168.0.106'、os.name:'Windows 10'、os.arch:'amd64'、os.version:'10.0'、java.version:'1.8.0_73'

ただし、以下のコードを実行すると、同じスクリプトが機能します。

package com.tests;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.google.common.base.Predicate;

    public class GrowlTest {

        static String JGROWL_SCRIPT = "http://cdnjs.cloudflare.com/ajax/libs/jquery-jgrowl/1.2.12/jquery.jgrowl.min.js";
        static String JQUERY_SCRIPT = "http://code.jquery.com/jquery-1.11.1.min.js";
        static String JGROWL_STYLE = "http://cdnjs.cloudflare.com/ajax/libs/jquery-jgrowl/1.2.12/jquery.jgrowl.min.css";
        static FirefoxDriver driver;
        public static void main(String [] args) throws InterruptedException
        {   


            driver =new FirefoxDriver();
            driver.manage().window().maximize();
            //waitForJStoLoad();

              driver.manage().deleteAllCookies();
              growlNotification(driver,"hi this is inital test..", "try");
              driver.get("http://www.amazon.in");
              Thread.sleep(10000);
              System.out.println("waiting for popup to come..");
              driver.findElement(By.xpath("//*[contains(text(),'Stay')]")).click();
              System.out.println("clicked on pop up..now waiting for notification...");
              Thread.sleep(10000);
              System.out.println("wait is completed..");

              growlNotification(driver,"Hi First try","first:");    
              Thread.sleep(2000);
              growlNotification(driver,"Hi second try","second:");
              Thread.sleep(2000);
              growlNotification(driver,"Hi third try","third:");
              Thread.sleep(2000);
              growlNotification(driver,"Hi fourth try","fourth:");
              Thread.sleep(2000);
              driver.quit();


            try {
                Thread.sleep(5000);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }   
        }
        private static void growlNotification(WebDriver driver, String msg, String header) throws InterruptedException 
        {
            FirefoxDriver js=(FirefoxDriver) driver;
            js.executeScript("if (!window.jQuery) {var jquery = document.createElement('script'); jquery.type = 'text/javascript';jquery.src = 'https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js';document.getElementsByTagName('head')[0].appendChild(jquery)}");
            //TODO Add check for existing jQuery on page
            js.executeScript( "var jq = document.createElement('script'); jq.type = 'text/javascript'; jq.src = '" +
            JQUERY_SCRIPT + "'; document.getElementsByTagName('head')[0].appendChild(jq);" );

            js.executeScript( "$.getScript(\"" + JGROWL_SCRIPT + "\");" );

            js.executeScript( "var lnk = document.createElement('link'); lnk.rel = 'stylesheet'; lnk.href = '" +
            JGROWL_STYLE + "'; lnk.type = 'text/css'; document.getElementsByTagName('head')[0].appendChild(lnk);" );
            js.executeScript( "$.jGrowl('" + msg + "', { header: '" + header + "' });" );
        }
    }

親切に助けて、何をする必要があるかを提案してください。

ありがとう、よろしく、 カーシック

4

1 に答える 1

2

私の最善の推測は、呼び出している JavaScript をロードする時間を与えるために、js.executeScript() 呼び出しの合間に少しスリープする必要があるということです。ご覧のとおり、動作するスクリプトには、操作の間にさまざまなスリープがあり、物事をロードして処理できる可能性があります。

于 2016-10-10T12:02:02.093 に答える