One article teaches data-driven UI automation testing step by step from 0 to 1!

In UI automated testing, we need to separate the data used in the test into files. If it is simply written in our test module, it is not a good design, so no matter what type of automated test it is, it needs to be The data is separated. Of course, separated into specific files, there are actually many forms of files. Here we mainly explain the application of JSON files and YAML files in UI automated testing.

1. JSON file

The JSON library is mainly used in serialization and deserialization, especially in automated testing of APIs. Serialization and deserialization are technology stack systems that must be mastered in the knowledge system. Of course, we can also serialize and deserialize files. Serialization of files can be simply understood as writing third-party data into files. The method used in the JSON library is the dump() method. Then Supporting serialization of files can be understood as reading data from the file, using the load() method in the JSON library. The following is mainly to separate the data for UI automation testing. The specific code is:

import unittest
from parameterized import  parameterized,param
from selenium  import  webdriver
import  time as t

#parameterized是参数化库

class AddTest(unittest.TestCase):

    def setUp(self) -> None:
        self.driver=webdriver.Chrome()
        self.driver.maximize_window()
        self.driver.implicitly_wait(30)
        self.driver.get('https://mail.sina.com.cn/#')

    def tearDown(self) -> None:
        self.driver.quit()


    @parameterized.expand([
        param('','','请输入邮箱名'),
        param('srtSA','saert','您输入的邮箱名格式不正确'),
        param('[email protected]','asdfrty','登录名或密码错误')
    ])
    def test_sina_email(self,username,password,result):
        t.sleep(2)
        self.driver.find_element_by_id('freename').send_keys(username)
        t.sleep(2)
        self.driver.find_element_by_id('freepassword').send_keys(password)
        t.sleep(2)
        self.driver.find_element_by_link_text('登录').click()
        t.sleep(3)
        div=self.driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/div/div[4]/div[1]/div[1]/div[1]/span[1]')
        assert div.text==result

if __name__ == '__main__':
    unittest.main(verbosity=2)

As you can see in the above file, we can separate the test data into JSON files. The separated data is specifically:

{
  "data":
  [
    {"username": "","password": "","text": "请输入邮箱名"},
    {"username": "srtSA","password": "saert","text": "您输入的邮箱名格式不正确"},
    {"username": "[email protected]","password": "asdfrty","text": "登录名或密码错误"}
  ]
}

The improved test script is:

import unittest
from parameterized import  parameterized,param
from selenium  import  webdriver
import  time as t
import  json

def readJson():
   return json.load(open('sina.json'))['data']

class AddTest(unittest.TestCase):

   def setUp(self) -> None:
      self.driver=webdriver.Chrome()
      self.driver.maximize_window()
      self.driver.implicitly_wait(30)
      self.driver.get('https://mail.sina.com.cn/#')

   def tearDown(self) -> None:
      self.driver.quit()


   @parameterized.expand([
      param(readJson()[0]['username'],readJson()[0]['password'],readJson()[0]['text']),
      param(readJson()[1]['username'],readJson()[1]['password'],readJson()[1]['text']),
      param(readJson()[2]['username'],readJson()[2]['password'],readJson()[2]['text'])
   ])
   def test_sina_email(self,username,password,result):
      t.sleep(2)
      self.driver.find_element_by_id('freename').send_keys(username)
      t.sleep(2)
      self.driver.find_element_by_id('freepassword').send_keys(password)
      t.sleep(2)
      self.driver.find_element_by_link_text('登录').click()
      t.sleep(3)
      div=self.driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/div/div[4]/div[1]/div[1]/div[1]/span[1]')
      assert div.text==result


if __name__ == '__main__':
   unittest.main(verbosity=2)
现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。
如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受
可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛
分享他们的经验,还会分享很多直播讲座和技术沙龙
可以免费学习!划重点!开源的!!!
qq群号:110685036

2. YAML file

Below we demonstrate how to store test data in a YAML file. The contents of the separated file are:

username: ""
password: ""
text: "请输入邮箱名"

---
username: "srtSA"
password: "saert"
text: "您输入的邮箱名格式不正确"

---
username: "[email protected]"
password: "asdfrty"
text: "登录名或密码错误"

The improved test script is:

import unittest
from parameterized import  parameterized,param
from selenium  import  webdriver
import  time as t
import  json
import yaml

def readJson():
   return json.load(open('sina.json'))['data']


def readYaml():
   with open('sina.yaml') as f:
      return list(yaml.unsafe_load_all(f))

class AddTest(unittest.TestCase):

   def setUp(self) -> None:
      self.driver=webdriver.Chrome()
      self.driver.maximize_window()
      self.driver.implicitly_wait(30)
      self.driver.get('https://mail.sina.com.cn/#')

   def tearDown(self) -> None:
      self.driver.quit()


   @parameterized.expand([
      param(readYaml()[0]['username'],readYaml()[0]['password'],readYaml()[0]['text']),
      param(readYaml()[1]['username'],readYaml()[1]['password'],readYaml()[1]['text']),
      param(readYaml()[2]['username'],readYaml()[2]['password'],readYaml()[2]['text'])
   ])
   def test_sina_email(self,username,password,result):
      t.sleep(2)
      self.driver.find_element_by_id('freename').send_keys(username)
      t.sleep(2)
      self.driver.find_element_by_id('freepassword').send_keys(password)
      t.sleep(2)
      self.driver.find_element_by_link_text('登录').click()
      t.sleep(3)
      div=self.driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/div/div[4]/div[1]/div[1]/div[1]/span[1]')
      assert div.text==result


if __name__ == '__main__':
   unittest.main(verbosity=2)

As above, it is demonstrated in detail that in UI automated testing, we can separate the test data used into JSON files and YAML files. This achieves data separation and aims to make testing simpler and more efficient.

Finally, I would like to thank everyone who read my article carefully. Looking at the increase in fans and attention, there is always some courtesy. Although it is not a very valuable thing, if you can use it, you can take it directly!

Software Testing Interview Document

We must study to find a high-paying job. The following interview questions are from the latest interview materials from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and some Byte bosses have given authoritative answers. After finishing this set I believe everyone can find a satisfactory job based on the interview information.
 

Insert image description here

Guess you like

Origin blog.csdn.net/jiangjunsss/article/details/133358043