How to get Selenium and TestNG to open one browser to run tests in multiple classes
I am using Selenium with TestNG to test a website. I have created tests using the Selenium IDE and exported them to TestNG with each test being a method in a class. Eg,
For login tests there is a Login class which has methods testLogin(), testLogin2() etc For signup tests there is a Signup class has methods testSignup(), testSignup2(), etc
I am using Ant to run the tests which works fine except that each class will open up a browser and then run its methods, eg, if I have five classes, then five browsers will open simultaneously and then run the tests.
What I want is to get Ant/Selenium/TestNG to just open up one browser and then run all the tests (in the same browser) in all the classes that I have specified in testng.xml. Using the example above, I want one browser to open then run testLogin(), testLogin2(), testSignup(), testSignup2(). If this cannot be achieved, then I want to open a browser, run all tests in a class then close the browser then open another browser then run the set of test methods in the next class.
Any help appreciated. Thanks in advance.
---------------------
Today I have found the answer that works for me. Give me a few minutes to gather all code samples :)
Init.java
//base class that will be called before all tests @Test(groups = "init") public class Init{ DefaultSelenium browser; public void start(ITestContext itc){ browser = (DefaultSelenium) itc.getAttribute("browser"); browser.open("url"); browser.click("xpath"); } }
TemplateForClasses.java
/* - all public methods will be tests * - all tests will be called after group init * - just before suite will start we will start 1 browser instance */ @Test(dependsOnGroup="init") public class TemplateForClasses{ DefaultSelenium browser; @BeforeSuite public void startBrowser(ITestContext itc){ browser = new DefaultSelenium(host,port,browser_type,url); itc.setAttribute("browser",browser); browser.start(); } @AfterSuite public void stopBrowser(ITestContext itc){ browser = (DefaultSelenium) itc.getAttribute("browser"); browser.stop(); } //any other: @Before, @After methods }
FirstGroupOfTests.java
//all tests classes will inherit preferences set in TemplateForClasses public class FirstGroupOfTests extends TemplateForClasses{ public void FirstTest(ITestContext itc){ browser = (DefaultSelenium) itc.getAttribute("browser"); //browser.click("start"); } }
idea:
- start browser just once have tests
- that run before every other tests(isBrowserRunning)
- refer to browser from single test
This code was tested but currently I took it from the top of my head so possibly I will edit it tomorrow to make it more exact.
Update: This result is based on testng.org documentation + some questions asked by me on stackoverflow + some answers found on several forums/groups
I must add I'm running testng programatically and I'm generating xml on the fly (as it is done on documentation.org). I am using it all in one package, I added package to the xml, included only classes Init + the ones that inherit from TemplateForClasses. If you need that xml, let me know.
No comments:
Post a Comment