zoukankan      html  css  js  c++  java
  • e2e 测试(1)

     距离上一随笔,已经有一个月没有没写。到今天,刚刚好好,是学习e2e测试的一个月。今天有点时间可以总结一下这个月来的收获。

     1、搭建e2e的测试环境

       我是使用 Vue 构建项目,所以我也是通过Vue-cli生成已经包含 Selenium 与 Nightwatch 的 Vue 种子项目。

      setup e2e tests with Nightwatch?Yes(单元测试需要)

    2、e2e的测试目录

    --e2e

     --custom-assertions(自定义断言)

     --reports(生成的测试报告存放位置)

     --specs(测试代码)

     --nightwatch.conf.js(配置文件)

    3、nightwatch.conf.js(配置文件)

    require('babel-register')
    var config = require('../../config')
    
    // http://nightwatchjs.org/gettingstarted#settings-file
    module.exports = {
      src_folders: ['test/e2e/specs'],//测试用例源文件路径
      output_folder: 'test/e2e/reports',//测试报告地址
      custom_assertions_path: ['test/e2e/custom-assertions'],// 自定义命令,这里用来更新测试信息到custom-assertions
      custom_commands_path : "",
      page_objects_path : ['test/e2e/page-objects'],
      globals_path : "",
    //selenium配置
      selenium: {
        start_process: true,//是否开启
        server_path: require('selenium-server').path,//路径
        host: '127.0.0.1',
        port: 4444,      //端口
        cli_args: {            //cli_args指定将要运行的webdriver
          'webdriver.chrome.driver': require('chromedriver').path
        }
      },
     // 测试配置
      test_settings: {
        default: {
          selenium_port: 4444,//指定 selenium server 接受的端口。。
          selenium_host: 'localhost',//指定 selenium server 接受的 hostname/IP。
          silent: true,//是否显示 selenium 命令日志。
          use_xpath:true,//是否用 xpath 做为默认的元素选择策略。
          globals: {//在测试代码中可以访问的全局变量,并且每次切换测试环境时可以重写该值。例如: *”globals” :{“myGlobal” : “some_global”}
            devServerURL: 'http://localhost:' + (process.env.PORT || config.dev.port)
          },
          screenshots : {
            "enabled" : false,
            "path" : ""
          },
        },
    
        chrome: {
          desiredCapabilities: {
            browserName: 'chrome',
            javascriptEnabled: true,
            acceptSslCerts: true,
            "chromeOptions": {
              "args" : ["--no-sandbox"]
            }
          }
        },
    
        firefox: {
          desiredCapabilities: {
            browserName: 'firefox',
            javascriptEnabled: true,
            acceptSslCerts: true
          }
        }
      }
    }

    从上面代码可以看到有三种配置,分别是:Basic settings,Selenium settings和Test settings。我们下面详细看看这三种配置主要有哪些配置信息。

    (1)Basic settings

    Nametype
      defaultdescription
    src_folders string|array none 测试代码目录(不包含子目录)
    output_folder (optional) string tests_output 生成的测试报告存放目录
    custom_commands_path (optional) string|array none Location(s) where custom commands will be loaded from.
    custom_assertions_path (optional) string|array none 自定义断言路径
    page_objects_path (Optional since v6.0.1) string|array none Location(s) where page object files will be loaded from.
    globals_path (optional) string none 外部模块路径,为测试一共全局变量. 也可在test_settings中重写
    selenium (optional) object   Selenium Server相关的设置
    test_settings (optional) object   与测试相关的测试,下面有详细描述
    live_output (optional) boolean false 是否缓存并持续输出结果
    disable_colors (optional) boolean false 控制命令行输出是否带有高亮颜色
    parallel_process_delay (optional) integer 10 在并行模式下启动子进程的时间,单位毫秒
    test_workers boolean|object false 是否为运行单个文件测试启动并行模式,如果设置为true,会进入并行模式,并自动设置线程数。如果设置为对象,要指定enable和workers参数,workers接受数值和’auto’。 例如:”test_workers” : {“enabled” : true, “workers” : “auto”}
    test_runner (optional) string|object “default”

    用什么工具运行测试。值可以是”default”或”mocha”. 例如:”test_runner” : {“type” : “mocha”, “options” : {“ui” : “tdd”}}

     (2)Selenium settings

    如下是 selenium 的配置选项。Nightwatch可以自动管理 selenium 服务进程,以便你专注于测试工作。 如果要启用 selenium自启动,设置 start_process 为true 并设置 server_path 值为 selenium jar包路径。

    Nametypedefaultdescription
    start_process boolean false 是否启用 selenium自启动
    start_session boolean true 是否自动启动 Selenium session.
    server_path string none selenium jar 包路径。如果设置了 start_process 字段,必须设置此字段。例如: node_modules/selenium-server/lib/runner/selenium-server-standalone-2.53.0.jar
    log_path string|boolean none selenium 生成的 output.log 文件存放位置,默认为当前目录下。设置false可以禁用 Selenium 日志。
    host string 127.0.0.1 设置 selenium 监听地址。不常用到,除非指定了 start_process 为true。
    port integer 4444 设置 selenium 监听端口
    cli_args object none 一系列传递给 selenium 的命令行参数。 有许多选项可以设置, 例如:
          webdriver.firefox.profile: 默认情况下会为每个会话创建一个firefox配置文件,如果想要使用现有配置文件,在这里指定文件名。firefox驱动参数的完整列表见这里
          webdriver.chrome.driver: Nightwatch 也可以在 Chrome 上运行测试,如果要在 chrome 上运行测试需要下载 ChromeDriver binary 并为此字段设置 ChromeDriver 路径。同时要在 desiredCapabilities 对象配置中指定 browserName值为 “chrome”。 更多信息参考ChromeDriver website
          webdriver.ie.driver: Nightwatch同时也支持Internet Explorer,要在IE中运行测试,需要下载 IE Driver binary 并为此字段设置 IE Driver 路径。同时要在 desiredCapabilities 对象配置中指定 browserName值为 “internet explorer”。

    (3)Test settings

    只有”default”配置是必须的,其他配置可以按需要覆盖default中的配置信息。
    测试环境可以通过 -env 参数传递给 nightwatch:

    nightwatch --env integration
    Nametypedefaultdescription
    launch_url string none 测试时要加载的首页url, 如果有多个测试环境,可以分别指定url。
    selenium_host string localhost 指定 selenium server 接受的 hostname/IP。
    selenium_port string 4444 指定 selenium server 接受的端口。
    silent boolean true 是否显示 selenium 命令日志。
    output boolean rue 是否在命令行显示完整输出。
    disable_colors boolean false 命令行输出是否高亮。
    firefox_profile string|boolean none 已经弃用
    chrome_driver string none 已经弃用
    ie_driver string none 已经弃用
    screenshots object none 当发生错误时 Selenium 会生成屏幕截图。如果 on_failure 设置为 true, 发生错误或没有通过测试时也生成屏幕截图。
          从 v0.7.5 版本开始,可以为”on_error”字段设置false来禁止错误时生成截图。
          例如:“screenshots”:{“enabled”:true,”on_failure”:true,”on_error”: false,”path”: “”}
    username string none 万一 selenium 需要凭证,该字段用来生成 Authorization header。值可以是系统变量,例如:”username” : “${SAUCE_USERNAME}”
    access_key string none 与 username 一样用于生成 Authorization header。像 username 一样,值也可以是系统变量。
    proxy string none 使用代理访问 selenium server。支持 http, https, socks(v5), socks5, sock4, 和 pac。使用node-proxy-agent。Example: http://user:pass@host:port
    desiredCapabilities object   在新建 session 之前传递给 Selenium WebDriver,可以用来指定浏览器名称和其他功能。例如:“desiredCapabilities” : {“browserName” : “firefox”, “acceptSslCerts” :true}。 完整的功能列表在这里
    globals object   在测试代码中可以访问的全局变量,并且每次切换测试环境时可以重写该值。例如: *”globals” :{“myGlobal” : “some_global”}
    exclude array   不包含的文件夹,接受字符串或模式字符串(relative to the main source folder)。例如:”exclude” : [“excluded-folder”] 或 :”exclude” : [“test-folder/*-smoke.js”]
    filter string   接受字符串或模式字符串,与之不匹配的文件会被忽略。
    log_screenshot_data boolean false 是否在日志(verbose模式)中记录屏幕截图的base64编码信息。
    use_xpath boolean false 是否用 xpath 做为默认的元素选择策略。
    cli_args object none 作用与 selenium 配置中的 cli_args 相同。 你可以在不同的测试环境配置中覆盖 selenium 中的配置。
    end_session_on_fail boolean true 在测试终止的时候,自动关闭会话,通常会在断言失败后触发。
    skip_testcases_on_fail boolean true 是否在任意测试用例测试失败后,跳过剩余的测试用例。
    output_folder string|boolean   生成的测试报告存放目录, 该值覆盖 Basic settings 中的配置, 也可以设置为 false 不生成报告。
    persist_globals boolean false Weather or not to persist use the same object instance for holding globals between testsuite runs or a (deep) copy of it.
  • 相关阅读:
    nginx 配置https详细步骤
    Git 上传本地仓库到远程git仓库
    VUE 配置vscode关于vue插件
    ORA-01439:要更改数据类型,则要修改的列必须为空
    Oracle查看主键、删除主键、添加联合主键
    std::stoi, std::stol, std::stoll
    C+++string类如何判断字符串为空
    1day漏洞反推技巧实战(1)
    java反射笔记,自用
    tomcat Valve内存马
  • 原文地址:https://www.cnblogs.com/qiu-Ann/p/7693279.html
Copyright © 2011-2022 走看看