Selenium 2 34 combat automated testing (written test Web)

Web writing test cases

1. Introduction of unittest unit testing framework, which is mainly to run Web automated test scripts. Simple Plan at the test directory:
web_demo1 /
------ test_case /
------------test_baidu.py
------------test_google.py
--- Report --- /
----------------------- of login.txt
------runtest.py
directory structure as shown below:

Create a web test cases.

#test_baidu.py

#coding:utf-8 from selenium import webdriver import unittest import time class MyTest(unittest.TestCase): def setUp(self): self.driver=webdriver.Chrome() self.driver.maximize_window() self.driver.implicitly_wait(10) self.base_url="http://www.baidu.com" def test_baidu(self): driver=self.driver driver.get(self.base_url+"/") driver.find_element_by_id("kw").clear() driver.find_element_by_id("kw").send_keys("unittest") driver.find_element_by_id("su").click() time.sleep(2) title=driver.title self.assertEqual(title,u"unittest_百度搜索") def tearDown(self): self.driver.quit() if __name__=="__main__": unittest.main()

  

#test_google.py

#coding:utf-8 from selenium import webdriver from selenium.webdriver.common.keys import Keys import unittest import time class MyTest(unittest.TestCase): def setUp(self): self.driver=webdriver.Chrome() self.driver.maximize_window() self.driver.implicitly_wait(10) self.base_url="https://www.google.com" def test_google(self): driver=self.driver driver.get(self.base_url+"/") driver.find_element_by_name("q").clear() driver.find_element_by_name("q").send_keys("webdriver") driver.find_element_by_name("q").send_keys(Keys.ENTER) time.sleep(2) title=driver.title self.assertEqual(title,u"webdriver - Google 搜索") def tearDown(self): self.driver.quit() if __name__=="__main__": unittest.main()  

Created under test_case / directory respectively Baidu search test_baidu and proper way search test_google.py test file, and write web automation test cases in the test file .

runtest.py file as shown below, to be noted here :( import module), and since test_baidu.py test_google.py with runtest.py not in the same directory, so here, only need to add a directory __init__ at test_case. py files.

#runtest.py

#coding:utf-8 import unittest #加载测试集 from test_case import test_baidu from test_case import test_google #构造测试集 suite=unittest.TestSuite() suite.addTest(test_baidu.MyTest("test_baidu")) suite.addTest(test_google.MyTest("test_google")) if __name__=="__main__": runer=unittest.TextTestRunner() runer.run(suite)

  

2. Save the test results

How to generate a test result log.txt file it? It should help the dos command to achieve.
First open Windows command prompt, enter the command in the ../web_demo1/ directory, as shown in FIG.

 

 

Open .... / Report / log.txt file contents as shown below

 

 


Summary: Learn unittest framework is to use it to write web automated test cases, test cases using its organization, asserted that the expected results and batch execution of test cases and other functions, can be well developed web automated testing.

 

Guess you like

Origin www.cnblogs.com/Rita-LJ/p/11793018.html