java.lang.NullPointerException当尝试点击时

huangapple go评论95阅读模式
英文:

java.lang.NullPointerException when try click

问题

我在尝试使用方法getClickOnHotelTab()时出现了java.lang.NullPointerException异常。

方法getClickOnHotelTab()位于页面SearchHotelPage上。当我在SearchHotelStep中尝试使用此方法时,我得到了java.lang.NullPointerException异常。

这是我的SearchHotelPage:

public class SearchHotelPage {

    public WebDriver driver;

    private By clickOnHotelTab = By.xpath("//a[@title='Hotéis']");
    private By destinyClick1 = By.xpath("(//div[@role='button']/span)[3]");
    private By destinyClick2 = By.xpath("//input[@placeholder='Destino ou nome do hotel']");

    public SearchHotelPage(WebDriver driver) {
        this.driver = driver;
    }

    public WebElement getDestinyClick1() {
        return driver.findElement(destinyClick1);
    }

    public WebElement getDestinyClick2() {
        return driver.findElement(destinyClick2);
    }

    public WebElement getClickOnHotelTab() {
        return driver.findElement(clickOnHotelTab);
    }
}

这是我的SearchHotelStep:

@RunWith(Cucumber.class)
public class SearchHotelStep extends BasePage {

    public WebDriver driver;

    @When("^Choose destiny or hotel name$")
    public void choose_destiny_or_hotel_name() throws Throwable {

        SearchHotelPage destinyClick = new SearchHotelPage(driver);
        destinyClick.getClickOnHotelTab().click();
        // destinyClick.getDestinyClick1();
        // destinyClick.getDestinyClick2().sendKeys("cancun");
        // destinyClick.getDestinyClick2().click();
    }
}

这是输出信息:

Scenario: Search Hotel With Travel Insurance
...
java.lang.NullPointerException
at pages.SearchHotelPage.getClickOnHotelTab(SearchHotelPage.java:28)
at steps.SearchHotelStep.choose_destiny_or_hotel_name(SearchHotelStep.java:22)
at ?.And Choose destiny or hotel name(SearchHotelWithTravelInsurance.feature:6)
...

请问你能帮我吗?

英文:

I get java.lang.NullPointerException when I try use method getClickOnHotelTab()

The method getClickOnHotelPage is on the page SearchHotelPage. When I try use this method on SearchHotelStep I got java.lang.NullPointerException.

This is my SearchHotelPage:

public class SearchHotelPage {

public WebDriver driver;

private By clickOnHotelTab = By.xpath("//*[@id='PageNavBarSectionList']//a[@title='Hotéis']");
private By destinyClick1 = By.xpath("(//div[@role='button']/span)[3]");
private By destinyClick2 = By.xpath("//input[@placeholder='Destino ou nome do hotel']");

public SearchHotelPage(WebDriver driver) {
	this.driver = driver;
}

public WebElement getDestinyClick1() {
	return driver.findElement(destinyClick1);
}

public WebElement getDestinyClick2() {
	return driver.findElement(destinyClick2);
}

public WebElement getClickOnHotelTab() {
	return driver.findElement(clickOnHotelTab);
}

This is my SearchHotelStep:

@RunWith(Cucumber.class)
public class SearchHotelStep extends BasePage {

public WebDriver driver;

@When("^Choose destiny or hotel name$")
public void choose_destiny_or_hotel_name() throws Throwable {
    
	SearchHotelPage destinyClick = new SearchHotelPage(driver);
	destinyClick.getClickOnHotelTab().click();
	//destinyClick.getDestinyClick1();
	//destinyClick.getDestinyClick2().sendKeys("cancun");
	//destinyClick.getDestinyClick2().click();
}

This is the output:

Scenario: Search Hotel With Travel Insurance             # SearchHotelWithTravelInsurance.feature:3
Given The browser is initialized                       # 
SearchAirTicketsStep.the_browser_is_initialized()
When User go to "https://www.turismocity.com.br/" site # SearchAirTicketsStep.user_go_to_site(String)
And Choose destiny or hotel name                       # 
SearchHotelStep.choose_destiny_or_hotel_name()
java.lang.NullPointerException
at pages.SearchHotelPage.getClickOnHotelTab(SearchHotelPage.java:28)
at steps.SearchHotelStep.choose_destiny_or_hotel_name(SearchHotelStep.java:22)
at ?.And Choose destiny or hotel name(SearchHotelWithTravelInsurance.feature:6)

And Choose round trip date                             # 
SearchAirTicketsStep.choose_round_trip_date()
And Choose quantity of guests                          # SearchHotelStep.choose_quantity_of_guests()
When Click on search button from hotels search         # 
SearchHotelStep.click_on_search_button_from_hotels_search()
Then The options of hotel are shown                    # 
SearchHotelStep.the_options_of_hotel_are_shown()
Then The page of travel insurance is open              # 
SearchHotelStep.the_page_of_travel_insurance_is_open()
Then The browser is closed                             # SearchAirTicketsStep.the_browser_is_closed()

Failed scenarios:
SearchHotelWithTravelInsurance.feature:3 # Scenario: Search Hotel With Travel Insurance

1 Scenarios (1 failed)
9 Steps (1 failed, 6 skipped, 2 passed)
0m9,057s

java.lang.NullPointerException
at pages.SearchHotelPage.getClickOnHotelTab(SearchHotelPage.java:28)
at steps.SearchHotelStep.choose_destiny_or_hotel_name(SearchHotelStep.java:22)
at ?.And Choose destiny or hotel name(SearchHotelWithTravelInsurance.feature:6)

Can you help me, please ?

答案1

得分: 1

我不确定Selenium的工作原理,但看起来你的SearchHotelStep类中从未分配或创建`WebDriver driver`变量,因此它将始终引发空指针错误。

你需要在使用之前创建或分配driver变量。

例如:`driver = yourCreatedDriver;` 或者 `public WebDriver driver = new WebDriver(...);`

类似这样:

```java
@RunWith(Cucumber.class)
public class SearchHotelStep extends BasePage {

public WebDriver driver = new WebDriver(...);

@When("^Choose destiny or hotel name$")
public void choose_destiny_or_hotel_name() throws Throwable {

    SearchHotelPage destinyClick = new SearchHotelPage(driver);
    destinyClick.getClickOnHotelTab().click();
}

<details>
<summary>英文:</summary>

I am unsure how selenium works, but it looks like the `WebDriver driver` variable is never assigned or created in your SearchHotelStep class, so it will always create a null pointer error.

You need to create or assign the driver before you use it.

For example `driver = yourCreatedDriver;` or `public WebDriver driver = new WebDriver(...);`

Something like this:

    @RunWith(Cucumber.class)
    public class SearchHotelStep extends BasePage {
    
    public WebDriver driver = new WebDriver(...);
    
    @When(&quot;^Choose destiny or hotel name$&quot;)
    public void choose_destiny_or_hotel_name() throws Throwable {
        
        SearchHotelPage destinyClick = new SearchHotelPage(driver);
        destinyClick.getClickOnHotelTab().click();
    }

</details>



huangapple
  • 本文由 发表于 2020年10月27日 04:31:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/64544564.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定