0
votes

I'm trying to run tests in parallel by using TestNg. I have 3 classes (two test classes which extend BaseTest class)

The BaseTest class has only "setup" stuff and "teardown", nothing else.

When i try to run tests in parallel like this, one class with tests runs and for other class only a browser is opened (but test isn't executed)

But!! when i cut code from BaseTest class and put it directly to each of my test classes (and thus not extending BaseClass) then the code works and tests run in parallel

why?? I'm not doing any code change at all... and when i wish to run tests not in parallel(just one by one), then using extend of BaseTest class works just fine

in testing.xml tried to put parallel="methods" "tests","true"...

below are my classes and testing.xml


public class BaseTest {

    public static WebDriver driver;
    public static WebDriverWait wait;

    @BeforeClass
    public static void setup() {
        System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir") + "\\src\\files\\geckodriver.exe");

        driver = new FirefoxDriver();

        wait = new WebDriverWait(driver, 10);

        driver.manage().window().maximize();
    }

    @AfterClass
    public static void teardown() {
        driver.quit();
    }
}

-----------------------
public class HomePageHotelsSearchTest extends BaseTest {

    @Test
    public void giveNameProperly() throws IOException {
        HomePage homePage = new HomePage(driver, wait);

        homePage.openUrl("https://www.phptravels.net/");

        homePage.openHotelsSearchTab();
        homePage.inputCityInHotelsSearch("London");
        homePage.selectCityFromSearchResult(0);
        homePage.inputCheckInDateInHotelsSearch("23/08/2018");
        homePage.inputCheckOutDateInHotelsSearch("27/08/2018");
        homePage.openPeopleAmountTabInHotelsSearch();
        homePage.addAdultsInHotelsSearch(1);
        homePage.startSearchingHotels();
        homePage.elementClickable(By.linkText("Grand Plaza Apartments"));

        String firstResult = homePage.readText(By.linkText("Grand Plaza Apartments"));
        Assert.assertEquals("Grand Plaza Apartments", firstResult);

        //Reporter.log("Test owned!", true);

    }

}

-----------------------
public class LoginTest extends BaseTest {

    @Test
    public void loginTest() throws InterruptedException, IOException {
        HomePage homePage = new HomePage(driver, wait);

        homePage.openUrl("https://www.phptravels.net/");


        LoginPage loginPage = homePage.goToLoginPage();
        loginPage.waitForPageToLoad("Login");
        loginPage.inputEmail("[email protected]");
        loginPage.inputPassword("demouser");

        loginPage.login();

        loginPage.waitForPageToLoad("My Account");

        String myAccountPageTitle = driver.getTitle();
        Assert.assertEquals(myAccountPageTitle, "My Account");

        //Reporter.log("Test owned!", true);


    }

}

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >
    <suite name="Suite" thread-count="2" parallel="true">
        <test name="test1">
            <classes>
                <class name="tests.LoginTest" />
                <class name="tests.HomePageHotelsSearchTest"/>
            </classes>
        </test>
    </suite>
2

2 Answers

1
votes

You have to define TestNG annotation in each class which you will call to Execute. You can not define @BeforeClass @AfterClass annotation in BaseTest. What you can call is BaseTest method. Refer your code with Example:

public class BaseTest {

    public WebDriver driver;
    public WebDriverWait wait;

    public void setup() {
        ....
    }

    public void teardown() {
        driver.quit();
    }
}

 public class HomePageHotelsSearchTest extends BaseTest {    

    @BeforeClass
    public void startTest(){
    setup(); 
    }

    @Test
    public void giveNameProperly() {
            ...
    }

    @AfterClass
    public void stopTest(){
    teardown();
    }
}

You can directly call BaseTest methods without object, As you have extend BaseTest class.


 public class HomePageHotelsSearchTest {

    BaseTest testObj = new BaseTest();

    @BeforeClass
    public void startTest(){
    testObj.setup(); 
    }

    @Test
    public void giveNameProperly() {
            ...
    }

    @AfterClass
    public void stopTest(){
    testObj.teardown();
    }    
 }

If you not extending BaseTest class, You need to call those method with reference to object.

Thus, in each class there must be @BeforeClass and @AfterClass annotation, So TestNG will call it before @Test execution.

0
votes

Thank you all for quick replies, it's working now :-)

Well, basically now BaseTest class has only methods to initiate/create webdriver objects and start browser session. Later test classes have their own instances of webdriver and use those methods to initiate them