我似乎无法弄清楚导致typeError的原因或原因。基本上我所要做的就是获取当前所选帧的名称。下面是代码和错误。
this.getCurrentFrameName = function(driver)
{
var frames = driver.findElements(driver.By.css('frame'));
for (var frame in frames)
{
if (frame.isSelected()) {
return frame.getName;
}
}
};
错误
TypeError: Cannot read property 'css' of undefined
at getCurrentFrameName (C:\Users\charles.sexton\WebstormProjects\JS-Selenium-Toolkit\src\FrameHandler.js:21:75)
at Context.<anonymous> (C:\Users\charles.sexton\WebstormProjects\JS-Selenium-Toolkit\test\FrameHandlerTest.js:17:29)
at callFn (C:\Users\charles.sexton\WebstormProjects\JS-Selenium-Toolkit\node_modules\mocha\lib\runnable.js:315:21)
at Test.Runnable.run (C:\Users\charles.sexton\WebstormProjects\JS-Selenium-Toolkit\node_modules\mocha\lib\runnable.js:308:7)
at Runner.runTest (C:\Users\charles.sexton\WebstormProjects\JS-Selenium-Toolkit\node_modules\mocha\lib\runner.js:422:10)
at C:\Users\charles.sexton\WebstormProjects\JS-Selenium-Toolkit\node_modules\mocha\lib\runner.js:533:12
at next (C:\Users\charles.sexton\WebstormProjects\JS-Selenium-Toolkit\node_modules\mocha\lib\runner.js:342:14)
at C:\Users\charles.sexton\WebstormProjects\JS-Selenium-Toolkit\node_modules\mocha\lib\runner.js:352:7
at next (C:\Users\charles.sexton\WebstormProjects\JS-Selenium-Toolkit\node_modules\mocha\lib\runner.js:284:14)
at Immediate._onImmediate (C:\Users\charles.sexton\WebstormProjects\JS-Selenium-Toolkit\node_modules\mocha\lib\runner.js:320:5)
编辑:我想在测试中包含所有内容,但仍然会遇到与以前相同的错误。我没有包括期望,断言或应该声明。
var expect = require('chai').expect;
var FrameHandler = require('../../JS-Selenium-Toolkit/src/FrameHandler.js');
var webdriver = require('selenium-webdriver');
/** required imports for Google Chrome without setting the PATH environment variable */
var chrome = require('selenium-webdriver/chrome');
var chromePath = require('selenium-chromedriver').path;
describe('FrameHandler', function()
{
var frameHandler = new FrameHandler();
it('', function()
{
var service = new chrome.ServiceBuilder(chromePath).build();
chrome.setDefaultService(service);
var driver = new webdriver.Builder()
.withCapabilities(webdriver.Capabilities.chrome())
.build();
driver.get('http://google.com');
var frames = driver.findElements(driver.By.css('frame'));
console.log(frames.length());
for (var frame in frames)
{
console.log(frame.getName);
if (frame.isSelected()) {
console.log(frame.getName);
return frame.getName;
}
}
});
});
答案 0 :(得分:2)
正如@amitbobade刚刚观察到的那样,By
不是构造的构建器对象driver
的属性,而是selenium-webdriver API实例webdriver
的属性。
(对于任何Java读者,这相当于尝试在WebDriver 实例而不是WebDriver 类上调用By.css()
。)
因此请使用webdriver.By
,而不是driver.By
。
您可以使用console.log
查看差异。如果您在webdriver
上执行此操作,则可以看到以下内容:
webdriver = { ActionSequence: [Function: ActionSequence],
Browser:
{ ANDROID: 'android',
CHROME: 'chrome',
[...]
HTMLUNIT: 'htmlunit' },
Builder: [Function: Builder],
Button: { LEFT: 0, MIDDLE: 1, RIGHT: 2 },
By:
[Function: name(name) {
return By.css('*[name="' + escapeCss(name) + '"]');
}],
Capabilities: [Function: Capabilities],
Capability:
{ ACCEPT_SSL_CERTS: 'acceptSslCerts',
[...]