2015-02-13 1 views
1

Я хочу, чтобы саранча могла войти в мое веб-приложение и начать щелкнуть ссылки в веб-приложении.Сделано Locust для входа в веб-приложение

С помощью этого кода я просто получаю активность для первой страницы с логином, и я не получаю уведомления внутри приложения.

Код:

import random 
from locust import HttpLocust, TaskSet, task 
from pyquery import PyQuery 


class WalkPages(TaskSet): 
    def on_start(self): 
     self.client.post("/", { 
      "UserName": "[email protected]", 
      "Password": "2Password!", 
      "submit": "Sign In" 
     }) 
     self.index_page() 

    @task(10) 
    def index_page(self): 
     r = self.client.get("/Dashboard.mvc") 
     pq = PyQuery(r.content) 
     link_elements = pq("a") 
     self.urls_on_current_page = [] 
     for l in link_elements: 
      if "href" in l.attrib: 
      self.urls_on_current_page.append(l.attrib["href"]) 

    @task(30) 
    def load_page(self): 
     url = random.choice(self.urls_on_current_page) 
     r = self.client.get(url) 


class AwesomeUser(HttpLocust): 
    task_set = WalkPages 
    host = "https://myenv.beta.webapp.com" 
    min_wait = 20 * 1000 
    max_wait = 60 * 1000 

я получаю последующие Сообщ в терминале после первого раунда.

[2015-02-13 12:08:43,740] webapp-qa/ERROR/stderr: Traceback (most recent call last): 
    File "/usr/local/lib/python2.7/dist-packages/locust/core.py", line 267, in run 
    self.execute_next_task() 
    File "/usr/local/lib/python2.7/dist-packages/locust/core.py", line 293, in execute_next_task 
    self.execute_task(task["callable"], *task["args"], **task["kwargs"]) 
    File "/usr/local/lib/python2.7/dist-packages/locust/core.py", line 305, in execute_task 
    task(self, *args, **kwargs) 
    File "/home/webapp/LoadTest/locustfile.py", line 31, in load_page 
    url = random.choice(self.urls_on_current_page) 
    File "/usr/lib/python2.7/random.py", line 273, in choice 
    return seq[int(self.random() * len(seq))] # raises IndexError if seq is empty 
IndexError: list index out of range 
[2015-02-13 12:08:43,752] webapp-qa/ERROR/stderr: Traceback (most recent call last): 
    File "/usr/local/lib/python2.7/dist-packages/locust/core.py", line 267, in run 
    self.execute_next_task() 
    File "/usr/local/lib/python2.7/dist-packages/locust/core.py", line 293, in execute_next_task 
    self.execute_task(task["callable"], *task["args"], **task["kwargs"]) 
    File "/usr/local/lib/python2.7/dist-packages/locust/core.py", line 305, in execute_task 
    task(self, *args, **kwargs) 
    File "/home/webapp/LoadTest/locustfile.py", line 31, in load_page 
    url = random.choice(self.urls_on_current_page) 
    File "/usr/lib/python2.7/random.py", line 273, in choice 
    return seq[int(self.random() * len(seq))] # raises IndexError if seq is empty 
IndexError: list index out of range 
[2015-02-13 12:08:43,775] webapp-qa/ERROR/stderr: Traceback (most recent call last): 
    File "/usr/local/lib/python2.7/dist-packages/locust/core.py", line 267, in run 
    self.execute_next_task() 
    File "/usr/local/lib/python2.7/dist-packages/locust/core.py", line 293, in execute_next_task 
    self.execute_task(task["callable"], *task["args"], **task["kwargs"]) 
    File "/usr/local/lib/python2.7/dist-packages/locust/core.py", line 305, in execute_task 
    task(self, *args, **kwargs) 
    File "/home/webapp/LoadTest/locustfile.py", line 31, in load_page 
    url = random.choice(self.urls_on_current_page) 
    File "/usr/lib/python2.7/random.py", line 273, in choice 
    return seq[int(self.random() * len(seq))] # raises IndexError if seq is empty 
IndexError: list index out of range 
+0

Элемент 'self.index_page()' не выглядит отступом правильно. Кроме того, вы пытаетесь ввести логику index_page внутри функции on_startup. – ivan

+0

Позвольте мне изменить логику on_startup, спасибо за ваш отзыв. – Almostwo

ответ

0

Ваш список может быть пустым.

@task(30) 
def load_page(self): 
    if self.urls_on_current_page: 
     url = random.choice(self.urls_on_current_page) 
     r = self.client.get(url) 
0

Мне требуется время, но кому это может понадобиться. Мои выводы в вашем коде: логин запросы кажутся неправильными (проверьте, если это правильно), вы не можете получить переменную, определенную внутри функции от другой функции, давая task(10) не подходит для данных setter function. Задайте urls_on_current_page как переменную класса, которая будет использоваться для других членов класса. См. Мой код и комментарий:

import random 
from locust import HttpLocust, TaskSet, task 
from pyquery import PyQuery 


class WalkPages(TaskSet): 
    # define variable here to access them from inside the functions 
    urls_on_current_page = [] 

    def login(self): 
     self.client.post("/login", data = {"UserName": "[email protected]", "Password": "password"}) 

    def get_urls(self): 
     r = self.client.get("/Dashboard.mvc") 
     pq = PyQuery(r.content) 
     link_elements = pq("a") 
     for link in link_elements: 
      if key in link.attrib and "http" not in link.attrib[key]: 
      # there maybe external link on the page 
       self.urls_on_current_page.append(link.attrib[key]) 


    def on_start(self): 
     self.login() 
     self.get_urls() 


    @task(30) 
    def load_page(self): 
     url = random.choice(self.urls_on_current_page) 
     r = self.client.get(url) 


class AwesomeUser(HttpLocust): 
    task_set = WalkPages 
    host = "https://myenv.beta.webapp.com" 
    min_wait = 20 * 1000 
    max_wait = 60 * 1000 
Смежные вопросы