How to get Selenium and TestNG to open one browser to run tests in several classes

I am using Selenium with TestNG to test a website. I created tests using the Selenium IDE and exported them to TestNG, each of which was a method in the class. For example,

For login tests, there is a Login class that has the methods testLogin (), testLogin2 (), etc. For registration tests, there is a Registration class that has the methods testSignup (), testSignup2 (), etc.

I use Ant to run tests that work fine, except that each class will open a browser and then run its methods, for example, if I have five classes, then five browsers will open at the same time, and then run the tests.

I want to get Ant / Selenium / TestNG to just open one browser and then run all the tests (in the same browser) in all the classes that I specified in testng.xml. Using the above example, I want to open one browser, then run testLogin (), testLogin2 (), testSignup (), testSignup2 (). If this cannot be achieved, I want to open a browser, run all the tests in the class, and then close the browser, and then open another browser, and then start the set of test methods in the next class.

Any help appreciated. Thanks in advance.

+3
source share
3 answers

Today I found an answer that works for me. Give me a few minutes to collect all the 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");
    }

}

:

  • (isBrowserRunning)

, , , , , .

: testng.org + , stackoverflow + /

, testng , xml ( documentation.org). , xml, Init +, TemplateForClasses. xml, .

+2

. . :

Class A (contains the code selenium.start();)
|
|(inherited classes)
|--------class B   }
|--------class C   }  Have some @Test methods
|--------class D   }

, , selenium.start(); , .

- , B , , C ..

, , , start() - B, C D.

, . start(); ( ) . (); .

= >

Class A, Class B
Class A, Class C
Class A, Class D

, selenium 1 start() 1 stop() , , .

start() A B, C D, 3 .

start() stop() B, C D, , , . , C, ..

, .: -)

0

Spring. init factory. , Selenium , . selenium.someMethod() . helper.goToSomePage() preferencesPage.changePassword(....).

, Selenium , , , . , Selenium . , .

For information, Spring is a Java framework, and dependency injection is only part of it. Of course, other DI frameworks such as Guice can also be used.

0
source

Source: https://habr.com/ru/post/1712964/


All Articles