5

Calling driver.get(url) causes errors when I try to compose it with other functions. Here are my small functional methods:

const webdriver = require('selenium-webdriver');
const By        = webdriver.By;
const R         = require('ramda');

// Load a webpage
const loadPage = url => driver => driver.get(url)

// Find an WebElement via some locator
const getElement = locator => driver => driver.findElement(method)

// Locator by name
const byName = name => By.name(name)

// Send a series of input keys to a WebElement
const sendKeys = keys => elem => elem.sendKeys(keys)

The following minimal example loads Google, and writes a message to the search bar. This works:

// Navigate to the Google webpage
const loadGoogle = loadPage('http://google.com')

// Retrieve the search form element
const getSearchForm = getElement(byName('q'))

const driver = new webdriver.Builder().forBrowser('chrome').build();
loadGoogle(driver); // NOTE: I have to do this seperately -- cannot do it inside the composition

var app = R.compose(sendKeys('search input'), getSearchForm)
app(driver);

But I wish to include loadGoogle within the function composition -- it'd be neater, and more 'correct'. Like so:

var app = R.compose(sendKeys('search input'), getSearchForm, loadGoogle)
app(driver);

But I get an driver.findElement is not a function error:

/Users/name/Desktop/functional-test.js:9
const getElement = locator => driver => driver.findElement(locator)
                                               ^

    TypeError: driver.findElement is not a function
        at driver (/Users/name/Desktop/functional-test.js:9:48)
        at /Users/name/node_modules/ramda/src/internal/_pipe.js:3:14
        at /Users/name/node_modules/ramda/src/internal/_pipe.js:3:27
        at /Users/name/node_modules/ramda/src/internal/_arity.js:5:45
        at Object.<anonymous> (/Users/name/Desktop/functional-test.js:28:1)
        at Module._compile (module.js:541:32)
        at Object.Module._extensions..js (module.js:550:10)
        at Module.load (module.js:458:32)
        at tryModuleLoad (module.js:417:12)
        at Function.Module._load (module.js:409:3)

I assume it's because loadPage is not returning a WebDriver instance, but I'm unsure, and don't know how to fix it.

haz
  • 2,034
  • 4
  • 28
  • 52

1 Answers1

2

You need to change

const loadPage = url => driver => driver.get(url) 

to

const loadPage = url => driver => { driver.get(url) ; return driver; }

The errors occurred because driver.get(url) returns a promise, rather than a WebDriver instance. Since the other functions in the composition accept a WebDriver instance as a parameter, it caused errors

Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
  • This fixes it! The errors occured because `driver.get(url)` [returns a promise](http://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/index_exports_WebDriver.html), rather than a `WebDriver` instance. Since the other functions in the composition accept a `WebDriver` instance as a parameter, it caused errors. Thanks! – haz Sep 08 '17 at 06:37
  • 1
    @user3668541, Thanks updated the explanation your wrote :-) – Tarun Lalwani Sep 08 '17 at 06:49