我简单描述下:
1、创建了2个页面,登录和主页
2、然后在一个用例里面,需要登陆后操作别的,
问题就出在这里,始终是打开2个driver,后面一直无法操作,求大佬解析,网上也找不到对应的方法
你截图下Login()和Index()所在的page页看看
老师求指导
你现在是想登录之后跳转到Index页面吗?是的话那你login函数那里需要返回的是Index的类名+传一个driver过去呀,类似return Index(self._driver)
不行,还是会弹出2个浏览器,。
看下你BasePage咋写的
能在gitee上看到你的源码不。我下载看看
self._driver.get(self.url) ----> 这一步就是打开浏览器的操作。放在BasePage里面看看
请把代码上传至gitee 或者把basepage所有代码贴出来
各位老师,我把流程重写贴一下,我都混乱了,目前总结看来,楼上各位老师有2种方法
1、将driver_get(url),写到base_page里面,而不是每个page页,这样可以解决未来多页面流转,但是我并没解决
2、将driver初始化在每个测试用例中,而不是在base_page写死,但是我也不清楚,现在请教各位老师,
我的目的:实现单页面、多页面测试时,只有一个driver,并且暂时不产生多的
下面是源码,用例1是单页面(正常),用例2是多页面(困难是每次都会产生多个driver,而不是一个)
base_page
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: yhz
# Time: 2021/5/20 17:06
import os
import time
from pathlib import Path
from conf.config import CONF_URL
from utils import time_unit
from selenium import webdriver
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.common.keys import Keys # 提供键盘按键的支持
class BasePage:
"""
初始化driver数据
"""
def __init__(self, driver: WebDriver = None):
print("正在初始化" + self.__class__.__name__ + "页面...")
# driver复用,如果不存在就构造一个新的
if driver is None:
# 忽略:Chrome正在受到自动软件的控制 警告
option = webdriver.ChromeOptions()
option.add_argument('disable-infobars')
# 移动端配置
# options = webdriver.ChromeOptions()
# options.add_argument(
# 'User-Agent=Mozilla/5.0 (Linux; U; Android 4.0.2; en-us; Galaxy Nexus Build/ICL53F) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30')
# self._driver = webdriver.Chrome(chrome_options=options)
# 设置浏览器大小,让它看起来跟手机的样式差不多。
# driver.set_window_size("380", "680")
# web端配置
self._driver = webdriver.Chrome()
# webdriver.Chrome(chrome_options=option,executable_path="./../../utils/driver_exe/chromedriver.exe")
# 最大化窗口
# self._driver.maximize_window()
# 设置隐式等待时间,全局等待超时时间
self._driver.implicitly_wait(10)
# from conftest import debug_check
# debug_check("断点")
# 访问网页
self._driver.get(self._base_url)
else:
# 避免重复构造driver
self._driver = driver
# # 获得当前窗口
# nowhandle = driver.current_window_handle
# # 获得所有窗口
# allhandles = driver.window_handles
# # 循环判断窗口是否为当前窗口
# for handle in allhandles:
# if handle != nowhandle:
# driver.switch_to(handle)
# print('now register window!')
def save_scren(self, name):
"""
截图方法
:param name: 参数名
:return:
"""
time.sleep(1)
# 判断文件夹是否存在,不存在则创建
if Path(CONF_URL).is_dir():
pass
else:
Path(CONF_URL).mkdir()
names = name + str(time_unit.now_time())
path_in = os.path.join(CONF_URL, names + '.png')
print(path_in)
self._driver.get_screenshot_as_file(path_in)
def close(self):
"""
关闭driver
:return:
"""
time.sleep(2)
self._driver.quit()
"""
固话常用函数,为页面对象类进行服务
"""
# def get(self, url):
# """
# 收缩所有URL,避免请求时URL未加载完导致数据索引不到
# :param url: werdriver.get(url)
# :return:
# """
# #
# self._driver.get(url)
# time.sleep(2)
# def locatoor(self, loc):
# return self.driver.find_element(*loc)
# 输入
# def input_(self, loc, txt):
# self.locatoor(loc.txt)
# 点击
# def click(self, loc):
# self.locatoor(loc).click()
# 等待
def wait(self, time):
time.sleep(time)
@property
def get_source(self):
"""获取页面源代码"""
return self._driver.page_source
def input_text(self, locator, txt):
"""输入(输入前先清空)"""
time.sleep(0.5)
ele = self._driver.find_element(locator)
ele.clear()
ele.send_keys(txt)
def refresh(self):
"""刷新页面F5"""
self._driver.refresh()
self._driver.implicitly_wait(30)
login
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: yhz
# Time: 2021/5/20 17:11
import time
from selenium import webdriver
from base.base_page import BasePage
from base_page.index import Index
from base_page.register import Register
from selenium.webdriver.common.by import By
class Login(BasePage):
# 核心元素
_base_url = "/"
# 核心业务流
def login(self, username, password):
# 登录
# self._driver.get(self._url)
time.sleep(3)
self._driver.find_element(By.ID, "username").send_keys(username)
self._driver.find_element(By.ID, "password").send_keys(password)
self._driver.find_element(By.ID, "subReal").click()
return self
def forgot_psd(self):
# 忘记密码/Forgot password?
self._driver.get(self._url)
self._driver.find_element(By.ID, "forgetPassword").click()
return Register(self._driver)
# 错误登录
def login_error(self):
# 获取警告信息
time.sleep(0.5)
tips_message = self._driver.find_element_by_xpath('//*[@id="fm1"]/div/span').text
return tips_message
# def get_error_message(self):
# # 收集错误信息并返回,貌似不管用
# result = []
# for element in self._driver.find_elements(By.CSS_SELECTOR, ".js_error_msg"):
# result.append(element.text)
# print(result)
if __name__ == '__main__':
driver = webdriver.Chrome()
username = "111"
pas = "222"
lp = Login(driver)
lp.forgot_psd()
index
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: yhz
# Time: 2021/6/8 19:22
from selenium import webdriver
from base.base_page import BasePage
from base_page.register import Register
from selenium.webdriver.common.by import By
class Index(BasePage):
"""
首页
"""
_url = ""
def menu(self):
self._driver.get(self._url)
self._driver.find_element(By.LINK_TEXT, "内推职位广场")
self._driver.find_element_by_link_text("审批中心").click() # 文字搜索方式
return self# !/usr/bin/env python
测试用例1
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: yhz
# Time: 2021/5/21 15:52
from time import sleep
import allure
import pytest
from selenium import webdriver
from base_page.login import Login
# from page.index import Index
from utils.opertaiton_unit import yaml_re
class Test_Login: # 所有步骤前的初始化
def setup(self):
# self._driver = webdriver.Chrome()
self.login = Login() # 对注册功能的测试
@allure.story("登录功能")
@allure.title("空密码登录登录")
@pytest.mark.parametrize("datas", yaml_re("./../../conf_yaml/login.yaml"))
def test_login(self, datas): # 进入index,然后进入注册页填写信息
# self.login.save_scren("未登录")
self.login.login(datas["username"], datas["password"]) # 对login功能的测试
# print("来个提示", self.login.login_error())
assert self.login.login_error() is None # 这里不知道该咋判断
# self.assertNotEqual(self.login._driver.switch_to.active_element.text,"bcuowu")
sleep(1)
# assert
# self.login.save_scren("已登录")
def teardown(self):
self.login.close()
if __name__ == '__main__':
pytest.main(["-s", "-v", "1_登录_test.py"])
测试用例2
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: yhz
# Time: 2021/5/21 15:52
import os
from time import sleep
import pytest
from selenium import webdriver
from base_page.index import Index
from base_page.login import Login
# from page.index import Index
from utils.opertaiton_unit import yaml_re
class Test_Login: # 所有步骤前的初始化
def setup(self):
self._driver=webdriver.Chrome()
self.login = Login(driver=self._driver) # 对注册功能的测试
self.index = Index(driver=self._driver)
def test_login(self): # 进入index,然后进入注册页填写信息
self.login = Login(self._driver) # 对注册功能的测试
# self.login = Login() # 对注册功能的测试
# self.login.save_scren("未登录")
sleep(3)
self.login.login("fengqiuhuang", "12345678") # 对login功能的测试
# self.index = Index()
self.index = Index(self._driver)
self.index.menu()
# self.index.menu()
# assert "请选择" in "|".join(relogin.get_error_message())
# self.assertNotEqual(self.login._driver.switch_to.active_element.text,"bcuowu")
sleep(1)
# assert
# self.login.save_scren("已登录")
def teardown_class(self):
self.login.close()
if __name__ == '__main__':
pytest.main(["-s", "-v", os.path.abspath(__file__)])
self.login = Login(driver=self._driver) # 对注册功能的测试
self.index = Index(driver=self._driver)
测试用例2
这两句再setup函数出现了,在test_login又出现了一次。实例化了两次。
把test_login中的这两句去掉试试。