Selenium : How to handle a window which automatically closes and back to parent window & Opens window PopUp



  • I have a test case with following scenario:

    1. Click on global Order Mgmt Super user
    2. Click on Sales Order link
    3. Which Actually Opens new Window & Closes automatically
    4. Focus came back to Parent window & Open up a window popup
    5. Click on Ok button

    Code :

    // Click on Global Order MGMT Superuser role & Sales Order
    
    driver.findElement(By.xpath("//a[contains(text(),'Global Order Management Super User')]")).click();
    
    driver.findElement(By.xpath("//*[@id=\"N104\"]")).click();
    
    
            Set <String> handles =driver.getWindowHandles();
            Iterator<String> it = handles.iterator();
    
            String parent = it.next();
            String child = it.next();
    
            driver.switchTo().defaultContent();
    
    
            Alert alert = driver.switchTo().alert();
            alert.accept();     
    

    Error :

    FAILED: testCase1
    org.openqa.selenium.NoAlertPresentException: 
    Build info: version: '3.14.0', revision: 'aacccce0', time: '2018-08-02T20:05:20.749Z'
    System info: host: 'ADONGAR-LAP', ip: '10.180.179.18', os.name: 'Windows 10', os.arch: 'x86', os.version: '10.0', java.version: '1.8.0_201'
    Driver info: org.openqa.selenium.firefox.FirefoxDriver
    Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 64.0, capabilities: {desiredCapabilities: {acceptInsecureCerts: true, browserName: firefox}}, javascriptEnabled: true, moz:accessibilityChecks: false, moz:headless: false, moz:processID: 23748, moz:profile: C:\Users\adongar\AppData\Lo..., moz:shutdownTimeout: 60000, moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, platformVersion: 10.0, rotatable: false, setWindowRect: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
    Session ID: 7e3fd1a5-32e0-4fb5-b83c-5f46f14ee28a
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createEx
    


  • May this help you:-

        driver.findElement(By.xpath("//a[contains(text(),'Global Order Management Super User')]")).click();
        driver.findElement(By.xpath("//*[@id=\"N104\"]")).click();
        // It will return the parent window name as a String
         String mainWindow=driver.getWindowHandle();
         // It returns no. of windows opened by WebDriver and will return Set of Strings
         Set<String> set =driver.getWindowHandles();
         // Using Iterator to iterate with in windows
         Iterator<String> itr= set.iterator();
         while(itr.hasNext()){
         String childWindow=itr.next();
         // Compare whether the main windows is not equal to child window. If not equal, we will close.
         if(!mainWindow.equals(childWindow)){
         driver.switchTo().window(childWindow);
         // If you just want to print or match the title of new opened window.
         System.out.println(driver.switchTo().window(childWindow).getTitle());
         }
         }
         // This is to switch to the main window
         driver.switchTo().window(mainWindow);
         // Wait for 10 seconds to find the Alert on parent window.
         WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.alertIsPresent());
        driver.switchTo().alert().accept();  
    

    I have just write this on the basis of your test steps.



Suggested Topics