How To Handle Unexpected Alerts In Selenium WebDriver

Some times when nosotros browsing software spider web application, Display to a greater extent than or less unexpected alerts due to to a greater extent than or less fault or another reasons. This sort of alerts not display every fourth dimension but they are displaying solely to a greater extent than or less time. If y'all direct maintain created webdriver software automation test instance for such page in addition to non handled this sort of alerts In your code then your script volition neglect Immediately If such unexpected warning popular up displayed.

We direct maintain already learnt, How to handgrip expected warning popups In selenium webdriver software testing tool In THIS EXAMPLE. Now unexpected warning appears solely to a greater extent than or less times thus nosotros tin non write straight code to bring or dismiss that alert. In this sort of situation, nosotros direct maintain to handgrip them specially.


To handgrip this sort of unexpected alerts, You must at to the lowest degree aware near on which activity such unexpected warning Is generated. Sometimes, They are generated during software spider web application page charge in addition to sometime they are generated when y'all perform to a greater extent than or less action. So start of all nosotros direct maintain to greenback downwards the activity where such unexpected warning Is generated in addition to thus nosotros tin cheque for warning subsequently performing that action. We require to piece of job endeavour select direct maintain of block for checking such unexpected alters because If nosotros volition piece of job straight code(without endeavour catch) to bring or dismiss warning in addition to If warning non appears thus our evidence instance volition fail. endeavour select direct maintain of tin handgrip both situations.

I direct maintain ane illustration where warning Is displaying when loading software spider web page. So nosotros tin cheque for warning Inside endeavour select direct maintain of block subsequently page charge every bit shown In bellow given example. After loading page, It volition cheque for alert. If warning Is at that topographic point on the page thus It volition dismiss It else It volition become to select direct maintain of block in addition to impress message every bit shown In bellow given example.

Run bellow given illustration In eclipse amongst testng in addition to let on the result.
package Testng_Pack;  import java.util.concurrent.TimeUnit;  import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test;  populace cast unexpected_alert {  WebDriver driver;   @BeforeTest  populace void setup() throws Exception {   driver =new FirefoxDriver();        driver.manage().window().maximize();   driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);   driver.get(" ");  }   @AfterTest  populace void tearDown() throws Exception {   driver.quit();  }   @Test  populace void Text() throws InterruptedException {   //To handgrip unexpected warning on page load.   try{       driver.switchTo().alert().dismiss();     }catch(Exception e){     System.out.println("unexpected warning non present");      }      driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("fname");   driver.findElement(By.xpath("//input[@name='lname']")).sendKeys("lname");   driver.findElement(By.xpath("//input[@type='submit']")).click();  } }

Above given webdriver code Is but for example. It Is but explaining the means of using endeavour select direct maintain of block to handgrip unexpected alert. You tin piece of job such endeavour select direct maintain of block In that expanse where y'all are facing unexpected alerts really frequently.

More interesting articles here :Generation Enggelmundus Internet Marketing Tool here :Zeageat IM http://www.software-testing-tutorials-automation.com/
Post a Comment (0)
Previous Post Next Post