【问题标题】:Getting "$ is not defined"得到“$未定义”
【发布时间】:2017-02-18 10:52:00
【问题描述】:

我正在尝试探索在测试中添加咆哮通知的功能。 这样可以在测试执行时将消息添加到屏幕上。

我正在按照以下步骤尝试这种方法:http://elementalselenium.com/tips/53-growl

机器:windows 10 硒版本:2.53 浏览器:火狐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 + "' });" );
    }
}

执行此操作时出现以下错误:

等待弹出窗口..点击弹出窗口..现在等待 通知...等待已完成..线程“main”中的异常 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 Capabilities [{applicationCacheEnabled=true,rotatable=false,handlesAlerts=true, databaseEnabled=true,版本=40.0,平台=WINDOWS, nativeEvents=false,acceptSslCerts=true,webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, 需要Screenshot=true,javascriptEnabled=true, cssSelectorsEnabled=true}] 会话 ID: 8249259c-1211-4472-b52f-fc0471061816 在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法) 在 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'

但是,当我执行以下代码时,相同的脚本可以工作:

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 + "' });" );
        }
    }

请帮助并建议需要做什么。

感谢和问候, 卡提克

【问题讨论】:

    标签: java jquery selenium firefox jgrowl


    【解决方案1】:

    我最好的猜测是,您需要在 js.executeScript() 调用之间稍作休息,以便为您调用的 javascript 提供加载时间。如果你看,你说的脚本在操作之间有各种睡眠,可能允许加载和处理。

    【讨论】:

    • 不...尝试使用相同的代码..它不起作用得到相同的错误:线程“main”中的异常 org.openqa.selenium.WebDriverException: $.jGrowl 不是函数命令持续时间或超时:16 毫秒构建信息:版本:'2.53.0',修订:'35ae25b',时间:'2016-03-15 16:57:40'
    • 我已经添加了所需的文件 1) 2) 3)cdnjs.cloudflare.com/ajax/libs/jquery-jgrowl/1.2.12/…" rel="stylesheet"> 当我在控制台中尝试 $.jGrowl('hi') 时它工作正常.但是当通过java程序执行时,我得到“$ is not defined”@stanlemon
    猜你喜欢
    • 2015-05-16
    • 2017-01-18
    • 1970-01-01
    • 2019-11-05
    • 2018-11-08
    • 2021-03-19
    • 2020-11-30
    • 2019-08-13
    • 1970-01-01
    相关资源
    最近更新 更多