0

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
1
  • On new open window do you perform any action? Apr 29, 2019 at 11:03

2 Answers 2

0

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.

0

Since it does not seem that you perform any actions on newly opened window and as you say it closes automatically I would just focus on the alert, as this is where the exception occurs.

remove this part:

Set <String> handles =driver.getWindowHandles();
Iterator<String> it = handles.iterator();

String parent = it.next();
String child = it.next();

driver.switchTo().defaultContent();

and add a FluentWait:

FluentWait<WebDriver> fluentWait = new FluentWait<>(webDriver)
        .pollingEvery(Duration.ofMillis(500))
        .withTimeout(Duration.ofSeconds(10))
        .ignoring(NoAlertPresentException.class);
fluentWait.until(ExpectedConditions.alertIsPresent()).accept();

Bare in mind that the alert that can be addressed this way is a default browser alert. If the popup you mention is something specific to your app it has to be handled like any other WebElement (FluentWait can still be used to wait for it, but the code would be different).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.