• Python_selenium之窗口切换(二)


    Python_selenium之窗口切换(二)
    一、思路拆分
    1. 之前有介绍窗口切换,这里加上断言部分
    2. 这里还是以百度新闻为例,获取百度新闻网址http://news.baidu.com/
    3. 同样的获取所有的句柄,然后获取当前的句柄,同之前操作差不多
    4. 最后加上断言部分,page2的信息内容是否包含page1,这里采用assert in语句
    二、测试脚本
    1. 测试脚本如下:
    #coding:utf-8
    from selenium import webdriver
    import time

    driver=webdriver.Chrome()
    driver.maximize_window()
    driver.implicitly_wait(8)

    driver.get("http://news.baidu.com/")
    ele=driver.find_element_by_xpath("//*[@id='pane-news']/div/ul/li/strong/a[contains(text(),'全球')]")
    page1=ele.text
    ele.click()
    handles=driver.window_handles#获取当前浏览器的所有句柄
    for handle in handles:
    if handle != driver.current_window_handle:
    print "switch to second windows",handle
    driver.close()
    driver.switch_to.window(handle)
    page2=driver.find_element_by_xpath("//*[@class='subject']/h2").text
    print "page1.text=",page1
    print "page2.text=",page2
    # if page1==page2:
    # print "test sucess"
    # else:
    # print "test failed"
    try:
    assert page1 in page2
    print "test sucess"
    except Exception as e:
    print "test failed"
    2. 测试结果如下图1所示

    3. 上述测试脚本中,备注部分运用了if语句,但是,定位到百度新闻首页获取好的元素信息与新开的页签下面的信息内容不是完全一致,所以这里运用assert in语句比较合适。

  • 相关阅读:
    Web安全测试
    性能测试---并发用户计算
    浅谈软件性能测试中关键指标的监控与分析
    fiddler抓包——IOS
    Linux下查看CPU型号,内存大小,硬盘空间,进程等的命令(详解)
    windows 升级pip
    java 使用Iterator 迭代器遍历AList、Set、Map
    springboot 封装redis工具类
    idea 取消@Autowired 不建议字段注入的警告
    查看服务器相关
  • 原文地址:https://www.cnblogs.com/Rita-LJ/p/7965971.html
Copyright © 2020-2023  润新知