zoukankan      html  css  js  c++  java
  • ui自动化测试 SeleniumBase

    ui自动化 SeleniumBase

      SeleniumBase是一个自动化web测试框架,它的设计pyse相似,基于selenium和unittest封装的框架,api多,支持命令行多参数执行

      文档地址:https://github.com/seleniumbase/SeleniumBase

    下载

      pip下载 

    pip install seleniumbase

      git克隆

    git clone https://github.com/seleniumbase/SeleniumBase.git
    cd SeleniumBase
    pip install -r requirements.txt
    python setup.py develop

      升级

    --upgrade

      卸载

    --force-reinstall  --no-cache-dir

      驱动安装

    seleniumbase install chromedriver
    
    seleniumbase install geckodriver
    
    seleniumbase install edgedriver
    
    seleniumbase install iedriver
    
    seleniumbase install operadriver

    运行

    pytest my_first_test.py --browser=chrome  #如果没有写明浏览器默认为chrome浏览器启动

    官方文档案例

    from seleniumbase import BaseCase
    
    
    class MyTestClass(BaseCase):
    
        def test_basic(self):#必须以test开头
            # 打开网页,参数1:url,默认为chrome浏览器
            self.open("https://xkcd.com/353/")
            '''
                 open()
                     其中有等待时间 
                         open方法中的设置setting.WAIT_FOR_RSC_ON_PAGE_LOADS=True 开启显式等待
                         wait_for_ready_state_complete方法中设置等待时间(长时间等待,默认为30s)  
            '''
    
            # 页面元素断言,参数1:元素定位代码, 参数2:by=By.CSS_SELECTOR,默认css_selector选择器
            # 参数3:timeout=settings.SMALL_TIMEOUT,默认为6s
            self.assert_element('img[alt="Python"]')
            '''
                settings.SMALL_TIMEOUT默认等待时间为6s
                wait_for_element_visible()中自动识别顺序,不填写默认css-->xpath( 判断开头是否为'/','./','(' )-->link( 判断开头是否为'link=','link_text=' )
            '''

    # 点击页面元素,参数1:selector元素定位代码, 参数2:by=By.CSS_SELECTOR, # 参数3:timeout = settings.SMALL_TIMEOUT self.click('a[rel="license"]') ''' settings.SMALL_TIMEOUT默认等待时间为6s wait_for_element_visible()中自动识别顺序,不填写默认css-->xpath-->link 内置纯js,jquery点击页面 ''' # 区域内的断言文本,参数1:text, 参数2:selector="html", 参数3:by=By.CSS_SELECTOR, # 参数4:timeout=settings.SMALL_TIMEOUT self.assert_text("free to copy", "div center") ''' timeout=settings.SMALL_TIMEOUT默认等待时间那为6s wait_for_text_visible()的自动识别,不填写默认css-->xpath( 判断开头是否为'/','./','(' )-->link_text( 判断开头是否为'link=','link_text=' ) ''' self.open("https://xkcd.com/1481/") # 打开另一个网页 # 得到一个属性,参数1:selector, 参数2:attribute获取元素属性值, 参数3:by=By.CSS_SELECTOR, # 参数4:timeout=settings.SMALL_TIMEOUT title = self.get_attribute("#comic img", "title") ''' timeout=settings.SMALL_TIMEOUT默认等待时间那为6s get_attribute()自动识别不填写默认css -->xpath-->link_text ''' # 校验这个字符串是否在这个属性中 self.assert_true("86,400 seconds per day" in title) # 点击链接标签中非属性字符串'Blag' self.click("link=Blag") # 校验字符串"The blag of the webcomic"是否是h2标签的非属性字符串 self.assert_text("The blag of the webcomic", "h2") # 用一个新值更新html中指定元素的值 self.update_text("input#s", "Robots! ") self.assert_text("Hooray robots!", "#content") self.open("https://xkcd.com/1319/") # 该方法类似于assert_text(),但要求文本精确,参数1:text, 参数2:selector="html", 参数3:by=By.CSS_SELECTOR, # 参数4:timeout=settings.SMALL_TIMEOUT self.assert_exact_text("Automation", "#ctitle")

      其他方法:   

    self.go_back() #这个方法将浏览器导航到前面的页面。
    self.go_forward() #这个方法在历史中导航浏览器前进。
    self.refresh_page() #此方法将重新加载当前页面。
    这个方法返回当前页面的URL。
    get_page_source() #这个方法返回当前页面源。
    get_text() #从页面上的元素获取文本
    wait_for_element_present("div.my_class", timeout=10) #在数秒内断言某个元素在页面上的存在
    
    wait_for_element_visible("a.my_class", timeout=5) #在数秒内断言页面上元素的可见性
    
    find_element("a.my_class", timeout=5).click() #在java中可以直接使用.click()直接找到元素进行点击,同样这里也可以
    
    在css选择器中如div[class="class_name"],可以使用div.class_name)简单化
    
    is_element_visible(selector) #是页面上可见的元素
    is_element_present(selector) #是页面上的一个元素
    is_text_visible(text, selector) #是页面上可见的文本
    switch_to_window() #该选项卡切换到new选项卡(切换到新建一个页面)
    
    switch_to_frame()#进入iframe
    switch_to_default_content() #进入别忘了退出iframe
    
    测试时弹出警告框:
    wait_for_and_accept_alert() #接收
    wait_for_and_dismiss_alert() #拒绝
    
    self.activate_jquery()#在尚未加载jquery的页面使用jquery
    delayed_assert_element()和delayed_assert_text()将保存将引发的任何异常。要将所有失败的延迟断言清除到一个异常中,请确保在测试方法的末尾调用self.process_delayed_assert()。如果您的测试到达多个页面,您可以在单个页面的所有延迟断言的末尾调用self. process_delayed_()。通过这种方式,日志文件中的屏幕快照将具有生成延迟断言的位置。

      校验邮件的案例:

        假设您有一个发送电子邮件的测试,现在您想检查电子邮件是否已收到

    from seleniumbase.fixtures.email_manager import EmailManager, EmailException
    num_email_results = 0
    email_subject = "This is the subject to search for (maybe include a timestamp)"
    email_manager = EmailManager("{YOUR SELENIUM GMAIL ACCOUNT EMAIL ADDRESS}")  # the password for this would be stored in seleniumbase/config/settings.py
    try:
        html_text = email_manager.search(SUBJECT="%s" % email_subject, timeout=300)
        num_email_results = len(html_text)
    except EmailException:
        num_email_results = 0
    self.assert_true(num_email_results)  # true if not zero
    View Code

      使用jquery的案例

    self.execute_script('jQuery, window.scrollTo(0, 600)')  # Scrolling the page
    
    self.execute_script("jQuery('#annoying-widget').hide()")  # Hiding elements on a page
    
    self.execute_script("jQuery('#hidden-widget').show(0)")  # Showing hidden elements on a page
    
    self.execute_script("jQuery('#annoying-button a').remove()")  # Removing elements on a page
    
    self.execute_script("jQuery('%s').mouseover()" % (mouse_over_item))  # Mouse-over elements on a page
    
    self.execute_script("jQuery('input#the_id').val('my_text')")  # Fast text input on a page
    
    self.execute_script("jQuery('div#dropdown a.link').click()")  # Click elements on a page
    
    self.execute_script("return jQuery('div#amazing')[0].text")  # Returns the css "text" of the element given
    
    self.execute_script("return jQuery('textarea')[2].value")  # Returns the css "value" of the 3rd textarea element on the page
    View Code

    注意事项:

      1.selenium iumbase自动处理常见的web驱动程序操作,比如在测试失败时启动web浏览器和保存屏幕截图,并且会保存latest_logs/文件夹中。如果在设置中将ARCHIVE_EXISTING_LOGS设置为True,那么这些日志将被移动到archived_logs/。,否则日志文件将在下一次测试运行开始时被清除。)

      2.settings.py设置,setting中有许多配置,如

          选择要使用的测试浏览器(默认:Chrome)

          选择betweeen pytest &nose unittest runners

          选择是否进入调试模式失败选择额外变量进入测试

          选择浏览器的用户代理使用自动化改变速度(演示模式)

          选择多线程运行测试

          选择是否重试失败测试

          选择BrowserStack服务器上运行

          选择的Sauce Labs server上运行

          选择TestingBot服务器上运行

          选择CrossBrowserTesting服务器

          选择一个selenium Grid连接选择数据库保存结果

          选择一个代理服务器连接 等

      3. 运行命令行案例

    pytest my_first_test.py
    
    pytest my_first_test.py --demo_mode --browser=chrome  
    
    pytest my_first_test.py --browser=firefox
    
    pytest test_suite.py --html=report.html
    
    nosetests test_suite.py --report --show_report
    
    pytest test_suite.py --headless -n 4
    
    pytest test_suite.py --reruns 1 --reruns-delay 2
    
    pytest test_suite.py --server=IP_ADDRESS --port=4444   
    
    pytest proxy_test.py --proxy=IP_ADDRESS:PORT
    
    pytest proxy_test.py --proxy=USERNAME:PASSWORD@IP_ADDRESS:PORT
    
    pytest user_agent_test.py --agent="USER-AGENT STRING"
    
    pytest test_fail.py --pdb -s

    官方建议使用pytest

      --demo_mode 演示模式

        使脚本执行的过程变得很慢,而且还会让操作的元素高亮显示,方便你查看和定位问题,并且可以在settings.py中设置demo_sleep设置等待时间

        或者:

    pytest my_first_test.py --browser=chrome --demo_mode --demo_sleep=1.2

      -n 4

        开启4个线程为你工作,这个取决于你cpu的颗粒数

      --reruns 5 --reruns-delay 1

        重试测试失败的案例5次,并且在重试期间等待多少秒

      --pdb -s

        在调试Selenium脚本的时候,我们希望错误时可以暂停脚本  

        当出现错误时,你可以继续输入命令:

        “c”:继续

        “s”:步骤

        “n”下一步

      --report  生成一个报告

      --show_report  在测试套件完成后立即显示Nosetest报告。只在本地运行测试时使用——show_report,因为它会暂停测试运行

      

      4.在jenkins可插件可实现自动化集成 Xvfb headless browser plugin

  • 相关阅读:
    程序返回插入数据库成功,但是数据库内却没有数据
    C++ 使用动态二维数组参数
    深入理解.Net中的内存释放,以及有关的注意事项
    用数据集时,错误:未能启用约束。一行或多行中包含违反非空、唯一或外键约束的值
    关于堆和栈
    C#加密方法总汇
    const与readonly
    struts 将上传文件保存到数据库中
    java Annotation注解的运用
    转:获取汉字的拼音(包括一级和二级)
  • 原文地址:https://www.cnblogs.com/RainBol/p/10906654.html
Copyright © 2011-2022 走看看