简体   繁体   English

黄瓜和硒错误

[英]Cucumber and Selenium Error on Feature

I created a automated test scenario using selenium + cucumber on java, and when I try to execute my test nothing occur. 我在Java上使用硒+黄瓜创建了一个自动化测试方案,当我尝试执行测试时,什么也没有发生。 I don't know what I did worng but I think that something happend with my feature, because exist warning on the following messages "No definition found for I try to login on facebook ", "No definition found for I put my user "email" ", "No definition found for I put my password "pass" " and "No definition found for validate login ". 我不知道我做了什么,但是我认为我的功能发生了某些问题,因为以下消息存在警告:“找不到定义, I try to login on facebook ”,“找不到定义, I put my user "email" ”,“找不到定义,因为I put my password "pass" ”和“找不到定义用于validate login ”。

I imported these jars using maven: cucumber-java : 1.2.5/cucumber-junit : 1.2.5/selenium-java : 3.0.1/selenium-firefox-driver : 3.0.1/junit : 4.12 我使用maven导入了这些罐子:黄瓜java:1.2.5 / cucumber-junit:1.2.5 / selenium-java:3.0.1 / selenium-firefox-driver:3.0.1 / junit:4.12

RunTest.java 运行测试

package com.tdd.facebook;   

import org.junit.runner.RunWith; 

import cucumber.api.junit.Cucumber;
import cucumber.api.CucumberOptions;

@RunWith(Cucumber.class) 
@CucumberOptions(
        format = {"pretty", "html:target/cucumber"},
        features = {"src/test/resources"}
        ) 

public class RunTest {

    public RunTest() {
        // TODO Auto-generated constructor stub
    }

}

LoginSteps.java LoginSteps.java

package com.tdd.facebook;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import cucumber.api.PendingException;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class LoginSteps {

    WebDriver driver = null;

    @Given("^I try to login on facebook$")
    public void i_try_to_login_on_facebook() throws Throwable {

        System.setProperty("webdriver.gecko.driver", "C:\\Caio\\Selenium\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        driver.get("https://www.facebook.com/");        
        throw new PendingException();
    }

    @When("^I put my user \"([^\"]*)\"$")
    public void i_put_my_user(String email) {
        driver.findElement(By.id(email)).click();
        driver.findElement(By.id(email)).sendKeys("UserTest");
    }

    @When("^I put my password \"([^\"]*)\"$")
    public void i_put_my_password(String pass){ 
        driver.findElement(By.id(pass)).click();
        driver.findElement(By.id(pass)).sendKeys("Test");
    }

    @Then("^validate login$")
    public void validate_login() throws Throwable {
        System.out.println("Login OK");
    }

    public LoginSteps() {
        // TODO Auto-generated constructor stub
    }

}

Login.feature 登录功能

Feature: Login on facebook

Scenario: Check if the login is successful
Given I try to login on facebook
When I put my user "email"
When I put my password "pass"
Then validate login

pow.xml pow.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.tdd</groupId>
    <artifactId>facebook</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>facebook</name>
    <url>http://maven.apache.org</url>

    <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>1.2.5</version>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>1.2.5</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-firefox-driver</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>
</project>

You need to add glue option to cucumberoptions with the path to the step definition classes. 您需要使用步骤定义类的路径将胶水选项添加到黄瓜选项。 Use glue="com.tdd.facebook" . 使用glue="com.tdd.facebook" I am not sure about this but you might want to remove the constructor in the runner RunTest . 我对此不太确定,但是您可能要在运行程序RunTest删除构造RunTest

In the LoginStps class - i_try_to_login_on_facebook() method, you are declaring another driver variable and initializing it. LoginStpsi_try_to_login_on_facebook()方法中,您将声明另一个驱动程序变量并对其进行初始化。 Thus the driver field of the class will remain null. 因此,该类的驱动程序字段将保持为空。 Remove this. 删除它。 Also in the same method you are throwing a k. 同样,您使用相同的方法抛出k。 As you have implemented the step you do not need it anymore. 完成该步骤后,您将不再需要它。

Also do check if all the jars are imported in the pom. 还要检查所有罐子是否都已导入pom中。 I think you are missing some jars. 我认为您缺少一些罐子。 Check the cucumber java page. 检查黄瓜Java页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM