繁体   English   中英

如何使用 Selenium Webdriver 验证浏览器通知

[英]How to validate browser notifications using Selenium Webdriver

我正在使用 Selenium Webdriver 和核心 Java 自动化一个测试用例,其中单击一个按钮,我会收到浏览器级别的通知“显示带有允许和阻止选项的通知”。 单击“允许”按钮后,我想验证来自以及单击它们的网络推送通知的内容。 是否有人知道如何做到这一点通过selenium.Notifications会是这样的Facebook铬通知

https://i.stack.imgur.com/ZNPYP.png

对于我在 Chrome 中的测试,我使用以下内容来检查推送通知是否可见。

//SITE: 
 driver.get("http://makemytrip.com/");

//Test for PUSH NOTIFICATION

if(driver.findElement(By.cssSelector("[id='webpush-bubble']" )).isDisplayed()) {
   System.out.println("Push Prompt is Present");

//Popup active(as iFrame) so navigate in it and get text
//See=> https://sqa.stackexchange.com/questions/31693/how-to-read-html-source-from-iframe-using-selinium-webdriver
  WebElement iFrame = driver.findElements(By.tagName("iframe")).get(6); 
 driver.switchTo().frame(iFrame);
          System.out.println(driver.findElement(By.xpath("//div[@class='description']")).getText());
//Then switch back to normal content for any other navigation           
driver.switchTo().defaultContent();

    } else {
        System.out.println("Push Prompt NOT Present");
    }

似乎没有办法通过 Webdriver 本地处理它。

但是,如果您可以控制网站的源代码(或者可以要求开发人员添加几行),实际上有一种方法可以对其进行测试。

只需将创建的通知保存在全局 Javascript 变量中:

var lastNotification;

// Later in the code where the notification is created...
let notification = new Notification(...);
// ... save it to be accessed from WebDriver tests. Also could be pushed to a list if many notifications can be present at the same time.
lastNotification = notification;

然后从您的 WebDriver 您可以等待通知出现(如果需要):

new WebDriverWait(...).until(ExpectedConditions.jsReturnsValue("return lastNotification");

然后您可以单击它(或执行Notification Javascript API 允许您执行的任何操作):

JavascriptExecutor jsDriver = (JavascriptExecutor) driver;
jsDriver.executeScript("lastNotification.dispatchEvent(new Event('click'))");

您可以控制 Firefox 配置文件范围的首选项以允许网络通知。

profile.setPreference("permissions.default.desktop-notification", 1);

有关更多信息,请参阅几乎重复的问题和答案: https : //stackoverflow.com/a/49111281/1079254

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM