element ui input视图没刷新_[Selenium自动化测试实战] 如何在UI自动化测试中加入REST API的操作...
問題
當我們描述一個“好的自動化測試用例”時,經常出現標準是:
- 精確。自動化測試用例應該測試一件事,只有一件事。與測試用例無關的應用程序的某個部分中的錯誤不應導致測試用例失敗。
- 獨立。自動化測試用例不應該受測試套件中任何其他測試用例影響。
- 快速。這不需要太多的解釋。自動化測試用例套件運行得越快,它就越有用。
這些標準在Selenium測試套件中可能特別具有挑戰性。原因在于,Selenium的UI自動化測試通常涉及可能包括注冊,登錄,某些導航,表單提交或與網站的其他交互的設置。只有在做完這些事情之后,你才可以在網站的某些方面做出斷言。但是這樣就會引入一些和原本的測試用例無關的錯誤導致自動化腳本運行失敗。解決方法
現在很多新的網站的后端都采用REST API。如果能夠在腳本中通過訪問REST API來完成一些基礎的必要操作,而不是在UI上,這樣將提高我們測試的執行效率。
REST API基于HTTP協議,這是基本上支持整個互聯網的協議。 幾乎所有現代編程語言都有可用于發出HTTP請求的庫,因此,我們可以將調用REST API的支持添加到幾乎任何測試套件中。實例
很多測試中都會遇到新建賬號的問題。需要測試新的賬號在登陸之后的行為。如果能夠使用REST API來新建用戶的話,就可以大大的節省填寫新建賬號的表單的時間。
這里假設一個虛擬的REST API
POST http://api.myfakeapp.com/v1/create-user
通過POST方法可以新建用戶。Body里面需要填充的JSON數據如下:
{
'username': 'example-username',
'password': 'abcd1234',
'email': 'bob@example.com',
'first_name': 'bob',
'last_name': 'example'
}
下面是代碼實現
require 'rest-client'
require 'json'
require 'securerandom'
class RestApiInterface
@headers = {
'content-type' => 'application/json',
'user-agent' => 'Rest Api Helper',
}
def post_to_api url, post_body_obj
json_body = JSON.generate(post_body_obj)
response = RestClient.post url, json_body, @headers
end
def create_test_user
# Step 1: Build the user parameters randomly
random_test_user = {
'username' => random_string,
'password' => random_string,
'email' => "#{random_string}@http://testing.com",
'first_name' => 'test',
'last_name' => 'user',
}
# Step 2: Execute the API call
response = post_to_api "http://api.myfakeapp.com/v1/create-user", random_test_user
# Step 3: Ensure the api call returned a success code
if response.code != '200'
raise 'User creation failed'
end
# Final Step: Return the user object so we can use it
response.body['user']['data']
end
def random_string
# This is an easy way to get a good randomized string
SecureRandom.hex
end
end
$driver = Selenium::WebDriver.for :firefox
user = RestApiInterface.new.create_test_user
$driver.get 'http://myfakeapp.com'
$driver.find_element(:css, 'input[name="username"]').send_keys @user['username']
$driver.find_element(:css, 'input[name="password"]').send_keys @user['password']
$driver.find_element(:css, 'button[name="login"]').click
puts $driver.find_element(:css, '#user_id').text總結
這里只是給出了一個如何把API訪問和UI自動化結合的思路。在這個思路的引導下,可以對UI自動化測試進行很多擴展,例如校驗界面數據元素的數據和API中數據的一致性等等。
軟件測試、接口測試、自動化測試、面試經驗交流。感興趣可以加我們的軟件測試技術交流群,有不定期的發放免費的資料鏈接,還有同行一起技術交流。
總結
以上是生活随笔為你收集整理的element ui input视图没刷新_[Selenium自动化测试实战] 如何在UI自动化测试中加入REST API的操作...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: nmap地址段下的ip_安服福音——花式
- 下一篇: rust矿洞绳子怎么爬下_rust矿洞绳