gerfred
gerfred

Reputation: 23

How to access browser in scenario when first called from a feature in background with karate

using Karate for ui testing, when I call a feature in background in order to log in the webapp, if I try to interact with the webapp in a scenario, an error is provided:

[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>

The chrome browser is opened, then the login proceed correctly, but when the first command of the scenario is tried it seems that the chrome browser is not "recognized". It is still open (on the expected state after login) but no interaction with the scenario.

Is there a way to do it ?



home_page.feature file to run:

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 called in background:

@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>

Upvotes: 1

Views: 32

Answers (0)

Related Questions