Harsh Nigam
Harsh Nigam

Reputation: 495

I am trying to close the browser instance on Webdriver but unable to do so

package erewards2;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class login_sib2 {

    public static void main(String[] args) {

        FirefoxDriver d1 = new FirefoxDriver();

                 d1.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
                 d1.get("https://sib3.erewardsredeem.com/fm/customer.html?action=userLogin");
                 d1.manage().window().maximize();
                 WebElement e4 = d1.findElementByLinkText("Login");
                 e4.click();
                 d1.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                 WebElement e1 = d1.findElement(By.id("showmemberid"));
                 e1.sendKeys("2800000091");
                 WebElement e2 = d1.findElementById("showmemberpwd");
                 e2.sendKeys("Miquser1");
                 WebElement e3 =  d1.findElementByClassName("loginbtn");
                 e3.click();

                WebElement e5 = d1.findElementByLinkText("Logout");

                 e5.click();



                Alert a1 = d1.switchTo().alert(); //web based alert
                WebDriverWait wait = new WebDriverWait(d1,10);
                wait.until(ExpectedConditions.alertIsPresent()).accept();
                a1.accept();

             System.out.println("test1");

                //close Firefox
                d1.close();
                System.out.println("test2");   
                // exit the program explicitly
                System.exit(0);
                System.out.println("test3");
    }

}

I added the system.out println lines for test purpose and found that My code did not able to execute completely from Test1 statement. Below is the exception I got on my Eclipse IDE.

Exception in thread "main" org.openqa.selenium.NoAlertPresentException: No alert is present (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 13.82 seconds Build info: version: '2.41.0', revision: '3192d8a', time: '2014-03-27 17:18:15' System info: host: 'pcs-PC', ip: '192.168.1.64', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_25' Session ID: 59fe366a-5fdc-4763-a6f2-0860bf1378be Driver info: org.openqa.selenium.firefox.FirefoxDriver Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, browserConnectionEnabled=true, webStorageEnabled=true, nativeEvents=false, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=30.0}] at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193) at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:595) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:610) at org.openqa.selenium.remote.RemoteWebDriver$RemoteAlert.accept(RemoteWebDriver.java:910) at erewards2.login_sib2.main(login_sib2.java:40)

Upvotes: 0

Views: 373

Answers (2)

Mrunal Gosar
Mrunal Gosar

Reputation: 4683

You can try using d1.quit() too.

Upvotes: 1

nablex
nablex

Reputation: 4767

It looks like it fails before it gets to the close statement.

Try structuring your code like this:

FirefoxDriver d1 = new FirefoxDriver();
try {
    // steps
}
finally {
    d1.close();
}

This will guarantee the close statement is executed.

Upvotes: 5

Related Questions