【问题标题】:Is there a way to check if web console (e.g. FireBug) is open in java language?有没有办法检查 Web 控制台(例如 FireBug)是否以 Java 语言打开?
【发布时间】:2017-08-11 21:17:12
【问题描述】:

我正在使用 Selenium WebDriver 2 创建自动化测试用例。我的代码已设置好,以便在 Selenium WebDriver 打开 FireFox 时自动打开 FireBug。几秒钟后,我要求 Selenium 再次关闭它。出于某种原因,如果我不这样做,有时 FireBug 将在稍后阶段对我不可用。

我的问题: 当我使用 TestNG 套件运行我的测试套件时,一些测试用例最终会打开 FireBug,但不会再次关闭它。我的猜测是,这是由于现有的浏览器窗口已经打开。然后“关闭 FireBug 代码”被滥用来打开 FireBug 而不是关闭它。

因此,我想检查一下 FireBug 是否打开,如果是,我想关闭它。

开启代码:

String firebugPath = TO_Constant.fireBug();
FirefoxProfile profile = new FirefoxProfile();       
profile.addExtension(new File(firebugPath));
profile.setPreference("extensions.firebug.showFirstRunPage", false);
profile.setPreference("extensions.firebug.allPagesActivation", "on");
driver = new FirefoxDriver(profile);

关闭 FireBug 的代码:

Actions action = new Actions(driver);
action.sendKeys(Keys.F12).build().perform();

我可以使用 window.console 找到在 javascript 中执行此操作的方法。有没有办法在java中做类似的事情?

【问题讨论】:

    标签: java selenium firebug web-console


    【解决方案1】:
    console.log('is DevTools open?', window.devtools.open);
    

    你也可以监听一个事件:

    window.addEventListener('devtoolschange', function (e) {
        console.log('is DevTools open?', e.detail.open);
    });
    

    当 DevTools 取消停靠时它不起作用。但是,可以与 Chrome/Safari/Firefox DevTools 和 Firebug 一起使用。

    答案由 Sindre Sorhus 提供,已从此处获取! Find out whether Chrome console is open

    【讨论】:

      猜你喜欢
      • 2010-09-18
      • 1970-01-01
      • 2015-10-05
      • 2016-07-15
      • 2015-06-21
      • 2015-05-14
      • 1970-01-01
      • 2021-08-11
      • 2013-01-15
      相关资源
      最近更新 更多