黄瓜没有运行硒代码

时间:2015-05-20 10:05:40

标签: selenium junit cucumber-jvm cucumber-junit cucumber-cpp

当我尝试运行我的代码时,它只显示黄瓜骨架。我使用JUnit运行器类作为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"

Selenium Class:

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 个答案:

答案 0 :(得分:0)

我找到了它没有执行的原因,因为我在Package cucumberTest中添加了Selenium测试类和Junit runner类;和骨架在包装步骤中;所以我做的是我把骨架移到了pacakge cucumberTest;其中junit runner和selenium类解决了问题。问题出现了,因为当你在包中放置Junit类时,它将在该包中搜索骨架但是如果你在源文件夹中添加JUnit runner类,那么它将能够在src文件夹下找到该页面