Step'我点击立即加入'没有匹配的胶水代码,当我鼠标悬停在步骤上时

时间:2017-06-02 04:46:13

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

以下是在运行场景后我得到的错误: -

@scenario
  Scenario: creating a account to user               # /Users/ajaysithagari/Documents/workspace/Selenium_Cucumber/features/signup.feature:6
    Given I am landing on nike homepage              # CommonStepDefination.i_am_landing_on_nike_homepage()
    And I click on join now                          # SignupStepDefination.i_click_on_join_now()
      java.lang.NullPointerException
        at pages.Signup.join(Signup.java:12)
        at step_defination.SignupStepDefination.i_click_on_join_now(SignupStepDefination.java:13)
        at ✽.And I click on join now(/Users/ajaysithagari/Documents/workspace/Selenium_Cucumber/features/signup.feature:8)

    When I provide the user the user details to join # SignupStepDefination.i_provide_the_user_the_user_details_to_join()
    Then I need to see the user details              # SignupStepDefination.i_need_to_see_the_user_details()

1 Scenarios (1 failed)
4 Steps (1 failed, 2 skipped, 1 passed)
0m8.658s

以下是我的情景: - @特征 功能:为了注册          用户需要创建帐户

@scenario   场景:为用户创建帐户     鉴于我登陆耐克主页     我现在点击加入     当我向用户提供要加入的用户详细信息时     然后我需要查看用户详细信息

Here is step defination for "given":-
package step_defination;
;
import cucumber.api.java.en.Given;
import utils.BrowserandDriver;

public class CommonStepDefination {

        String PageURL = "xxxxx";
        int ImplicitWait = 15;
        int pageLoadTimeOut = 30;
        String browserName = "safari";

        BrowserandDriver BD = new BrowserandDriver();

        @Before
        public void launchBrowser()
        {
            BD.launchBrowser(browserName);
            BD.maximizeBrowser();
            BD.setImplicitWait(ImplicitWait);
            BD.setPageLoadTimeout(pageLoadTimeOut);
        }   

        @Given("^I am landing on nike homepage$")
        public void i_am_landing_on_nike_homepage() throws Throwable {
        BD.launchApp(PageURL);
        }

        @After
        public void tearDown(Scenario scenario) {
        BD.tearDown(scenario);
       }
}


    Here is my step defination:- 

    package step_defination;

    import cucumber.api.java.en.And;
    import cucumber.api.java.en.Then;
    import cucumber.api.java.en.When;
    import pages.Signup;

    public class SignupStepDefination {

        @And("^I click on join now$")
        public void i_click_on_join_now() throws Throwable {
            Signup sign = new Signup();
            sign.join();   
        }
    @When("^I provide the user the user details to join$")
        public void i_provide_the_user_the_user_details_to_join() throws Throwable {
        }
        @Then("^I need to see the user details$")
        public void i_need_to_see_the_user_details() throws Throwable {
        }

    Here is my page:-

    package pages;

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

    public class Signup {

        public static WebDriver driver;

        public void join()
        {
        driver.findElement(By.xpath("/html/body/div[7]/nav/div[1]/ul[2]/li[2]/button")).click();
        }
    }

每件事情都很好但是当我将鼠标悬停在功能文件中的步骤上时,我收到此错误(鉴于工作但是,当时不起作用)Step'我点击立即加入'没有匹配的胶水代码@feature功能:为了注册用户需要创建帐户

@scenario场景:为用户创建帐户鉴于我登陆nike主页我现在点击加入当我向用户提供加入的用户详细信息然后我需要查看用户详细信息

1 个答案:

答案 0 :(得分:0)

如果您的BrowserandDriver类看起来像这样:

package utils;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.safari.SafariDriver;

public class BrowserandDriver {
    public static WebDriver driver;

    public static void launchBrowser(String browserName) {
        if (browserName.toLowerCase().contains("safari")) {
            driver = new SafariDriver();
        }
    }

    public static void setPageLoadTimeout(int waitTime) {
        driver.manage().timeouts().pageLoadTimeout(waitTime, TimeUnit.SECONDS);
    }

    public static void launchApp(String appURL) {
        driver.get(appURL);
    }
}

(我把你的所有方法都设置为静态,因为它们只在静态字段driver上运行),然后你可以像这样编写你的步骤类(CommonStepDefination.java):

package step_defination;

import cucumber.api.java.en.Given;
import utils.BrowserandDriver;

public class CommonStepDefination {
    String PageURL = "xxxxx";
    int ImplicitWait = 15;
    int pageLoadTimeOut = 30;
    String browserName = "safari";

    // no need for a BrowserandDriver instance here, since everything in BrowserandDriver is static

    @Before
    public void launchBrowser()
    {
        BrowserandDriver.launchBrowser(browserName);
        BrowserandDriver.maximizeBrowser();
        BrowserandDriver.setImplicitWait(ImplicitWait);
        BrowserandDriver.setPageLoadTimeout(pageLoadTimeOut);
    }
    // in other methods the references to the field BD must also be replaced with the class name BrowserandDriver
}

和Signup.java:

package pages;

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

public class Signup {

    public void join()
    {
        BrowserandDriver.driver.findElement(By.xpath("/html/body/div[7]/nav/div[1]/ul[2]/li[2]/button")).click();
    }
}

在旁注中,我不会选择带有By.xpath("/html/body/div[7]/nav/div[1]/ul[2]/li[2]/button")的按钮 - 这几乎会破坏您在html页面上所做的每一项更改。最好为按钮添加唯一ID(例如joinButton)并选择By.id("joinButton")