-
Notifications
You must be signed in to change notification settings - Fork 0
/
createKahoot.py
executable file
·206 lines (177 loc) · 6.28 KB
/
createKahoot.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env python2
import sys
import time
import re
import os
import openpyxl
import selenium
import gtk, pygtk
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver import ActionChains
driver = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver")
# Open camera ip page
driver.get("https://kahoot.com/welcomeback/")
actions=ActionChains(driver)
#relocate window
driver.set_window_size(gtk.gdk.screen_width()/2, gtk.gdk.screen_height())
driver.set_window_position(0, 0)
def login():
time.sleep(1)
button=driver.find_element_by_xpath('//*[@id="menu-header-menu"]/li[4]/a')
button.click()
time.sleep(1)
username=driver.find_element_by_xpath('//*[@id="username-input-field"]/div[1]/input')
username.send_keys("TeachBot")
password=driver.find_element_by_xpath('//*[@id="password-input-field"]/div[1]/input')
password.send_keys("IamTeachBot")
signin=driver.find_element_by_xpath('//*[@id="layout"]/div/main/div/form/button[2]')
signin.click()
return
def createKahoot():
button=driver.find_element_by_xpath('//*[@id="top-menu-bar"]/div[2]/div/a')
button.click()
button=driver.find_element_by_xpath('//*[@id="screen"]/div/div/div/section/div/ul/li[1]/a')
button.click()
return
def testSummary(filename):
title=driver.find_element_by_xpath('//*[@id="layout"]/div/main/form/div/div/div[1]/div/div/input')
title.send_keys(filename)
summary=driver.find_element_by_xpath('//*[@id="layout"]/div/main/form/div/div/div[2]/div/div/textarea')
summary.send_keys('Automated test')
button=driver.find_element_by_xpath('//*[@id="layout"]/div/main/form/div[2]/div[3]/div/div/div/select')
button.click()
button=driver.find_element_by_xpath('//*[@id="layout"]/div/main/form/div[2]/div[3]/div/div/div/select/option[2]')
button.click()
button=driver.find_element_by_xpath('//*[@id="app"]/div/div/span/section/div/div/div/div/button[1]')
button.click()
time.sleep(1)
button=driver.find_element_by_xpath('//*[@id="layout"]/div/header/header/div[3]/button[1]')
button.click()
button=driver.find_element_by_xpath('//*[@id="app"]/div/div/span/section/div/div/div/div/button[1]')
button.click()
time.sleep(2)
button=driver.find_element_by_xpath('//*[@id="layout"]/div/header/header/div[3]/button[1]')
button.click()
return
def addQuestion(ws, i):
actions= ActionChains(driver)
button=driver.find_element_by_xpath('//*[@id="layout"]/div/main/section[2]/div/div/button')
button.click()
time.sleep(1)
#Send question
question=driver.find_element_by_xpath('//*[@id="ql-editor-1"]/div')
question.click()
actions.send_keys(str(ws.cell(row=i , column=2 ).value))
actions.perform()
actions.reset_actions()
j=3
x=1
while ws.cell(row=i , column=j).value!=None:
#Send answers
answer=driver.find_element_by_xpath('//*[@id="ql-editor-'+str(x+1)+'"]/div')
answer.click()
actions.send_keys(str(ws.cell(row=i , column=j ).value))
actions.perform()
actions.reset_actions()
#Validate answer
if str(ws.cell(row=i , column=j+1 ).value)=='correct' or str(ws.cell(row=i , column=j+1 ).value)=='correcto':
if (x)%2==0:
b=2
else:
b=1
if x<3:
a=1
else:
a=2
answer2=driver.find_element_by_xpath('//*[@id="layout"]/div/main/form/div[2]/div['+str(a)+']/div['+str(b)+']/div/div/div[2]/label/span')
answer2.click()
j=j+2
x=x+1
button=driver.find_element_by_xpath('//*[@id="layout"]/div/header/header/div[3]/button')
button.click()
return
def obtain_key ():
text=driver.find_element_by_xpath('//*[@id="mainView"]/div[2]/div[2]/div[1]/div[1]/div[2]/span/strong')
def check_exists_by_xpath(xpath):
try:
driver.find_element_by_xpath(xpath)
except NoSuchElementException:
return False
return True
def main():
filename="Test"
#logn to server
print("loging to kahoot...")
time.sleep(2)
login()
time.sleep(2)
print("creating new kahoot...")
createKahoot()
time.sleep(2)
#Questions
print("adding questions...")
wb = openpyxl.load_workbook(filename+'.xlsx')
ws = wb.active
testSummary(filename)
for i in range (1, ws.max_row+1):
time.sleep(1)
addQuestion(ws, i)
#Start quiz
print("starting kahoot...")
button=driver.find_element_by_xpath('//*[@id="layout"]/div/header/header/div[3]/button')
button.click()
time.sleep(3)
button=driver.find_element_by_xpath('//*[@id="layout"]/div/header/header/div[3]/button')
button.click()
time.sleep(3)
button=driver.find_element_by_xpath('//*[@id="kahoot-list-container"]/div/table[2]/tbody/tr/td[4]/button')
button.click()
time.sleep(15)
driver.close()
tabs=driver.window_handles
driver.switch_to.window(str(tabs[0]))
button=driver.find_element_by_xpath('//*[@id="mainView"]/div[3]/div/div[1]/div[1]/button')
button.click()
time.sleep(7)
#Obtain key and start players
text=driver.find_element_by_xpath('//*[@id="mainView"]/div[2]/div[2]/div[1]/div[1]/div[2]/span/strong')
print (text.text)
print("starting players...")
for i in range (0,2):
run='python player1.py '+text.text+' '+str(i)+' '+str(ws.max_row)
os.system("gnome-terminal -e "+"'"+run+"'")
print("Lets Go!!")
#Wait for players and start quiz
time.sleep(15)
button=driver.find_element_by_xpath('//*[@id="mainView"]/div[3]/button')
button.click()
time.sleep(10)
#Wait for question to finish
for i in range (0, ws.max_row):
time.sleep(30)
iframe=driver.find_element_by_css_selector('iframe')
driver.switch_to_frame(iframe)
button=driver.find_element_by_xpath('//*[@id="app"]/main/div[1]/section[1]/section[3]/div/aside/button')
button.click()
#return to original frame
driver.switch_to_default_content()
if i!=ws.max_row-1:
time.sleep(3)
iframe=driver.find_element_by_id('scoreBoardIframe')
driver.switch_to_frame(iframe)
button=driver.find_element_by_xpath('//*[@id="app"]/main/div[1]/div/div/button')
button.click()
driver.switch_to_default_content()
#Get results
time.sleep(5)
iframe=driver.find_element_by_id('scoreBoardIframe')
driver.switch_to_frame(iframe)
#button=driver.find_element_by_xpath('//*[@id="mainView"]/div/div[2]/div/div/button')
#button.click()
time.sleep(5)
driver.quit()
return
if __name__ == '__main__':
main()