Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate browser notifications using Selenium Webdriver

I am Automating one test case using Selenium Webdriver and core Java,in which on clicking one button I get browser level notification 'Show notifications with options Allow and Block'. After clicking "Allow" button, I want to validate contents of web push notifications that come as well as click on them. Does anybody know how to do that through selenium.Notifications will come something like this facebook chrome notifications

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

like image 755
user11221 Avatar asked Nov 18 '22 12:11

user11221


1 Answers

For my test in Chrome I used the following to check if push notification is visible.

//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");
    }
like image 197
John T Avatar answered Mar 05 '23 18:03

John T