forked from thoughtbot/capybara-webkit
/
FrameFocus.cpp
66 lines (56 loc) · 1.36 KB
/
FrameFocus.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "FrameFocus.h"
#include "Command.h"
#include "WebPage.h"
FrameFocus::FrameFocus(WebPage *page, QObject *parent) : Command(page, parent) {
}
void FrameFocus::start(QStringList &arguments) {
findFrames();
switch(arguments.length()) {
case 1:
focusId(arguments[0]);
break;
case 2:
focusIndex(arguments[1].toInt());
break;
default:
focusParent();
}
}
void FrameFocus::findFrames() {
frames = page()->currentFrame()->childFrames();
}
void FrameFocus::focusIndex(int index) {
if (isFrameAtIndex(index)) {
frames[index]->setFocus();
success();
} else {
frameNotFound();
}
}
bool FrameFocus::isFrameAtIndex(int index) {
return 0 <= index && index < frames.length();
}
void FrameFocus::focusId(QString name) {
for (int i = 0; i < frames.length(); i++) {
if (frames[i]->frameName().compare(name) == 0) {
frames[i]->setFocus();
success();
return;
}
}
frameNotFound();
}
void FrameFocus::focusParent() {
if (page()->currentFrame()->parentFrame() == 0) {
emit finished(new Response(false, "Already at parent frame."));
} else {
page()->currentFrame()->parentFrame()->setFocus();
success();
}
}
void FrameFocus::frameNotFound() {
emit finished(new Response(false, "Unable to locate frame. "));
}
void FrameFocus::success() {
emit finished(new Response(true));
}