Selenium实战丨第一个TestCase


以下是TestCase的代码,

TestCase/test_search_ticket.py

import unittest
from time import sleep
from PageObject.search_ticket import Search_Air_Ticket


class SearchTicket(unittest.TestCase):

    def setUp(self) -> None:
        self.driver = Search_Air_Ticket()
        self.driver.get_url()

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

    def test_01_search_ticket(self):
        self.driver.trains_model()
        self.driver.departure_city("北京")
        self.driver.arrival_city("上海")
        self.driver.dep_date("2020-09-01")
        self.driver.search_button()
        sleep(2)
        result = self.driver.search_result_assert()
        # print可以查看获取的是什么
        print(result)
        self.assertIn('北京', result)
        self.assertIn('上海', result)
        self.assertIn('2020-09-01', result)


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

在    search_ticket的pageobject中新增一个断言要判定元素定位

PageObject/search_ticket.py


...
    def search_result_assert(self):
        return self.find_e('cne', 'trainList_title').text
...

同时可以删除该PageObject的main方法。