1. With the unittest script analysis
Maybe there any necessary connection between you and now my heart is still in doubt, unittest framework and we've written Web automated testing? Of course, since unittest can organize, run the test case, then why not organize, run the Web test automation do? Let us now begin with an example and see it.
# -*- coding:utf-8 -*- from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import Select from selenium.common.exceptions import NoSuchElementException from selenium.common.exceptions import NoAlertPresentException import unittest,time,re class BaiduTest(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.implicitly_wait(30) self.base_url = "http://www.baidu.com/" self.verificationErrors = [] self.accept_next_alert = True 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("selenium") driver.find_element_by_id("su").click() def is_element_present(self,how,what): try: self.driver.find_element(by=how,value=what) except NoSuchElementException: return False return True def is_alert_present(self): try: self.driver.switch_to.alert() except NoAlertPresentException: return False return True def close_alert_and_get_its_text(self): try: alert = self.driver.switch_to.alert() alert_text = alert.text if self.accept_next_alert: alert.accept() else: alert.dismiss() return alert_text finally: self.accept_next_alert = True def tearDown(self): self.driver.quit() self.assertEqual([],self.verificationErrors) if __name__ == "__main__": unittest.main()
I believe we now no longer feel strange to see when this script, here we come to analyze these diamante have done what to do.
import unittest
First introduced unittest framework.
class BaiduTest(unittest.TestCase):
BaiduTest class inheritance TestCase class called unittest framework of the standard test class.
def setUp(self): self.driver = webdriver.Chrome() self.driver.implicitly_wait(30) self.base_url = "http://www.baidu.com/" self.verificationErrors = [] self.accept_next_alert = True
setUp initialization for setting, before execution is performed before each test case, it echoes tearDown method, which is performed after each test case execution. Here initialization defines the browser starts and the base URL address.
implicitly_wait () have already learned, the element is provided on page recessive waiting time is 30 seconds.
Next, define the empty verificationErrors array, the error message when the script runs will be recorded in this array.
Accept_next_alert defined variable indicating whether to continue to accept the next warning, the initial state is True.
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("selenium") driver.find_element_by_id("su").click()
test_baidu is placed in our test script, this part we are very familiar with, it is no longer here to explain.
def is_element_present(self,how,what): try: self.driver.find_element(by=how,value=what) except NoSuchElementException: return False return True
is_element_present method used to find the page element exists, to receive a positioning method element (How) and target value (What) by find_element (). If the element is positioned to return True, otherwise an exception is thrown and returns False. try ... except ... for the Python language exception handling.
def is_alert_present(self): try: self.driver.switch_to.alert() except NoAlertPresentException: return False return True
is_alert_present () method for determining whether there is a warning current page frame, using switch_to.alert () method to capture provided WebDriver alert box on the page. If the alert box to capture the returns True, otherwise an exception is thrown NoAlertPresentException type, and returns False.
def close_alert_and_get_its_text(self): try: alert = self.driver.switch_to.alert() alert_text = alert.text if self.accept_next_alert: alert.accept() else: alert.dismiss() return alert_text finally: self.accept_next_alert = True
close_alert_and_get_its_text () to close the warning and get a warning message. First, get a warning by switch_to_alert (), get a warning box information through text. Then determine if statements by state accept_next_alert in setUp () has been initialized state is True, If True, a warning is accepted by accept (), otherwise dismiss () ignore this warning.
def tearDown(self): self.driver.quit() self.assertEqual([],self.verificationErrors)
tearDown () method after each test method execution call this method for cleaning up after the test case execution, such as exit the browser, turn off the drive, resumption of state and other use cases.
VerificationErrors empty array defined in setUp () method, where by assertEqual () is empty comparator which, if the process is performed with the embodiment is empty, it is no abnormality, the AssertionError responsible throws.
if __name__ == "__main__": unittest.main()
Test method to run through unittest.main () method in the current file, and run the default matching methods start with test.
2. Write Web test
In front of a lot of space details the unittest unit testing framework, which aims to use it to run a Web test automation scripts. Prior to this, simply look at the test planning directory.
test_project/
test_case/
test_baidu.py
test_youdao.py
report/
login.txt
runtest.py
Create a Web test case.
test_baidu.py:
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,"unittest_百度搜索") def tearDown(self): self.driver.quit() if __name__ == "__main__": unittest.main()
test_youdao.py:
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.youdao.com" def test_youdao(self): driver = self.driver driver.get(self.base_url+"/") driver.find_element_by_id (__name__" TranslateContent " ) .clear () driver.find_element_by_id ( " translateContent " ) .send_keys ( " the webdriver " ) driver.find_element_by_xpath ( ' // * [@ ID = "form"] / Button ' ) .click () the time.sleep ( 2 ) title = driver.title self.assertEqual (title, " [translation webdriver] What does it mean _ _ _ phonetic pronunciation of English webdriver sentence _ _ _ use online translation _ way dictionary " ) DEF tearDown (Self): Self. driver.close () IF == "__main__": unittest.main()
Created under test_case / directory respectively Baidu search test_baidu.py and proper way search test_youdao.py test files, and writing Web automated test cases in the test file.
runtest.py:
Import the unittest # directory as the current directory defined test test_dir = ' ../test_project/test_case ' Discover = unittest.defaultTestLoader.discover (test_dir, pattern = ' Test * .py ' ) IF the __name__ == " __main__ " : Runner = unittest.TextTestRunner () runner.run (the Discover)
You may have a doubt, report catalog is doing what? Perhaps you have already guessed from the name which is used to store test report, then how can the test results generate a log.txt file it? Here it needs dos command.
First, open the Windows command prompt, enter to perform directory ... / test_project / directory.
Open log.txt file, as follows:
I do not know why, pycharm run no problem, use the cmd command line on the problem.