testproject-io / java-opensdk

TestProject Java OpenSDK
Apache License 2.0
50 stars 42 forks source link

Cucumber Framework - Session reuse not working as expected when recreating the driver in each scenario #86

Closed gileli121 closed 3 years ago

gileli121 commented 3 years ago

java/MyStepdefs.java

import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import io.testproject.sdk.drivers.GenericDriver;
import io.testproject.sdk.drivers.web.ChromeDriver;
import io.testproject.sdk.internal.exceptions.AgentConnectException;
import io.testproject.sdk.internal.exceptions.InvalidTokenException;
import io.testproject.sdk.internal.exceptions.ObsoleteVersionException;
import org.openqa.selenium.By;
//import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.Assert;

import java.io.IOException;

public class MyStepdefs {

    private final ChromeDriver driver;
    public MyStepdefs() throws InvalidTokenException, AgentConnectException, ObsoleteVersionException, IOException {
        driver = new ChromeDriver();
    }

    @Given("Navigate to TestProject")
    public void given_header() {
        driver.navigate().to("https://example.testproject.io/");

    }

    @When("Login with right password")
    public void when_header() {
        driver.findElement(By.cssSelector("#name")).sendKeys("Gil");
        driver.findElement(By.cssSelector("#password")).sendKeys("12345");
        driver.findElement(By.cssSelector("#login")).click();
    }

    @When("Login with wrong password")
    public void login_with_wrong_password() {
        driver.findElement(By.cssSelector("#name")).sendKeys("Gil");
        driver.findElement(By.cssSelector("#password")).sendKeys("11111");
        driver.findElement(By.cssSelector("#login")).click();
    }

    @Then("Login should success")
    public void then_header() {
        String result = driver.findElement(By.xpath("//*[@id=\"greetings\"]")).getText();
        Assert.assertEquals(result,"Hello Gil, let's complete the test form:");
    }

}

resources/Features/SomeFeatureTest.feature

Feature: Log in (failed and success)
  Scenario: Success Login
    Given Navigate to TestProject
    When Login with right password
    Then Login should success

  Scenario: Failed Login
    Given Navigate to TestProject
    When Login with wrong password
    Then Login should success

Issue If you run the whole feature at once, you will get 2 jobs. Each job have another scenario. The expected result is to get in report one job with name of the feature "Log in (failed and success)" and 2 tests - "Success Login" and "Failed Login".

This expected behavior is happening in python-opensdk but not in java-opensdk

Note Changing the code:

    private final ChromeDriver driver;
    public MyStepdefs() throws InvalidTokenException, AgentConnectException, ObsoleteVersionException, IOException {
        driver = new ChromeDriver();
    }

to

    private static ChromeDriver driver = null;

    public static ChromeDriver getDriver() throws InvalidTokenException, AgentConnectException, ObsoleteVersionException, IOException {
        if (driver == null)
            driver = new ChromeDriver();

        return driver;
    }

Will fix the issue. But this is not a solution in case you want to reopen the driver before each scenario/test.

DavidG8168 commented 3 years ago

99