浏览器中打开多个窗口,窗口间进行切换

  • 需求
    打开百度–点击hao123–在hao123中点击‘人民网’,之后切换到第一次打开的百度窗口,之后输入jd.com

  • 目录
    在这里插入图片描述

  • 代码

'''conftest.py内容'''
#-*-coding:utf-8-*-
import pytest
from selenium import webdriver
@pytest.fixture(scope='session')
def d(request):
    driver=webdriver.Chrome()
    def end():
        driver.quit()
    #这里为什么不用yield呢因为yield不能return,addfinalizer这个功能可以实现饿yield功能一样,而且可以return参数传递给后面的用例
    request.addfinalizer(end)
    return driver

'''test_one.py文件中的测试用例'''
#-*-coding:utf-8-*-
import os
import time

import pytest
from selenium.webdriver.common.by import By

class TestOne:
    def test_baidu(self,d):
        d.get('https://www.baidu.com')
        time.sleep(5)
        t=d.title
        assert "百度一下,你就知道"==t
    def test_hao(self,d):
        time.sleep(2)
        d.find_element(By.XPATH,'//*[text()="hao123"]').click()
        #设置当前窗口为主窗口
        mainWindow = d.current_window_handle
        #切换到新打开的窗口
        for h in d.window_handles:
            d.switch_to.window(h)
            if 'hao123' in d.title:
                time.sleep(3)
                d.find_element(By.XPATH, '//*[text()="人民网"]').click()
                time.sleep(3)
                for g in d.window_handles:
                    d.switch_to.window(g)
                    if '人民网' in d.title:
                        d.switch_to.window(mainWindow)
                        time.sleep(3)
        assert '百度一下' in d.title
        
'''test_two.py文件中的测试用例'''
#-*-coding:utf-8-*-
import time

class TestTwo:
    def test_jd(self,d):
        d.get('http://www.jd.com')
        time.sleep(5)
        t = d.title
        print('测试标题:', t)
        assert t=='京东(JD.COM)-正品低价、品质保障、配送及时、轻松购物!'
 
'''run.py文件运行用例'''
#-*-coding:utf-8-*-
import pytest

if __name__ == '__main__':
    pytest.main(['-vs','./pytestBase'])
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值