i'm new in java , have 1 important question. have java frame button "start tests", on btn click -> chromedriver initialized , tests being started in chrome. in case when browser window closed frame still opened/visible - click "run tests" once again , browser window opened again. seems when closing browser window - browser session id not valid more , i've got 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 class 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 browser"); } } return driver; }
and how call method in tests:
login = pagefactory.initelements(testbase.getdriver(), login.class); login.test1();
i have opinion need generate unique session id in beginning of tests, , kill in end. or may should check if chrome opened, , if no - start driver again, idk. please me solving or advice something. appreciate of or advice. thanks
so, after attempts i've got answer.
was added healthcheck method, checking if driver 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 chromedriver started singleton, i've added simple check 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 browser"); }
now, if browser accidentally closed user, program goes healthcheck() method, , reopen once again. in case "chrome not reachable"-exception gone.
Comments
Post a Comment