Selenium的driver.get()在函数组合下不起作用

时间:2017-09-07 14:07:05

标签: javascript selenium functional-programming ramda.js

当我尝试将其与其他功能组合时,调用driver.get(url)会导致错误。以下是我的小功能方法:

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)

以下最小示例加载Google,并将消息写入搜索栏。这有效:

// 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);

但是我希望在功能组合中加入loadGoogle - 它更整洁,更正确'。像这样:

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

但我收到driver.findElement is not a function错误:

/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)

我认为这是因为loadPage没有返回WebDriver个实例,但我不确定,并且不知道如何修复它。

1 个答案:

答案 0 :(得分:1)

您需要更改

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

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

发生错误是因为driver.get(url)返回一个promise,而不是WebDriver实例。由于合成中的其他函数接受WebDriver实例作为参数,因此会导致错误