selenium - How can I contain test members, under a TestNG test, in their own thread? -
selenium - How can I contain test members, under a TestNG test, in their own thread? -
i attempting run selenium tests in parallel using testng runner. parallel requirement set in testng.xml file so.
<test name="smoke tests" parallel="methods" thread-count="2">
the problem have want "quit" browser after each test. start new browser , run next test. has worked me until tried run tests in parallel. seems methods share threads , if quit browser thread gone. testng tries run next test on thread quit , sessionnotfoundexception error.
i have tried parallel="tests" doesn't work , tests run sequentially instead of in parallel. there way run each test in new thread , not reuse threads or out of luck?
here @beforemethod
private static threadlocal<webdriver> driverforthread; @beforemethod public static void setupmethod() { log.info("calling setup before method"); driverforthread = new threadlocal<webdriver>() { @override protected webdriver initialvalue() { webdriver driver = loadwebdriver(); driver.manage().window().maximize(); homecoming driver; } }; }
and @aftermethod
@aftermethod public static void aftermethod() { getdriver().quit(); } public static webdriver getdriver() { homecoming driverforthread.get(); }
here sample test
@test public void shouldbeabletogetsessionidtest() { login = new loginpage(getdriver()).get(); home = login.loginwithouttelephony(username, password); string sessionid = home.getsessionid(); assert.assertnotnull(sessionid); log.info("the session text is: " + sessionid); }
testng has threading built in methods annotated as@test
. when ran problem, because wasn't threading webdriver. xml configuration specifies parallel="methods"
correct.
at class level, declare 2 threadlocal objects, 1 type webdriver , other type desiredcapabilities:
threadlocal<desiredcapabilities> capabilities = new threadlocal<desiredcapabilities>(); threadlocal<webdriver> driver = new threadlocal<webdriver>();
then, in @beforemethod
, you'll need initialize both new instance of appropriate types, configure, , start webdriver.
capabilities.set(new desiredcapabilities()); capabilities.get().setbrowsername("firefox"); driver.set(new remotewebdriver(new url("url of grid hub"), capabilities.get()));
you'll need either add together throws declaration method or wrap driver.set()
in try/catch.
at end of test, thread close , along it, desiredcapabilities , webdriver. if want close these manually, though, can in @aftermethod
appropriate calls (see threadlocal javadoc).
this produce setup start each @test
method in fresh browser closed @ conclusion of execution.
as aside, desiredcapabilities provides wealth of configuration options test environment. options such specifying browser, browser version, os platform, , browser configuration (such disabling js, etc) supported. using this, grid run test on node supports given criteria.
hopefully you'll find helpful. ran exact problem describe , solved me.
selenium webdriver testng selenium-grid
Comments
Post a Comment