Hot questions for Using Cucumber in selenide

Question:

Is there some way to get these cucumber report values inside code in some variables?

For example:

int scenariosRun = cucumber.getScenarios(); // 8
int scenariosPassed = cucumber.getScenariosPassed(); // 8


Answer:

Yes, you can get current scenario details in the after/before hook as given below,

@Before
public void before(Scenario scenario) {
    System.out.println("------------------------------");
    System.out.println("Starting - " + scenario.getName());
    System.out.println("------------------------------");
}

@After
public void before(Scenario scenario) {
    System.out.println("------------------------------");
    System.out.println(scenario.getName() + " Status - " + scenario.getStatus());
    System.out.println("------------------------------");
}

Question:

I can't add screenshot of failed cucumber step under the step in Allure report.

I use allure-cucumber4-jvm:2.12.1, cucumber-java:4.3.1 and try to add screenshot of tested web app to allure report EXACTLY under failed cucumber step. So I tried to use cucumber's @AfterStep and @After annotations and add screenshot in annotated methods under scenario.isFailed() conditions. But the problem is that screenshots are added after all steps in Tear Down section. The only difference is that @After method is called once and @AfterStep methods is called by every step.

@AfterStep
public void addScreenshotOnFailed(Scenario scenario) throws IOException {
        if (scenario.isFailed()) {
            try {
                Allure.addAttachment("Failed step", new FileInputStream(Screenshots.takeScreenShotAsFile()));
            } catch (FileNotFoundException ignored) {}
        }
    }

I expect screenshot of tested web app exactly under failed cucumber step? but actually I have it in Tear Down section after all scenario


Answer:

Idea is using the events to capture the step status and embed the screenshot. If I am correct, As per current design AfterStep does not execute when the step is failed. So we have to use AfterConfiguration in hooks.

Ruby: Here is the ruby implementation.

AfterConfiguration do |scenario, config|
    config.on_event :test_step_finished do |event|
        if event.result.failed?
          # get screenshot
        end
    end
  end

Check this thread regarding the allure implementation.

Question:

I am using Cucumber with Selenide for my UI tests and I have these methods

public static void initPage(String pageName) throws Exception {
    Set<Class<?>> annotated = new Reflections(PAGES_PACKAGE).getTypesAnnotatedWith(PageTitle.class);

    for (Class classToInit : annotated) {
        PageTitle annotation = (PageTitle) classToInit.getAnnotation(PageTitle.class);
        if (annotation.name().equals(pageName)) {
            classToInit.newInstance();
            lastInitialized = substringAfter(classToInit.toString(), "class ");
            lastInitClass = classToInit;
            return;
        }
    }

    throw new AutotestError("Could not find page to init: " + pageName);
}

public static SelenideElement findElementByTitle(String elementName) throws IllegalAccessException, InstantiationException {
    Set<Field> annotated = new Reflections(lastInitialized, new FieldAnnotationsScanner()).getFieldsAnnotatedWith(ElementTitle.class);

    for (Field field : annotated) {
        ElementTitle annotation = field.getAnnotation(ElementTitle.class);
        if (annotation.name().equals(elementName)) {
            field.setAccessible(true);
            SelenideElement el = (SelenideElement) field
            return el;
        }
    }
    throw new AutotestError("Element not found: " + elementName);
}

I am very new to reflections and am trying to build a page objects pattern utilising the org.reflections.Reflections library to search for annotated fields in various page object classes. I am, however, having problems returning the SelenideElement from the field I'm getting in the second method (line SelenideElement el = ... is plain wrong at the moment). How do I get the field I can use as a SelenideElement (with @ElementTitle and @FindBy annotations) in my test? Thanks in advance.


Answer:

You should change line SelenideElement el = (SelenideElement) field to SelenideElement el = ((SelenideElement) field.get(pageObject))

Explanation

According to documentation of Field.get: Returns the value of the field represented by this Field, on the specified object. The value is automatically wrapped in an object if it has a primitive type.