Search code examples
ui-automationkaratebrowser-automation

BeforeStep Karate


I'm working with iFramed web(micro frontend), the iFrame wraps the entire body(element inside it), and I need to switch iFrame every time the page loads.

Given driver siteUrl
And call read('classpath:web/pages/login/login.feature@login_with_email') args
* def args = {main_menu: fleet-management, submenu: Truk}
And call read('classpath:web/internal/pages/dashboard/dashboard.feature@open_page') args
# LOAD NEW PAGE
* waitFor(iFrameId).switchFrame()
And click(pageA.elementA)
# LOAD NEW PAGE
* waitFor(iFrameId).switchFrame()
And click(PageB.elementB)

Since switchFrame will be used frequently, beforeStep will be a great solution. I've done research :

Hook.java

    @Override
    public boolean beforeStep(Step step, ScenarioRuntime sr)
    {
        Runner.runFeature("classpath:utils/helper.feature@switch_frame", null, false);
    }

helper.feature

Feature: Test Feature For Switch Frame
  @switch_frame
  Scenario: Switching focus to iFrame
    * waitFor(iFrameId).switchFrame()

but right now I'm stuck, helper.feature called from hook.java unable to use any keywords(waitFor, switchFrame) and unable to get global variable set from karate-config.js.

Please help, Thanks!!


Solution

  • No, I think this is over-engineering and Runner.runFeature() is not designed to do this.

    How about you combine the frame switching and click into a single function:

    * def frameClick = function(frame, locator){ waitFor(frame).switchFrame(); click(locator) }
    

    Now you can do:

    * frameClick(iFrameId, pageA.elementA)
    

    You can even be smarter and set the frameId one-time:

    * frameClick(pageA.elementA)