Forum Discussion

mchelikani's avatar
mchelikani
Contributor
12 years ago

[Resolved] Add Assertions to TestStep in Groovy

can anyone please help to provide a sample code to add assertions to teststep manually using groovy.

I would like to Add "XPath Match" and "XPath Contains" Assertions

Thanks in Advance!!
  • Cizo89's avatar
    Cizo89
    Frequent Contributor
    Hi mchelikani,

    try this script, it should help you:

    def xpathAssertion = testRunner.testCase.testSteps["name_of_your_step"].addAssertion("XPath Match")
    xpathAssertion.setPath("your_xpath")
    xpathAssertion.setExpectedContent("expected_result")
    xpathAssertion.setName("your_required_name_xpath")

    def containsAssertion = testRunner.testCase.testSteps["name_of_your_step"].addAssertion("Contains")
    containsAssertion.setToken("your_expected_string")
    containsAssertion.setName("your_required_name_contains")


    Regards,
    Marek
  • Thank you for the Reply!

    Does "Contains" check for the Token in only the Value of Elements or Attributes or It even Checks the Elements and Attributes also?

    I would like to check if an XPath Present(Exists) in Response( don't care about the value). Which Assertion Should I use and what is the groovy syntax?
  • Cizo89's avatar
    Cizo89
    Frequent Contributor
    Hi,

    if you want to use the XPath Present(Exists) function, I'd say that XPath Match assertion would be better than simple Contains Assertion.

    Use the script for creating XPath Assertion with these modifications:

    xpathAssertion.setPath("exists(your_xpath)")
    xpathAssertion.setExpectedContent("true")


    You just have to put your XPath into function exists(), that should work fine for your requirements

    Regards,
    Marek