2

So I creating a scrolling function which looks like this

Feature: Dynamic page scrolling
Scenario: Scrolling function  
* def ScrollHeight = function(){return script("document.body.scrollHeight")
* def ScrollFunction = function(){ script("window.scrollTo(0,document.body.scrollHeight)")
* def ScrollingFunction = 
"""
function(){
var height = ScrollHeight()
while(true)
{ ScrollFunction()
var newHeight = ScrollHeight() 
if(height  === newHeight )
break;
height = newHeight 
} 
}
"""

So I created this in the same feature file in which I am having my test scenarios but I want to make this as a centralized function into another feature file from where I can use it in other feature files also. But I will not be able to call ScrollHeight and ScrollFunction from other feature files?

This is how I am trying this to create it in a new feature file Scrolling.feature

Feature: Dynamic page scrolling Function
Scenario: Scrolling function  
*def obj = read('classpath:Testing.feature')
* def ScrollingFunction = 
"""
function(){
var height = obj.ScrollHeight()
while(true)
{ obj.ScrollFunction()
var newHeight = obj.ScrollHeight() 
if(height  === newHeight )
break;
height = newHeight 
} 
}
"""

And my Testing.feature file looks like

Feature: Testing
Background:
* def ScrollHeight = function(){return script("document.body.scrollHeight")
* def ScrollFunction = function(){ 
script("window.scrollTo(0,document.body.scrollHeight)")}
Scenario: Test-1
* def fun = call read('classpath:Scrolling.feature')
* call fun.ScrollingFunction 

But it does n't work for me

2 Answers 2

1

All the methods such as script() are available only after a driver has been instantiated by default. So make sure you declare any re-usable functions after the driver <url> step.

Else you have to do something like this for re-usability:

   * def getScrollHeight =
   """
   function() {
     var driver = karate.get('driver');
     return driver.script("document.body.scrollHeight");
   }
   """

If this is not working, please create a quickstart sample so that we can look into it: https://github.com/intuit/karate/tree/develop/examples/ui-test

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your time but that is not giving any error but is not doing the scrolling also @peter
@Ajex so follow the instructions
Sure, but can you please conform that obj.ScrollHeight() is it the correct way to call karate function in js code? @peter
I don't know. I have used other JS not this. why don't you just scroll to some element or div using developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView
I tried this way but it doesn't worked in my case.Thanks for your suggestion.
1

You don't need to reference another function. It can be done in the same script.

This works for me:

Feature: Search Posts

  Background:
    * configure driver = { type: 'chrome', start: false }

    # build search endpoint
    * def buildSearch =
      """
      function() {
        var keys = 'fake%20news';
        var filter = 'eyJycF9jcmVhdGlvbl90aW1lIjoie1wibmFtZVwiOlwiY3JlYXRpb25fdGltZVwiLFwiYX'+
                     'Jnc1wiOlwie1xcXCJzdGFydF95ZWFyXFxcIjpcXFwiMjAyMFxcXCIsXFxcInN0YXJ0X21v'+
                     'bnRoXFxcIjpcXFwiMjAyMC0xXFxcIixcXFwiZW5kX3llYXJcXFwiOlxcXCIyMDIwXFxcIi'+
                     'xcXFwiZW5kX21vbnRoXFxcIjpcXFwiMjAyMC0xMlxcXCIsXFxcInN0YXJ0X2RheVxcXCI6'+
                     'XFxcIjIwMjAtOC0xMlxcXCIsXFxcImVuZF9kYXlcXFwiOlxcXCIyMDIwLTgtMTJcXFwifVwifSJ9';
        
        return 'https://m.facebook.com/search/posts/?q='+keys+'&epa=FILTERS&filters='+filter;
      }
      """

  Scenario: facebook search

    # Scroll down until no more pager
    # return null to keep looping
    * def fecthPage =
      """
      function() {
        var results = script("window.scrollTo(0, document.body.scrollHeight); document.querySelectorAll('#see_more_pager').length")
        return results == 0 ? results : null;
      }
      """

    # Print Post information
    * def permalinks = []
    * def getPost =
      """
      function(x, y, z) {
        var feat = x.script("_.getAttribute('data-ft');");
        var store = x.script("_.getAttribute('data-store');");
        if (feat && store) {
            feat = karate.toJson(feat);
            store = karate.toJson(store);
            karate.appendTo(permalinks, {'Features': feat, 'Store': store});
        }
      }
      """

    Given driver buildSearch()

    * waitUntil(fecthPage)
    * def posts = locateAll('//div[@data-store][@data-ft]')
    * karate.forEach(posts, getPost)
    * print "Posts:", permalinks

To avoid chrome profiles eating all my disk, I start it before karate.
That's why start: false

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.