1- from locust import HttpLocust , TaskSet , task
1+ from locust import HttpUser , TaskSet , task , Locust
22import bs4
33import random
44import sys , os
55
66
7- class WebsiteTasks (TaskSet ):
7+ class WebsiteTasks (HttpUser ):
88 def on_start (self ):
99 res = self .client .get ("/runestone/default/user/login" )
10- pq = bs4 .BeautifulSoup (res .content , features = "lxml" )
10+ pq = bs4 .BeautifulSoup (res .content )
1111 # Get the csrf key for successful submission
1212 i = pq .select ('input[name="_formkey"]' )
1313 token = i [0 ]["value" ]
1414 # login a user
1515 try :
16- user = os . environ [ "RUNESTONE_TESTUSER" ]
17- pw = os . environ [ "RUNESTONE_TESTPW" ]
16+ user = "testuser1"
17+ pw = "xxx"
1818 except :
1919 print ("ERROR please set RUNESTONE_TESTUSER and RUNESTONE_TESTPW " )
2020 sys .exit (- 1 )
2121 res = self .client .post (
2222 "/runestone/default/user/login" ,
2323 {"username" : user , "password" : pw , "_formkey" : token , "_formname" : "login" },
2424 )
25+ # Switch to fopp
26+ res = self .client .post ("/runestone/default/coursechooser/fopp" ,
27+ data = {"course" : "fopp" })
28+
2529 # Get the index and make a list of all chapters/subchapters
26- res = self .client .get ("/runestone /books/published/fopp/index.html" )
27- pq = bs4 .BeautifulSoup (res .content , features = "lxml " )
30+ res = self .client .get ("/ns /books/published/fopp/index.html" )
31+ pq = bs4 .BeautifulSoup (res .content , features = "html.parser " )
2832 pages = pq .select (".toctree-l2 a" )
33+ print (f"Found { len (pages )} pages in the book" )
2934 self .bookpages = [p ["href" ] for p in pages ]
35+ print (f"Found { len (self .bookpages )} pages in the book" )
3036
3137 @task (5 )
3238 def index (self ):
3339 self .client .get ("/runestone" )
3440
41+ @task (5 )
42+ def course (self ):
43+ self .client .get ("/ns/course/index" )
44+
45+ @task (5 )
46+ def doassign (self ):
47+ self .client .get ("/assignment/student/doAssignment?assignment_id=90" )
48+
3549 @task (20 )
3650 def boookpage (self ):
3751 # pick a page at random
3852 url = random .choice (self .bookpages )
39- base = "/runestone /books/published/fopp/"
53+ base = "/ns /books/published/fopp/"
4054 res = self .client .get (base + url )
41- pq = bs4 .BeautifulSoup (res .content , features = "lxml" )
55+ pq = bs4 .BeautifulSoup (res .content )
4256 # client.get ONLY gets the html, so we need to simulate getting all
4357 # of the static assets ourselves.
4458 for s in pq .select ("script" ):
@@ -53,10 +67,14 @@ def boookpage(self):
5367 css = self .client .get (
5468 base + s ["href" ].replace ("../" , "" ), name = "css"
5569 )
56-
57-
58- class WebsiteUser (HttpLocust ):
59- host = "http://localhost"
60- task_set = WebsiteTasks
61- min_wait = 1000
62- max_wait = 15000
70+ data = {
71+ "lastPageUrl" : base + url ,
72+ "lastPageScrollLocation" : 0 ,
73+ "completionFlag" : 0 ,
74+ "pageLoad" : True ,
75+ "markingComplete" : False ,
76+ "markingIncomplete" : False ,
77+ "course" : "fopp" ,
78+ "isPtxBook" : False ,
79+ }
80+ self .client .post ("/ns/logger/updatelastpage" , json = data )
0 commit comments