HMotay
HMotay

Reputation: 31

Check if browser has been closed

I am automating a webpage that has an 'Exit Application' button. Upon clicking this button the browser is closed.

How can I verify that once clicking this button that the browser is closed?

Upvotes: 2

Views: 2620

Answers (3)

Jayesh Doolani
Jayesh Doolani

Reputation: 1233

Selenium has no readymade method to check if browser is open or close, but there is a workaround to this. You can try doing this:

/*
* returns true if browser is open, else retrurns false
*/
public boolean isBrowserOpen() {
    try {
        driver.getTitle(); //can also use driver.getCurrentUrl()
        return true;
    }
    catch(Exception e) {
        return false;
    }
}

And then in your method, add this line

if(!isBrowserOpen())
    System.out.println("Browser was still open");

Upvotes: 1

MamathaMacherla
MamathaMacherla

Reputation: 1196

Hope below code works for your case.

try {
    driver.getTitle();
    System.out.println("Browser Window is still exist");
    } 
    catch (Exception e) {
    System.out.println("Brower window is closed");
      }

Upvotes: 1

ZhenyaM
ZhenyaM

Reputation: 686

ProcessBuilder builder = new ProcessBuilder();
builder.command("tasklist");
Process process = builder.start();
String string = toString(process.getInputStream());
// find ie process name by regexp

Upvotes: 0

Related Questions