Selenium实战丨第一个PageObject


因为飞猪使用selenium需要登录,过于繁琐,所以我将网站更改为携程网。只是一个简单的测试,确保我们PageObject流程能够正常走通。

以下为代码

PageObject/search_ticket.py

from Base.base_function import Base
from Common.get_function import get_project_path
from Common.Excle import load_excle


class Search_Air_Ticket(Base):

    def test_data(self):
        self.data = load_excle(f'{get_project_path()}/Data/TestData.xlsx', '测试数据')

    def trains_model(self):
        # 火车模块
        self.e_click('ie', 'nav_trains')

    def departure_city(self, value):
        # 出发城市
        self.e_send_key('ie', 'departCityName', value)

    def arrival_city(self, value):
        # 到达城市
        self.e_send_key("ie", 'arriveCityName', value)

    def dep_date(self, value):
        # 出发日期
        self.driver.execute_script("document.getElementById('departDate').removeAttribute('readonly')")
        self.e_send_key("ie", 'departDate', value)

    def search_button(self):
        # 搜索按钮
        self.e_click('cne', 'searchbtn')

    def main(self):
        self.get_url()
        self.trains_model()
        self.departure_city("北京")
        self.e_clear_select()
        self.arrival_city("上海")
        self.e_clear_select()
        self.dep_date("2020-09-01")
        # self.end_date("2020-09-02")
        self.e_clear_select()
        self.search_button()


if __name__ == '__main__':
    a = Search_Air_Ticket()
    a.main()