Cucumber + Selenium Java: keep browser open between test cases

I am currently using Cucumber and Selenium WebDriver in Java to test a web application. I am not very happy that the browser is closed and reopened between each test case.

Firstly, it is rather slow and, in addition, it makes no sense to me.

What I want to do is exit the application between each test and leave the browser open.

I thought this line could do the job, but it does not work properly:

driver.navigate().to("http://myurl.url");

Instead:

driver.get("http://myurl.url");

He opens a new browser. I understand why he does this, but I want to connect to the previous session of my browser.

+4
source share
3 answers

. Singleton Design Pattern picocontainer. static .

public class Drivers {
    private static boolean initialized = false;
    private static WebDriver driver;

    @Before
    public void initialize(){
        if (!initialized){
            initialized = true;
            driver = new FirefoxDriver();
            driver.get("http://myurl.url");
        }
    }
    public static WebDriver getDriver(){
        return driver;
    }   
}

StepDefinitions , my Singleton.

public class DashboardSteps extends SuperSteps {
    Drivers context;

    @After
    public void close(Scenario scenario) {
        super.tearDown(scenario);
        loginPage = homePage.clickLogout();
        loginPage.waitPageLoaded();
    }

    public DashboardSteps(Drivers context) {
        super(context);
    }

SuperSteps:

public class SuperSteps {
    protected Drivers context;

    public SuperSteps(Drivers context){
        this.context = context;
    }

, , .

+7

m ur @Before setUp, @After tearDown. , tearDown , .

0

You can use annotations @BeforeClassand @AfterClass. They work before @Before and after @After respectively. Assuming that the test cases you are trying to test are in the same class, you can initialize yours WebDriverin the method @BeforeClassand close it in the method @AfterClass.

0
source

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


All Articles