gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:26:19 24 4
gpt4 key购买 nike

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

@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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com