我正在为我的应用程序编写负载测试。
我想模拟以下步骤:
- 登录
- 访问几个页面
- 在网站内提交表格
我首先编写了登录并访问了几个页面并成功运行它们(没有错误)。当我添加代码以提交表单时,我收到提交摘要事务的“404/未找到错误”。
我感谢任何可以为我提供解决此问题的方向的人。
我使用 ruby 编写了这个测试脚本,然后执行将其转换为 .jmx 文件,我使用该文件在 cli 中运行无头测试。
登录代码并访问几个页面:
require 'ruby-jmeter'
test do
threads count: 100, rampup: 60, loops: 10, duration: 120 do
defaults domain: 'myapp.herokuapp.com', protocol: 'https'
cookies policy: 'rfc2109', clear_each_iteration: true
transaction 'Page Load Tests' do
user_defined_variables [{name: 'email', value: 'example@example.com'}, {name: 'password', value: 'Pass_w0rd'}]
visit name: 'Visit Login', url: '/users/sign_in' do
extract name: 'csrf-token', xpath: "//meta[@name='csrf-token']/@content", tolerant: true
extract name: 'csrf-param', xpath: "//meta[@name='csrf-param']/@content", tolerant: true
extract name: 'authenticity_token', regex: 'name="authenticity_token" value="(.+?)"'
end
end
http_header_manager name: 'X-CSRF-Token', value: '${csrf-token}'
submit name: 'Submit login', url: '/users/sign_in',
fill_in: {
'${csrf-param}' => '${csrf-token}',
'user[email]' => '${email}',
'user[password]' => '${password}',
'authenticity_token' => '${authenticity_token}'
}
visit name: 'Welcome Page', url: '/static_pages/welcome'
visit name: 'New Abstract Page', url: '/users/2/abstracts/new'
visit name: 'My Profile Page', url:'/users/2/participations/1/profile'
visit name: 'My Own Abstract Page', url:'/users/2/participations/1/abstracts/1'
view_results_in_table
aggregate_report
end.jmx
登录、访问页面和提交表单的代码:
require 'ruby-jmeter'
test do
threads count: 100, rampup: 60, loops: 10, duration: 120 do
defaults domain: 'myapp.herokuapp.com', protocol: 'https'
cookies policy: 'rfc2109', clear_each_iteration: true
transaction 'Page Load Tests' do
user_defined_variables [{name: 'email', value: 'example@example.com'}, {name: 'password', value: 'Pass_w0rd'}]
visit name: 'Visit Login', url: '/users/sign_in' do
extract name: 'csrf-token', xpath: "//meta[@name='csrf-token']/@content", tolerant: true
extract name: 'csrf-param', xpath: "//meta[@name='csrf-param']/@content", tolerant: true
extract name: 'authenticity_token', regex: 'name="authenticity_token" value="(.+?)"'
end
end
http_header_manager name: 'X-CSRF-Token', value: '${csrf-token}'
submit name: 'Submit login', url: '/users/sign_in',
fill_in: {
'${csrf-param}' => '${csrf-token}',
'user[email]' => '${email}',
'user[password]' => '${password}',
'authenticity_token' => '${authenticity_token}'
}
visit name: 'Welcome Page', url: '/static_pages/welcome'
visit name: 'New Abstract Page', url: '/users/2/abstracts/new'
visit name: 'My Profile Page', url:'/users/2/participations/1/profile'
visit name: 'My Own Abstract Page', url:'/users/2/participations/1/abstracts/1'
transaction 'Submit Abstract' do
visit name: 'New Abstract Page', url: '/users/2/abstracts/new' do
extract name: 'csrf-token', xpath: "//meta[@name='csrf-token']/@content", tolerant: true
extract name: 'csrf-param', xpath: "//meta[@name='csrf-param']/@content", tolerant: true
extract name: 'authenticity_token', regex: 'name="authenticity_token" value="(.+?)"'
end
http_header_manager name: 'X-CSRF-Token', value: '${csrf-token}'
submit name: 'Submit Abstract', url: '/users/2/abstracts/new',
fill_in: {
'${csrf-param}' => '${csrf-token}',
'abstract[title]' => 'Lorem Ipsum',
'abstract[main_author]' => '2',
'abstract[co_authors][]' => ["", "1", "3"],
'abstract[corresponding_author_email]' => '${email}',
'abstract[keywords]' => 'word, words',
'abstract[body]' => 'The test directive is a root point, where all the magic starts. Then, using threads method we are telling JMeter what number of users we want to use. The defaults command allows us to specify default options for all our http requests. And, finally,cookies indicates that we need to store cookies and send them with each request.',
'abstract[references]' => '1\r\n2\r\n3',
'authenticity_token' => '${authenticity_token}'
} do
assert 'contains' => 'Abstract submission completed.'
assert 'contains' => 'Lorem Ipsum'
end
end
end
view_results_in_table
aggregate_report
end.jmx
根据@DMITRI T 建议更新:
请求标头:
浏览器:
仪表:
请求正文:
浏览器:
仪表:
结果:提交抽象事务仍然出现 404 错误