java - 当我将鼠标悬停在步骤上时,步骤 'I click on join now' 没有匹配的粘合代码

标签 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

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

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

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();
        }
    }

一切都很好,但是当我将鼠标悬停在功能文件中的步骤上时,我收到此错误(Given 正在工作,但是然后不起作用)步骤“我现在点击加入”没有匹配的粘合代码@feature 功能:为了注册用户需要创建帐户

@scenario 场景:为用户创建帐户假设我登陆耐克主页并且我点击立即加入当我向用户提供要加入的用户详细信息时然后我需要查看用户详细信息

最佳答案

如果您的 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")

选择它

关于java - 当我将鼠标悬停在步骤上时,步骤 'I click on join now' 没有匹配的粘合代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44320968/

相关文章:

javascript - 使用单选按钮更改记录。

java - 无法使用 selenium webdriver 3.0.1 单击注销链接

Java - 如何使用 JavaScriptExecutor 调用字符串内的变量?

css - Selenium 记录和回放一个 css 元素

ruby - 即使被排除, cucumber 标签也会运行(我认为)

java - 用于测试分布式系统的集成测试框架?

java - Java 中的事件是如何产生的?

ruby-on-rails - Cucumber 失败的场景未提供详细信息(Ubuntu)

cucumber - Wdio.conf : How to use result object in oncomplete hook

java - 使用 MongoDb 创建索引