错误:java.lang.空指针异常:无法调用“org.openqa.selenium.WebDriver.findElement(org.openqa.selenium.By)”,因为“此.驱动程序”为空

bf1o4zei  于 2023-03-06  发布在  Java
关注(0)|答案(1)|浏览(226)

下面是logout.feature文件,大多数步骤都已在checkout.feature中定义,但CheckOutStepDefinition类中已实现了(以下文件中的粗体行除外

Feature: Swag Labs Logout feature

Scenario: Logout from website
Given user is on Login Page
When title of page is "Swag Labs"
Then user enter username and password
| username     |   password  |
|standard_user | secret_sauce|

Then user click on login button
Then user lands on home page
Then user click on the ADD TO CART button for first product
Then user click on cart button to check product
Then user lands on YOUR CART page And "Your Cart" title appears
Then user verify the number of products in cart
Then user click on CHECKOUT button
Then fill out personal information firstname lastname postalcode
|firstname | lastname | postalcode|
|john      | smith    | R4RT4T    |

Then user click on CONTINUE button
Then user click on FINISH button
Then user lands on CHECKOUT: COMPLETE! PAGE And "Thank you for your order!" msg appears
**Then user clicks on menu
Then user clicks on logout option
Then user lands on "Swag Labs" login page**
Then user close browser

注销步骤定义类

package stepDefinitions;

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

import io.cucumber.java.en.Then;

public class logoutStepDefinition {

    public WebDriver driver;
    
    @Then("user clicks on menu")
    public void user_clicks_on_menu() {
        driver.findElement(By.id("react-burger-menu-btn")).click();
    }

    @Then("user clicks on logout option")
    public void user_clicks_on_logout_option() {
       driver.findElement(By.id("logout_sidebar_link")).click();
    }

    @Then("user lands on {string} login page")
    public void user_lands_on_login_page(String title) {
        String expected_title = title;
        String actual_title = driver.getTitle();
        Assert.assertEquals(actual_title, expected_title);
    }
    
}

checkout.feature

Feature: Swag Labs checkout
 
Scenario Outline: checkout feature for products

Given user is on Login Page
When title of page is "Swag Labs"
Then user enter username and password
| username     |   password  |
|standard_user | secret_sauce|

Then user click on login button
Then user lands on home page
Then user click on the ADD TO CART button for first product
Then user click on cart button to check product
Then user lands on YOUR CART page And "Your Cart" title appears
Then user verify the number of products in cart
Then user click on CHECKOUT button
Then fill out personal information firstname lastname postalcode
|firstname | lastname | postalcode|
|john      | smith    | R4RT4T    |

Then user click on CONTINUE button
Then user click on FINISH button
Then user lands on CHECKOUT: COMPLETE! PAGE And "Thank you for your order!" msg appears
Then user close browser

检验步骤定义类

package stepDefinitions;

import java.util.List;
import java.util.Map;

import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import io.cucumber.datatable.DataTable;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;

public class CheckOutStepDefinition {

    WebDriver driver;

    @Given("user is on Login Page")
    public void user_is_on_login_page() {
        System.setProperty("webdriver.chrome.driver",
                "C:\\Users\\omson\\eclipse-workspace\\Saucedemo_BDD_Automation\\Drivers\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("https://www.saucedemo.com/");
    }

    @When("title of page is {string}")
    public void title_of_page_is(String title) {
        String expected_title = title;
        String actual_title = driver.getTitle();
        Assert.assertEquals(actual_title, expected_title);
    }

    @Then("user enter username and password")
    public void user_enter_and(DataTable login) {
        List<Map<String, String>> data = login.asMaps(String.class, String.class);
        driver.findElement(By.id("user-name")).sendKeys(data.get(0).get("username"));
        driver.findElement(By.id("password")).sendKeys(data.get(0).get("password"));
    }

    @Then("user click on login button")
    public void user_click_on_login_button() {
        driver.findElement(By.id("login-button")).click();
    }

    @Then("user lands on home page")
    public void user_lands_on_home_page() {
        String expected = "Products";
        String products = driver.findElement(By.className("title")).getText();
        Assert.assertEquals(products, expected);
    }

    @Then("user click on the ADD TO CART button for first product")
    public void user_click_on_the_add_to_cart_button_for_first_product() {
        driver.findElement(By.xpath("//div[@class='inventory_item'][1]//button[contains(text(),'Add to cart')]"))
                .click();
    }

    @Then("user click on cart button to check product")
    public void user_click_on_cart_button_to_check_product() {
        driver.findElement(By.className("shopping_cart_link")).click();
    }

    @Then("user lands on YOUR CART page And {string} title appears")
    public void user_lands_on_your_cart_page_and_title_appears(String cartTitle) {
        String expected_title = cartTitle;
        String actual_title = driver.findElement(By.className("title")).getText();
        Assert.assertEquals(actual_title, expected_title);
    }

    @Then("user verify the number of products in cart")
    public void user_verify_the_number_of_products_in_cart() {
        List<WebElement> items = driver.findElements(By.className("cart_item"));
        System.out.println(items.size());
        Assert.assertTrue(items.size() == 1);
    }

    @Then("user click on CHECKOUT button")
    public void user_click_on_checkout_button() {
        driver.findElement(By.id("checkout")).click();
    }

    @Then("fill out personal information firstname lastname postalcode")
    public void fill_out_personal_information_fname_lname_postal(DataTable personal) {
        List<Map<String, String>> personal_info = personal.asMaps(String.class, String.class);
        driver.findElement(By.id("first-name")).sendKeys(personal_info.get(0).get("firstname"));
        driver.findElement(By.id("last-name")).sendKeys(personal_info.get(0).get("lastname"));
        driver.findElement(By.id("postal-code")).sendKeys(personal_info.get(0).get("postalcode"));
    }

    @Then("user click on CONTINUE button")
    public void user_click_on_continue_button() {
        driver.findElement(By.id("continue")).click();
    }

    @Then("user click on FINISH button")
    public void user_click_on_finish_button() {
        driver.findElement(By.id("finish")).click();
    }

    @Then("user lands on CHECKOUT: COMPLETE! PAGE And {string} msg appears")
    public void user_lands_on_checkout_complete_page_and_msg_appears(String thankYou) {
        String expected_title = thankYou;
        String actual_title = driver.findElement(By.className("complete-header")).getText();
        Assert.assertEquals(actual_title, expected_title);
    }

    @Then("user close browser")
    public void user_close_browser() {
        driver.quit();
    }
}

我已经创建了运行测试的Runner类。checkout. feature运行良好。但当我尝试运行logout. feature时,它会给出一个NullPointerException错误,说明驱动程序为空。我已经在LogOutStepDefinition中初始化了WebDriver驱动程序,但仍然得到一个错误。
有什么问题吗?

y4ekin9u

y4ekin9u1#

查看您的代码,您只在logoutStepDefinition类中声明了“driver”,但没有初始化。
在CheckOutStepDefinition类中退出驱动程序时,请在logoutStepDefinition类中再次初始化驱动程序。

相关问题