我正在使用Selenium WebDriver 2创建自动化测试用例。我的代码已经设置好,这样当Selenium WebDriver打开FireFox时,FireBug会自动打开。几秒钟后,我要求硒再次关闭它。出于某种原因,如果我不这样做,有时 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 中做类似的事情?
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 控制台是否已打开