课堂练习 1
- 用账号密码的方式登录豆瓣
- 检查登录是否成功
- pytest自动测试框架
Chrome options
https://peter.sh/experiments/chromium-command-line-switches/
高亮显示定位的元素
driver.execute_script(“arguments[0].style.background = ‘rgb(138,43,226 )’;”, element)
判断整个页面是否loader完成。
driver.execute_script(“return((window.jQuery != null) && (jQuery.active === 0))”) == True
initilization/broswerdriver.py
#! /usr/bin/env python
#coding=utf-8
from selenium import webdriver
def build_up_driver():
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--disable-infobars')
driver = webdriver.Chrome(options=chrome_options)
driver.implicitly_wait(10)
driver.get("https://www.douban.com/")
return driver
basepage.py
#! /usr/bin/env python
#coding=utf-8
from initilization import broswerdriver
class BasePageObject:
def __init__(self):
self.driver = broswerdriver.build_up_driver()
def extend_find_element(self,locator):
element = self.driver.find_element(*locator)
self.driver.execute_script("arguments[0].style.background "
"= 'rgb(138,43,226 )';",
element)
return element
def switch_frame_by_element(self,element):
self.driver.switch_to.frame(element)
def get_element_text(self,element):
return element.text
def get_table_content(self,tabel_name):
pass
def set_value(self,element,value):
element.clear()
element.click()
element.send_keys(value)
def click_element(self,element):
element.click()
login.py
#! /usr/bin/env python
#coding=utf-8
from .basepage import BasePageObject
from selenium.webdriver.common.by import By
class LoginPageObject(BasePageObject):
_login_frame = (By.TAG_NAME,'iframe')
_account_login_tab = (By.CLASS_NAME,'account-tab-account')
_username_field = (By.ID,'username')
_password_field = (By.ID,'password')
_login_button = (By.LINK_TEXT,'登录豆瓣')
_my_douban = (By.LINK_TEXT,"我的豆瓣")
def __init__(self):
BasePageObject.__init__(self)
def _locate_login_frame(self):
return self.extend_find_element(LoginPageObject._login_frame)
def switch_to_login_frame(self):
self.switch_frame_by_element(self._locate_login_frame())
def _locate_login_tab(self):
return self.extend_find_element(LoginPageObject._account_login_tab)
def click_login_tab(self):
self.click_element(self._locate_login_tab())
def _locate_username_field(self):
return self.extend_find_element(LoginPageObject._username_field)
def input_user_name(self,user_name):
self.set_value(self._locate_username_field(),user_name)
def _locate_password_field(self):
return self.extend_find_element(LoginPageObject._password_field)
def input_password(self,password):
self.set_value(self._locate_password_field(),password)
def _locate_login_button(self):
return self.extend_find_element(LoginPageObject._login_button)
def click_login_button(self):
element = self._locate_login_button()
self.click_element(self._locate_login_button())
def _locate_my_douban(self):
return self.extend_find_element(LoginPageObject._my_douban)
def get_my_douban_text(self):
return self.get_element_text(self._locate_my_douban())
def login(self,username,password):
self.switch_to_login_frame()
self.click_login_tab()
self.input_user_name(username)
self.input_password(password)
self.click_login_button()
test_login.py
#! /usr/bin/env python
#coding=utf-8
import pytest
from pages.loginpage import LoginPageObject
@pytest.mark.parametrize('username, password',
[('xxxx', 'xxxx')
])
def test_douban(username, password):
login_page_object = LoginPageObject()
'''
#allure.step
login_page_object.switch_to_login_frame()
#allure step
login_page_object.click_login_tab()
#allure step
login_page_object.input_user_name(username)
# allure step
login_page_object.input_password(password)
login_page_object.click_login_button()
'''
login_page_object.login(username,password)
text = login_page_object.get_my_douban_text()
assert text == "我的豆瓣"
目录结构
练习题2 :
练习题2:
- 用PO 思想实现读书页面的pageobject类
- 查询 ”利用Python进行数据分析“的书
- 判断查询成功与否的条件是: 查询结果里面有 ”利用Python进行数据分析“的书,而且作者是”Wes McKinney“
获取整个table 内容
table_source=self.web_driver.find_element_by_id(table_id).get_attribute(‘innerHTML’)