下面将以登陆页面为例:
//src/pages/login_page.py
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import sys
sys.path.append("../..")
from src.common.BasePage import BasePage
class LoginPage(BasePage):
#locator
username_loc = (By.NAME, 'username')
password_loc = (By.NAME, 'password')
username_shown_loc = (By.XPATH, '/html/body/div[1]/div/div[3]/span')
def __init__(self, selenium_driver, base_url):
super(LoginPage, self).__init__(selenium_driver, base_url)
#open page
def open_page(self):
self._open_page(self.url)
#input username
def input_username(self, username):
self.find_element(*self.username_loc).send_keys(username)
#input password
def input_password(self, password):
self.find_element(*self.password_loc).send_keys(password)
#click login button or Return button
def click_login(self):
self.find_element(*self.password_loc).send_keys(Keys.RETURN)
def check_username_shown_correct(self, username):
"""
check if the given username is included in the user info area, return True, otherwise return False
"""
return self.check_page_loaded_correct(username, *self.username_shown_loc)
if __name__ == '__main__':
driver = webdriver.Firefox()
url = 'https://www.baidu.com'
login_page = LoginPage(driver, url)
login_page.open_page()
对上述代码需要解释的有几下几点:
1. 此页面类 以BasePage为父类,因此需要from src.common.BasePage import BasePage, 而import若想要找到这个BasePage库,则在此之前必须添加如下代码,
import sys
sys.path.append("../..")
意为将当前目录的父目录的父目录,即C:\Users\xxx\Desktop\test\unittest_lars 添加到环境变量中,同时还要再\src\pages\目录下添加一个__init__.py,内容为空即可,这样系统才能找到这个BasePage库。
2. #locator 部分,将该页面所有需要的元素的定位方法罗列出来:
username_loc = (By.NAME, 'username') #用户名输入框
password_loc = (By.NAME, 'password') #密码输入框
username_shown_loc = (By.XPATH, '/html/body/div[1]/div/div[3]/span') #登陆后用户名显示区,以验证用户登陆后显示的用户名是否正确
元素定位方式有很多方法,网上搜索到最多的就是如下8种方式:
- id定位:find_element_by_id(self, id_)
- name定位:find_element_by_name(self, name)
- class定位:find_element_by_class_name(self, name)
- tag定位:find_element_by_tag_name(self, name)
- link定位:find_element_by_link_text(self, link_text)
- partial_link定位find_element_by_partial_link_text(self, link_text)
- xpath定位:find_element_by_xpath(self, xpath)
- css定位:find_element_by_css_selector(self, css_selector)
而下面两种方式的资料相对较少:
- find_element(self, by=By.ID, value=None)
- find_elements(self, by=By.ID, value=None)
但若将find_element封装的的话,还是后面两种方式更方便些。而by参数有以下几种选项:ID,NAME,LINK_TEXT,PARTIAL_LINK_TEXT,TAG_NAME,XPATH,CLASS_NAME,CSS_SELECTOR
(注意:全部大写)
而这些通过webdriver源码C:\Python27\Lib\site-packages\selenium\webdriver\remote\webdriver.py 也可以查看到
find_elements_by_xx 调用的实际上就是方法find_element(by=By.xx, value=xx)
同样find_element(by=By.xx, value=xx) 源码中也给出了用法。
3. 最后,若要单独测试当前脚本是否能运行成功,添加了如下代码,只是验证了当前脚本能够打开浏览器,并未添加输入用户名密码的步骤
if __name__ == '__main__':
driver = webdriver.Firefox()
url = 'https://www.baidu.com'
login_page = LoginPage(driver, url)
login_page.open_page()