当前位置: 首页 > 知识库问答 >
问题:

cucumber不运行硒代码

酆出野
2023-03-14

当我尝试运行我的代码时,它只显示cucumber骨架。我使用JUnitRunner类作为JUnit测试套件。

下面是所有三个类的代码。

特点是:

Feature: Check addition in Google calculator

   In order to verify that google calculator work correctly
   As a user of google
   I should be able to get correct addition result
@Runme
   Scenario: Addition
   Given I open google
   When I enter "2+2" in search textbox
   Then I should get result as "4"

硒类:

package cucumberTest;

import java.util.concurrent.TimeUnit;

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

public class SeleniumTest {
    private SeleniumTest()
    {


    }
        private static WebDriver driver = null;
    public static void seleniumTest() {
        // Create a new instance of the Firefox driver

        driver = new FirefoxDriver();

        //Put a Implicit wait, this means that any search for elements on the page could take the time the implicit wait is set for before throwing exception

        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        //Launch the Online Store Website

        driver.get("http://www.store.demoqa.com");

        // Find the element that's ID attribute is 'account'(My Account) 

        driver.findElement(By.xpath(".//*[@id='account']/a")).click();

        // Find the element that's ID attribute is 'log' (Username)

        // Enter Username on the element found by above desc.

        driver.findElement(By.id("log")).sendKeys("testuser_1"); 

        // Find the element that's ID attribute is 'pwd' (Password)

        // Enter Password on the element found by the above desc.

        driver.findElement(By.id("pwd")).sendKeys("Test@123");

        // Now submit the form. WebDriver will find the form for us from the element 

        driver.findElement(By.id("login")).click();

        // Print a Log In message to the screen

        System.out.println("Login Successfully");

        // Find the element that's ID attribute is 'account_logout' (Log Out)

        driver.findElement (By.xpath(".//*[@id='account_logout']/a")).click();

        // Print a Log In message to the screen

        System.out.println("LogOut Successfully");

        // Close the driver

        driver.quit();

    }

}

JUnit类:

package cucumberTest;

import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
//@CucumberOptions(
    //  features = "Feature/googleCalc.feature"
        ////,glue={"stepDefinition"}
    //  )
@CucumberOptions(

features = {"Feature/googleCalc.feature"},glue={"stepDefinition"},

plugin = {"pretty"},

tags = {"@Runme"}

)

public class TestRunner {

}

步骤定义 :

package stepDefination;


import java.util.concurrent.TimeUnit;

import org.junit.Assert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;

import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import cucumberTest.SeleniumTest;

public class googleCalcStepDefinition {

    @Given("^I open google$")
    public void i_open_google() throws Throwable {
        // Write code here that turns the phrase above into concrete actions
      SeleniumTest.seleniumTest();
    }

    @When("^I enter \"(.*?)\" in search textbox$")
    public void i_enter_in_search_textbox(String arg1) throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        SeleniumTest.seleniumTest();
    }

    @Then("^I should get result as \"(.*?)\"$")
    public void i_should_get_result_as(String arg1) throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        SeleniumTest.seleniumTest();
    }

}

显示的输出是:

Feature: Check addition in Google calculator

   In order to verify that google calculator work correctly
   As a user of google
   I should be able to get correct addition result

  @Runme
  Scenario: Addition                     [90m# Feature/googleCalc.feature:7[0m
    [33mGiven [0m[33mI open google[0m
    [33mWhen [0m[33mI enter "2+2" in search textbox[0m
    [33mThen [0m[33mI should get result as "4"[0m

1 Scenarios ([33m1 undefined[0m)
3 Steps ([33m3 undefined[0m)
0m0.000s


You can implement missing steps with the snippets below:

@Given("^I open google$")
public void i_open_google() throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@When("^I enter \"(.*?)\" in search textbox$")
public void i_enter_in_search_textbox(String arg1) throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Then("^I should get result as \"(.*?)\"$")
public void i_should_get_result_as(String arg1) throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

共有1个答案

刁英朗
2023-03-14

我发现它没有执行的原因是因为我在cucumberTest包中添加了Selenium测试类和Junit runner类;骨架在包装步骤定义中;所以,我所做的是把骷髅移到包装cucumber测试;junit runner和selenium类在哪里解决了问题。之所以会出现问题,是因为当您将junit类放在包中时,它将在该包中搜索骨架,但如果您在源文件夹中添加junit runner类,则它将能够在src文件夹下找到页面

 类似资料:
  • 我正在尝试设置硒网格以实现测试的并行执行。首先,我将解释我当前的场景。 < li >我用watir webdriver在cucumber中编写了我的全功能测试套件 < li >我需要在多个环境中执行所有测试。 < li >我为selenium hub和node创建了一个设置 < li >我可以通过hub在单个节点上运行我的测试 我的目标是在多个虚拟机上同时运行我的测试。我遗漏了一部分,我需要配置我

  • 我的问题是关于cucumber特性文件的并行执行。在Selenium Java中,可以通过一个runner类并行运行多个cucumber特性文件吗? 我尝试过不同的方法,但都没有成功。

  • 我一直在使用selenium进行python中的自动浏览器模拟和web抓取,这对我来说效果很好。但是现在,我必须在代理服务器后面运行它。因此,现在selenium将打开窗口,但无法打开请求的页面,因为未在打开的浏览器上设置代理设置。当前代码如下(示例): 我现在如何更改上述代码以使用代理服务器?

  • cucumber中的多个标签不会运行这些场景。 我有两个不同的标签,在同一个功能文件下标记为两个不同的场景。我尝试使用cucumber tages命令运行标记的两个场景 cucumber--标签@计费--标签@重要 当我运行这个时,cucumber不识别场景,它提供如下输出 但是当我单独运行标签时,比如< code>cucumber - t @billing,cucumber能够识别并运行这个场景

  • 在eclipse IDE中,我使用Maven项目创建了一个基本的cucumber框架。 我已经添加了pom.xml.for dependencies下面添加的TestNG插件中所需的所有依赖项。 但是“TestNG套件”选项没有出现在首选项中,所以通过帮助->Install New Software安装TestNG。 testng.xml文件 但它的投掷低于错误

  • 镜头版本:我如何做JUnit的与cucumber功能? 我有我喜欢的功能文件。我想多次运行完全相同的功能,但要更改“环境”,使step defs中的