Hot questions for Using Cucumber in serenity bdd

Top Java Programmings / Cucumber / serenity bdd

Question:

My feature files in my cucumber project always worked until now. It seems like the Strings I pass, aren't recognized anymore?

I used to do this:

 @When("I set the local storage vars {string}, {string}, {string}")

Answer:

It is because of the new version of cucumber(5.0.0). If you are using IntelliJ, just update the plugins and the IntelliJ itself. From cucumber version 4.x.x you can use the below format to pass any kind of data types from your feature file and use it in whichever data type required. Below is the sample.

Feature File:

When I set the local storage vars a, b, c

Step Definition File:

@When("I set the local storage vars (.*), (.*), (.*)") public void iSetTheLocalStorageVars(String a, String b, String c) { }

Question:

I am writing scenario test cases in cucumber and I want to check if all the elements of a PageObject are valid and selenium can interact with them, before I run the test. I want to avoid running my multi-step, long test case just to get to the last page and get an exception that an element is not found. I wanted to be able to test my PageObject after I write it, so I can be sure that all the elements are reachable (before running the long test..).

The website I am working on does not have consistent id tags and using @FindBy() sometimes takes some playing around with. I am trying to make a simple process of:

  1. Navigate to the PageObject url
  2. check if all elements in the PageObject are reachable by selenium

Do you know of a way to access all the PageObjects elements with out making a method for each one? A method that initializes all the elements?


Answer:

After trying a number of different ways, I used reflection within the class to pull all fields, and then used .isPresent(); to check if the element is on the page. This solution requires this method to be placed in each class that you want to test, which I was trying to avoid.

Solution:

SuiteRunner
@RunWith(CucumberWithSerenity.class)
@CucumberOptions(features = "src/test/resources/features/checkElems")
public class RegressionTestSuite {}
Feature File: CanFindElems.feature
Feature: Check if WebPage Object elements can be found

  Scenario: Check if the page's elements are reachable
    Given navigate to webpage
    Then check page elements
Test Class
public class CanFindElemsTest {

  // Change the class
  ClassYouAreTesting page;

  @Given("^navigate to webpage$")
  public void navigate_to_webpage() throws Exception {

    page.open();
  }


  @Then("^check page elements$")
  public void check_page_elements() throws Exception {

    page.checkPageElementsExist();
  }
}
Page Class
public class SomeWebPage extends PageObject {

  @FindBy(id = "someID")
  private WebElementFacade someElement;

  @FindBy(linkText = "some text")
  private WebElementFacade someLink;

  @FindBy(className = "some-class")
  private WebElementFacade anotherElement;


  public void checkPageElementsExist() throws Exception {

    Field[] allFields = getClass().getDeclaredFields();
    for (Field field : allFields) {

      if (field.get(this) instanceof WebElementFacade) {

        WebElementFacade f = (WebElementFacade) field.get(this);

        if (!f.isPresent()) {

          System.out.println("ElementNotFound: " + field.getName());
        } else {

          System.out.println("Found: " + field.getName());
        }
      }
    }
  }

}

Question:

I add log4j.properties to my project but serenity does not want to write all steps he performed on console. Could you please help me?

log4j.rootLogger=INFO, file, stdout

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=Test.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
#log4j.appender.file.Append=false
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

Here is my log4j config. but I can not see any of steps from my .feature file showed up on console or Test.log file.


Answer:

Take a look at Serenity BDD properties here.

There are serenity.verbose.steps and serenity.logging properties which you might find useful.