5

My test checks to see if I can send a message to the mail. When I click the send button should check the alert.

My code for alert is:

        Alert alert = driver.switchTo().alert();
        System.out.println(alert.getText());
        alert.accept();
        Assert.assertTrue(alert.getText().contains("Thanks."));

My error is:

Starting ChromeDriver 2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed) on port 13707
Only local connections are allowed.

org.openqa.selenium.NoAlertPresentException: no alert open
  (Session info: chrome=55.0.2883.87)
  (Driver info: chromedriver=2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed),platform=Windows NT 6.3.9600 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 76 milliseconds
Build info: version: 'unknown', revision: '31c43c8', time: '2016-08-02 21:57:56 -0700'
System info: host: 'Gaga', ip: '192.168.1.6', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_101'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed), userDataDir=C:\Users\Dragana\AppData\Local\Temp\scoped_dir6652_7181}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=55.0.2883.87, platform=WIN8_1, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: b7131fce27b2ef40f1daff3c82188e7c
2
  • 2
    is it because you accepted the alert first and then tried to get the text !? Commented Dec 21, 2016 at 21:24
  • I don't know, this is my first alert. Commented Dec 21, 2016 at 21:42

2 Answers 2

13

The code has to wait for alert. Below is the sample code

try {
    WebDriverWait wait = new WebDriverWait(driver, 2);
    wait.until(ExpectedConditions.alertIsPresent());
    Alert alert = driver.switchTo().alert();
    System.out.println(alert.getText());
    alert.accept();
    Assert.assertTrue(alert.getText().contains("Thanks."));
} catch (Exception e) {
    //exception handling
}
Sign up to request clarification or add additional context in comments.

2 Comments

Works like a charm.
still same exception for me even though it has opened alert window
2

This is a very bad approach to use try-catch especially in selenium based test cases and their use should be limited:

Let's understand the real solution of this problem and why the given solution is incorrect because:

  1. If we try to call alert.getText() after alert.accept(); it would obvious that it going to throw error because we have already closed the alert.

  2. It is working fine because by using try-catch, it will going to handle the exception.

  3. The problem is that assertion is never checked here.

Code: The below code will terminate every time even before checking the assertion.

alert.accept();    
Assert.assertTrue(alert.getText().contains("Thanks."));

Corrected Code: The alert.accept(); should be call after getting the text.

    WebDriverWait wait = new WebDriverWait(driver, 2);
    wait.until(ExpectedConditions.alertIsPresent());
    Alert alert = driver.switchTo().alert();
    Assert.assertTrue(alert.getText().contains("Thanks."));
    alert.accept();

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.