fearghal O reilly
fearghal O reilly

Reputation: 140

How to restart browser after test when using data providers

Im currently using a Data Provider to enter numerous different users, but when the tests ends then the previous user is still logged in, how do make the bowser reset after each run? Heres my code:

public class login_from_login_page extends ConditionsWebDriverFactory {

    public static final int TEST_CASE_PASSED_STATUS = 1;
    public static final int TEST_CASE_FAILED_STATUS = 5;


    @Test(dataProviderClass = Data.DataProviders.class, dataProvider = "customer")
    public void LoginActionTest (String pass, String email, String user) throws Exception{

        Header header = new Header();
        header.guest_select_login();
        Pages.Login login = new Pages.Login();
        login.LoginAction(pass, email, user);


        //TestRail.addResultForTestCase("16870",TEST_CASE_PASSED_STATUS," ");
        //TestRail.addResultForTestCase("16874",TEST_CASE_PASSED_STATUS," ");
        //TestRail.addResultForTestCase("17199",TEST_CASE_PASSED_STATUS," ");
    }
}

I have a Webdriverfactory class that is extended from my test class here but its doesn't seem to be creating a new instance, im always still logged in when it restarts using the new DataProvider information.

@Listeners({ScreenShotOnFailListener.class})

public class ConditionsWebDriverFactory {

    @BeforeClass
    public void beforeTest() {
        Drivers.startBrowser(true);
        Drivers.getDriver().manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    }

    @AfterClass (alwaysRun = true)
    public void afterTest() {
        Drivers.finishBrowser();
    }
}

Upvotes: 2

Views: 497

Answers (2)

AutomatedOwl
AutomatedOwl

Reputation: 1089

Start your test with initialization before test execution:

@BeforeMethod
void setupTest() {
  WebDriver driver = new ChromDriver();
  // method code here
}

Alternatively initialize your page object with new driver instance:

Pages.Login login = new Pages.Login(new ChromDriver());

Another alternative:

@Test
void seleniumTest() {
  // Test code here
  login.getDriver().manage().deleteAllCookies();
  login.getDriver().navigate.refresh();
}

Upvotes: 1

Sergiy Konoplyaniy
Sergiy Konoplyaniy

Reputation: 408

You can add after method, if you use testng:

   @AfterMethod
void deleteCoockies() {
  Drivers.getDriver().manage().deleteAllCookies();
  Drivers.getDriver().get ("homePageUrl");
}

Upvotes: 0

Related Questions