-
需求
打开百度–点击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'])