当我们描述一个“好的自动化测试用例”时,经常出现标准是:

  • 精确
    自动化测试用例应该测试一件事,只有一件事。与测试用例无关的应用程序的某个部分中的错误不应导致测试用例失败
  • 独立
    自动化测试用例不应该受测试套件中任何其他测试用例影响
  • 快速
    这不需要太多的解释。自动化测试用例套件运行得越快,它就越有用

这些标准在Selenium测试套件中可能特别具有挑战性。原因在于,Selenium的UI自动化测试通常涉及可能包括注册,登录,某些导航,表单提交或与网站的其他交互的设置。只有在做完这些事情之后,你才可以在网站的某些方面做出断言。但是这样就会引入一些和原本的测试用例无关的错误导致自动化脚本运行失败。

02、解决方法

现在很多新的网站的后端都采用REST API。如果能够在脚本中通过访问REST API来完成一些基础的必要操作,而不是在UI上,这样将提高我们测试的执行效率

REST API基于HTTP协议,这是基本上支持整个互联网的协议。几乎所有现代编程语言都有可用于发出HTTP请求的库,因此,我们可以将调用REST API的支持添加到几乎任何测试套件中。

03、实例

很多测试中都会遇到新建账号的问题。需要测试新的账号在登陆之后的行为。如果能够使用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}@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中数据的一致性等等。