首页 > 解决方案 > 如何使用 Selenium Webdriver 验证浏览器通知

问题描述

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

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

标签: javaseleniumpush-notificationwebdrivercore

解决方案


对于我在 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");
    }

推荐阅读