0

I'm new in Java and have one important question. So I have a Java Frame with button "Start Tests", On btn click -> chromedriver is initialized and tests are being started in Chrome. In case when browser window is closed but Frame is still opened/visible - I would like to click "Run Tests" once again and get browser window opened again. But seems when closing the Browser window - Browser session ID is not valid any more and I've got an Exception: Chrome not reachable. (Session info: chrome=49.0.2623.23) (Driver info: chromedriver=2.15.322448, platform=Windows NT 6.1 SP1 x86_64)
System info: host: ip: '192.168.69.3', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_55' Session ID: 934535b637b44a82c3482cc2b58471d6 Driver info: org.openqa.selenium.chrome.ChromeDriver

This is my class for initialize webDriver:

public class TestBase {
  public static WebDriver driver = null;

public static WebDriver getDriver() {
        if (driver == null) {
           if (Config.getProperty("browser").equalsIgnoreCase("Chrome")) {

           File fileChrome = new File("src//test/java/chromedriver.exe");
            System.setProperty("webdriver.chrome.driver", fileChrome.getAbsolutePath());
            DesiredCapabilities chrome = DesiredCapabilities.chrome();

            try {
                driver = new ChromeDriver(chrome);
            } catch (Exception e) {
                throw new RuntimeException(e);
                }

            } else {
            System.out.println("Can't get any browser");
          }
       }
    return driver;
}

And this is how I call this method in tests:

login = PageFactory.initElements(TestBase.getDriver(), Login.class);        
login.test1();

I have opinion that I need to generate a unique session ID in the beginning of tests, and kill it in the end. Or may be should I do the check if Chrome is opened, and if no - start driver again, IDK. Please help me with solving this or advice something. I will appreciate any of your help or advice. Thanks

Evg Evg
  • 209
  • 1
  • 9
  • You don't necessarily need to do anything about sessions. Once you have your working ChromeDriver you can reuse it, provided you don't call `close()` or `quit()` on it (are you?). Assuming you have a simple case, you can just run your tests in sequence with the same session. – Andrew Regan Jan 29 '16 at 14:17
  • @AndrewRegan, thank for idea. But I want to keep Browser opened. And in case when user is closing it - I 'd like to restart browser with new session. – Evg Evg Jan 29 '16 at 16:44
  • @AndrewRegan, by the way I've tried use driver.quit() and after start tests again. As a result I've got a **SessionNotFoundException:** Session ID is null. Using WebDriver after calling quit()? – Evg Evg Jan 29 '16 at 16:47
  • "And in case when user is closing it" But this is an automated test, right? The browser would only close if you're closing or quitting it in code, or perhaps some JavaScript on the site is doing so. If so, you will have to carefully manage sessions. If not, keep things simple and work in the one browser. – Andrew Regan Jan 29 '16 at 17:15
  • @AndrewRegan, "you will have to carefully manage sessions" - can you suggest something about it please? – Evg Evg Feb 01 '16 at 09:41
  • Maybe my other answer will help: http://stackoverflow.com/a/35116310/954442. I suspect that as you're just starting out, you don't need any complexity and that you shouldn't call `close()`, just a single `quit()` at the end of all your tests. – Andrew Regan Feb 01 '16 at 10:18

1 Answers1

0

So, after some attempts I've got the answer.

Was added a healthCheck method, which is checking if driver is alive.

private static boolean healthCheck(){
    try{
        driver.getTitle().equals(null); 
    }catch(Exception ex){
        System.out.println("Browser closed");
        return false;
    }

    return true;
}

And then, in getDriver() method where my chromeDriver get started with a singleton, I've added simple check with new method:

if (driver != null){
        if(healthCheck()){
            return driver;
        }
        driver = null;
    }

and after using sigleton - driver got started.

if (driver == null) {
            File fileChrome = new File("src//test/java/chromedriver.exe");
            System.setProperty("webdriver.chrome.driver", fileChrome.getAbsolutePath());

            DesiredCapabilities chrome = DesiredCapabilities.chrome();

            try {
                driver = new ChromeDriver(chrome);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

        } else {
            System.out.println("Can't get any browser");
        }

Now, if browser was accidentally closed by user, program goes to healthCheck() method, and will REopen it once again. In this case "Chrome not reachable"-exception is gone.

Evg Evg
  • 209
  • 1
  • 9