You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Karate Framework中多浏览器顺序执行Feature文件并避免配置重复

Karate Framework多浏览器执行优化:避免重复Examples配置

问题描述

作为Karate Framework新手,我需要在Chrome、Edge、Firefox浏览器上执行同一个Feature文件内的多个Scenario Outline。目前每个Scenario Outline后都重复编写了相同的Examples配置,示例代码如下:

Feature: Login test

Background:
#* configure driver = {type: 'msedge', headless: true}
#* configure driver = [{type: 'msedge'}, {type: 'chrome', executable:'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe'}]
* def element = read('classpath:Test/FunctionalAutomation/Locators/LoginWebelement.json')
* def data = read ('classpath:Test/FunctionalAutomation/TestData/DataLogin.json')

  Scenario Outline: Login to application with positive and negative scenario
    #With correct p/w
    * configure driver = {type: '<conf>', executable: '<exePath>'}
    Given driver "https://www.saucedemo.com/"
    And input(element.x_username, data.txt_username)
    And input(element.id_password, data.txt_password)
    When click(element.id_loginButton)
    Then print driver.title
    Then match driver.title == 'Swag Labs'
    Examples:
      | conf   | exePath                                                           |
      | msedge | C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe |
      | chrome | C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe        |


  Scenario Outline: With invalid p/w
    * configure driver = {type: '<conf>', executable: '<exePath>'}
    Given driver "https://www.saucedemo.com/"
    And input(element.x_username, data.txt_username)
    And input(element.id_password, data.txt_wrongPassword)
    When click(element.id_loginButton)
    Then print text(element.x_errorMessageText)
    Then match text(element.x_errorMessageText) == data.err_msg_incorrectCredential
    Examples:
      | conf   | exePath                                                           |
      | msedge | C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe |
      | chrome | C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe        |

请问是否有办法避免在每个Scenario后重复编写相同的Examples,同时实现多浏览器执行?


解决方案

方法1:在Background中配置多驱动数组,自动遍历浏览器

Karate支持在configure driver时传入浏览器配置数组,整个Feature内的所有场景会自动在每个浏览器上执行,无需重复编写Examples,也不需要使用Scenario Outline。

修改后的代码:

Feature: Login test

Background:
  # 配置多浏览器驱动数组,包含所有需要测试的浏览器
  * configure driver = [
    {type: 'msedge', executable: 'C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe'},
    {type: 'chrome', executable: 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe'},
    {type: 'firefox'} # Firefox路径默认时可省略executable参数
  ]
  * def element = read('classpath:Test/FunctionalAutomation/Locators/LoginWebelement.json')
  * def data = read ('classpath:Test/FunctionalAutomation/TestData/DataLogin.json')

  Scenario: Login with correct password
    Given driver "https://www.saucedemo.com/"
    And input(element.x_username, data.txt_username)
    And input(element.id_password, data.txt_password)
    When click(element.id_loginButton)
    Then print driver.title
    Then match driver.title == 'Swag Labs'

  Scenario: Login with invalid password
    Given driver "https://www.saucedemo.com/"
    And input(element.x_username, data.txt_username)
    And input(element.id_password, data.txt_wrongPassword)
    When click(element.id_loginButton)
    Then print text(element.x_errorMessageText)
    Then match text(element.x_errorMessageText) == data.err_msg_incorrectCredential

方法2:提取共享Examples到外部文件,通过read复用

如果需要保留Scenario Outline结构,可以把重复的浏览器配置提取到单独的JSON文件中,所有场景通过read引用该文件。

  1. 创建browser-configs.json文件:
[
  {"conf": "msedge", "exePath": "C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe"},
  {"conf": "chrome", "exePath": "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"},
  {"conf": "firefox"}
]
  1. 修改Feature文件:
Feature: Login test

Background:
  * def element = read('classpath:Test/FunctionalAutomation/Locators/LoginWebelement.json')
  * def data = read ('classpath:Test/FunctionalAutomation/TestData/DataLogin.json')

  Scenario Outline: Login with correct password
    * configure driver = {type: '<conf>', executable: '<exePath>'}
    Given driver "https://www.saucedemo.com/"
    And input(element.x_username, data.txt_username)
    And input(element.id_password, data.txt_password)
    When click(element.id_loginButton)
    Then print driver.title
    Then match driver.title == 'Swag Labs'
    Examples: read('classpath:browser-configs.json')

  Scenario Outline: Login with invalid password
    * configure driver = {type: '<conf>', executable: '<exePath>'}
    Given driver "https://www.saucedemo.com/"
    And input(element.x_username, data.txt_username)
    And input(element.id_password, data.txt_wrongPassword)
    When click(element.id_loginButton)
    Then print text(element.x_errorMessageText)
    Then match text(element.x_errorMessageText) == data.err_msg_incorrectCredential
    Examples: read('classpath:browser-configs.json')

方法3:通过系统参数动态指定浏览器,结合并行执行

如果需要灵活指定测试浏览器(比如只测单个或多个),可以通过系统参数传递浏览器类型,结合Karate并行执行实现多浏览器测试。

  1. 修改Feature文件:
Feature: Login test

Background:
  * def browser = karate.properties['browser'] ?: 'chrome'
  * def exePaths = {
    msedge: 'C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe',
    chrome: 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe'
  }
  * configure driver = {type: browser, executable: exePaths[browser]}
  * def element = read('classpath:Test/FunctionalAutomation/Locators/LoginWebelement.json')
  * def data = read ('classpath:Test/FunctionalAutomation/TestData/DataLogin.json')

  Scenario: Login with correct password
    Given driver "https://www.saucedemo.com/"
    And input(element.x_username, data.txt_username)
    And input(element.id_password, data.txt_password)
    When click(element.id_loginButton)
    Then match driver.title == 'Swag Labs'

  Scenario: Login with invalid password
    Given driver "https://www.saucedemo.com/"
    And input(element.x_username, data.txt_username)
    And input(element.id_password, data.txt_wrongPassword)
    When click(element.id_loginButton)
    Then match text(element.x_errorMessageText) == data.err_msg_incorrectCredential
  1. 执行命令(指定多个浏览器并开启并行):
mvn test -Dbrowser=chrome,msedge,firefox -Dkarate.options="--parallel 3"

内容的提问来源于stack exchange,提问作者Ash Desh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 16:30:16