Karate中Background调用Feature后场景无法识别浏览器的问题
解决Karate UI测试中
click未定义的问题 问题描述
使用Karate进行UI测试时,在Background中调用登录Feature完成Web应用登录后,执行场景中的Web交互操作时出现错误:
[ERROR] ExamplesTest.testParallel:61 js failed: >>>>>> 01: click(homeMenuItem) <<<<<< org.graalvm.polyglot.PolyglotException: ReferenceError: "click" is not defined - <js>.:program(Unnamed:1) classpath:examples/xxx/home_page.feature:18 ==> expected: <true> but was: <false>
Chrome浏览器已正常打开并完成登录,但场景执行首个命令时无法识别浏览器上下文,导致无法进行后续交互。
相关代码片段
home_page.feature
Feature: Manage home page Background: * configure driver = { type: 'chrome' } * karate.call('login.feature', { name: 'xxxxx', passwd: '*******' }) * configure afterScenario = """ function(){ karate.call('after-scenario.feature', { caller: karate.feature.fileName }); } """ Scenario: Display/hide home page Given click(homeMenuItem)
login.feature
@ignore Feature: Login to application Scenario: login Given driver urlWebUi And retry(5, 2000).waitForUrl(urlBaseConnect) And input("//input[@id='username']", name) And input("//input[@id='password']", passwd) When click("//input[@id='kc-login']")
Runner类
public class ExamplesTest { @Test void testParallel() { Results results = Runner.path("classpath:examples") .outputCucumberJson(true) .karateEnv("dev") .parallel(1); generateReport(results.getReportDir()); assertTrue(results.getFailCount() == 0, results.getErrorMessages()); } }
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany</groupId> <artifactId>myproject</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <maven.compiler.version>3.6.0</maven.compiler.version> <karate.version>1.5.1</karate.version> </properties> <dependencies> <dependency> <groupId>io.karatelabs</groupId> <artifactId>karate-junit5</artifactId> <version>${karate.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>net.masterthought</groupId> <artifactId>cucumber-reporting</artifactId> <version>5.8.4</version> <scope>test</scope> </dependency> </dependencies> <build> <testResources> <testResource> <directory>src/test/java</directory> <excludes> <exclude>**/*.java</exclude> </excludes> </testResource> </testResources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven.compiler.version}</version> <configuration> <encoding>UTF-8</encoding> <source>${java.version}</source> <target>${java.version}</target> <compilerArgument>-Werror</compilerArgument> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> </plugin> </plugins> </build> </project>
解决方案
1. 补充Karate UI核心依赖
当前pom.xml仅引入了karate-junit5,但Karate UI测试需要额外的核心依赖。在<dependencies>中添加以下内容:
<dependency> <groupId>io.karatelabs</groupId> <artifactId>karate-core</artifactId> <version>${karate.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>io.karatelabs</groupId> <artifactId>karate-chrome</artifactId> <version>${karate.version}</version> <scope>test</scope> </dependency>
Karate 1.5.x版本中,click、input等UI API需要这些依赖才能被正确识别和加载。
2. 确保驱动上下文复用
在home_page.feature的Background中,为驱动配置添加reuse: true,保证驱动实例在多个Feature调用之间保持上下文:
Background: * configure driver = { type: 'chrome', reuse: true } * karate.call('login.feature', { name: 'xxxxx', passwd: '*******' }) # 显式确认驱动上下文有效 * driver.currentUrl
3. 修正场景步骤语法
- 确保
homeMenuItem是已定义的定位器变量,或直接使用定位表达式:Scenario: Display/hide home page * def homeMenuItem = "//your/actual/locator/for/homeMenuItem" Given click(homeMenuItem) - 避免步骤被误解析为JS代码,确保
click操作关联Karate的UI驱动上下文。
4. 检查版本兼容性
- 尝试升级Karate到1.5.2版本,修复1.5.1中的已知UI上下文问题
- 确认Chrome浏览器版本与Karate自动下载的ChromeDriver版本匹配(可通过查看测试日志中的ChromeDriver版本信息验证)
内容的提问来源于stack exchange,提问作者gerfred
相关产品推荐
相关产品推荐

