Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
1.7k views
by Contributing Tester (60 points)
edited by
In Cucumber I have set up the following step:

   

    Then("^(\\d+) pages should show on the (\\S+\\.page) in the related news widget$", (Integer outcome, String currentPage) -> {

        LOG.info("scenario 3 --- " + outcome + " pages should show on the  " + currentPage);

        assertThat(parse(xml), hasXPath("/Collection/Root/Group/NewsRelease", equalTo(outcome.toString())));

    });

    

The result of this test is:

    java.lang.AssertionError:

    Expected: an XML document with XPath Collection

         but: was <[#document: null]>

        at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)

        at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)

        at uk.ac.soton.livesite.cucumber.RelatedNewsSteps.lambda$new$1(RelatedNewsSteps.java:48)

        at ✽.Then 3 pages should show on the /news/2018/04/key-area-of-immune-cell.page in the related news widget(creating-news-articles/related_news.feature:14)

I have set up a Unit test with similar logic that passes the test
by Contributing Tester (60 points)
I have created a SimpleTest.java with the same logic and the test passes:

XML file contents

    <Collection>
      <Root>
           <Group>
                <NewsRelease>
                    <EditorNotes />
                </NewsRelease>
                <NewsRelease>
                    <EditorNotes />
                </NewsRelease>
                <NewsRelease>
                    <EditorNotes />
                </NewsRelease>
            </Group>
        </Root>
    </Collection>

Test:

import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import uk.ac.soton.mockery.MockitoExtension;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.*;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.xml.HasXPath.hasXPath;

/**
 * Junit5 Simple Test.java
 */
@ExtendWith(MockitoExtension.class)
@RunWith(JUnitPlatform.class)
public class SimpleTest {

    private static final Logger LOG = LoggerFactory.getLogger(uk.ac.soton.cucumber.SimpleTest.class);

    private static InputSource xmlSource;

    @BeforeAll
    static void initializeExternalResources() throws IOException {
        getXMLSource();
    }

    private static void getXMLSource() {
        try {
            xmlSource = new InputSource(
                    new InputStreamReader(
                    new FileInputStream(
                    new File("/Users/arnout/dev/build/result.xml")), "UTF-8"));
            xmlSource.setEncoding("UTF-8");
        } catch (IOException e) {
            LOG.error("Exception reading File ", e);
        }
    }

    @Test
    @DisplayName("Given the database is down, result is generator stops directly and returns returns XML with error attribute.")
    void givenNullStatementResultIsEmptyXMLObject() throws Exception {
        assertThat(parse(xmlSource) , hasXPath("/Collection/Root/Group/NewsRelease",equalTo("3")));
    }

    private static Document parse(InputSource xml) throws Exception {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(false);
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        return documentBuilder.parse(xml);
    }
}
by Master (1.2k points)
can you post steps on your .feature file?

1 Answer

1 like 0 dislike
by Contributing Tester (60 points)
selected by
 
Best answer

Here is the feature file:

@news @article @widget @feed
Feature: A related news widget featured on the bottom of a news page.

  (Feature Injection template)

  In order to publish related news along the news article page
  As a business user
  I want to add a feed subscription to a news article to display related article blocks
  in a related news widget on the page.

  @multiple @articles
  Scenario Outline: When the feed has an amount of articles subscribed, the same amount of articles should display in the related news widget.
    When <Feed> has <Count> articles subscribed
    Then <Outcome> pages should show on the <Current Page> in the related news widget
    And  <Current Page> should not show in the related news widget

    Scenarios:
      | Feed                            | Current Page                               | Count | Outcome |
      | /home/news/education-highlights | /news/2018/04/key-area-of-immune-cell.page | 3     | 3       |
by Contributing Tester (60 points)
I have the test working now. I am now running the following :

        When("^(\\S+) has (\\d+) articles subscribed$", (String feed, Integer count) -> LOG.info("scenario 3 --- number of articles subscribed to feed " + feed + " is " + count));

        Then("^(\\d+) pages should show on the (\\S+\\.page) in the related news widget$", (Integer outcome, String currentPage) -> {
            LOG.info("scenario 3 --- " + outcome + " pages should show on the  " + currentPage);
            try {
                Node group = getNode("/Collection/Root/Group");
                assertThat(group.getChildNodes().getLength(), equalTo(outcome.toString()));
            } catch (Exception e) {
                LOG.error("Exception handling XML source", e);
            }
        });
by Master (1.2k points)
thank you for posting your solution.


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

1.4k questions

1.6k answers

866 comments

1.9k users

...