Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium's driver.get() does not work under function composition

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.

like image 847
haz Avatar asked Oct 29 '22 02:10

haz


1 Answers

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

like image 105
Tarun Lalwani Avatar answered Nov 09 '22 22:11

Tarun Lalwani