当前位置: 首页 > 知识库问答 >
问题:

获取“未定义$”

弓温书
2023-03-14

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

我正在尝试通过以下步骤指定的这种方法: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 + "' });" );
    }
}

执行此操作时,我收到以下错误:

等待弹出窗口出现..单击弹出窗口..现在等待通知…等待已完成..线程“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功能[{applicationCacheEnabled=true,rotable=false,handlesAlerts=true,databaseEnabled=true、version=40.0,platform=WINDOWS,nativeEvents=false,acceptSslCerts=true、webStorageEnabled=true,locationContextEnabled=true;browserName=firefox,takeScreenshot=true,javascriptEnabled=true,cssSelectorsEnabled=true}〕会话ID:8249259c-1211-4472-b52f-fc0471061816,位于sun.relect.NativeConstructorAccessorImpl.newInstance0(本机方法)位于sun.relect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessor Impl.java:62)位于sun.elect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorsImpl.java:45)位于org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)位于org.openqa.selecton.remote.ErrorHandler.throwIfResponseFailed(ErrorHandleer.java:158)位于com.tests.GrowlTest.main(GrowlTest.java:38)位于org.openqa.selection.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:577org.openqa.selenium.WebDriverException:$.jGrowl不是函数Build info:version:'2.53.0',revision:'35ae25b',time:'2016-03-15 16:57:40'系统信息:host:'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 + "' });" );
        }
    }

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

谢谢和问候,卡尔西克

共有1个答案

钮承恩
2023-03-14

我的最佳猜测是,您需要在js.executeScript()调用之间Rest一会儿,以便让您调用的javascript有时间加载。如果你仔细观察,你说有效的脚本在操作之间有各种各样的睡眠,可能是为了加载和处理东西。

 类似资料:
  • 问题内容: 我正在使用Expressjs版本4.我在req.param上得到了’undefined’。这是我的示例:app.js ./routes/新 newdata.html 我也尝试了和,但答案仍然相同。 问题答案: 引用路径中的变量。 帖子数据可以通过以下方式引用 假设您正在使用中间件。 然后是那些。 您可以使用上面的3种。外观上的顺序是,,。

  • 什么会引起这样的问题?解决办法可能是什么?

  • 问题内容: 在开发机器上,我的网站运行正常,但是在托管服务器上我遇到了javascript’Sys ‘未定义 错误,并且我的AJAX无法正常工作。 我在上面做了一个示例页面和脚本管理器,请仔细研究。 http://nexapps.com/default2.aspx 按钮和文本框位于AJAX updatepanel中,但是当单击按钮页面时,将回发完整。 注意:托管服务器-IIS7,但我的其他网站在同

  • 问题内容: 我正在将React代码转换为打字稿,tsconfig中的目标是es5。 在IE 11中运行时,出现错误“未定义承诺” 我知道我需要填充,但是如何? 我没有使用Babel。 以下是我的Webpack.config 问题答案: 在axios上面写这个对我有用,也许其他选择也可以 我主要面对的是IE中的缓存问题 安装webpack插件也可以 并包括 在webpack插件中 我将使用更多可能的

  • 我正试图从“iframe”标签中获取信息。当我执行这段代码时,它说没有定义“USER\u AGENT”。我怎样才能解决这个问题?

  • 我正在从参考资料中学习spring security。版本3.1.2。释放如中所述,我已经像这样配置了安全性:http security-context.xml web.xml 安全servlet。xml 但我在启动应用程序时遇到了这个异常。如果我删除了安全配置,我的spring web应用程序就会正常工作。我在stackoverflow中也遇到过同样的问题。但运气不好。