1

我正在使用 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中做类似的事情?

4

1 回答 1

0
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 提供的,并且取自这里! 查看 Chrome 控制台是否已打开

于 2017-03-21T11:20:22.247 回答