使用 Maven 项目和 TestNG 框架编写脚本

问题描述 投票:0回答:0

嗨,我想完成这个任务,但我创建了 scrip ans 仍然有错误,它没有正常运行

转到 https://expedia.com 并使用带有 TestNG 框架的 Maven 项目创建脚本以执行以下步骤:

我。单击航班选项卡

二。填写表格搜索往返航班,然后点击搜索按钮

三。选择往返航班

v。切换到其他选项卡 v. 点击结帐

六。填写乘客信息



Home Class:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class HomePage {
    private WebDriver driver;

    @FindBy(id = "tab-flight-tab")
    private WebElement flightsTab;

    @FindBy(id = "flight-type-roundtrip-label-hp-flight")
    private WebElement roundTripOption;

    @FindBy(id = "flight-origin-hp-flight")
    private WebElement originInput;

    @FindBy(id = "flight-destination-hp-flight")
    private WebElement destinationInput;

    @FindBy(id = "flight-departing-hp-flight")
    private WebElement departureDateInput;

    @FindBy(id = "flight-returning-hp-flight")
    private WebElement returnDateInput;

    @FindBy(xpath = "//button[@class='btn-primary btn-action gcw-submit']")
    private WebElement searchButton;

    public HomePage(WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }

    public void clickFlightsTab() {
        flightsTab.click();
    }

    public void selectRoundTripOption() {
        roundTripOption.click();
    }

    public void enterOrigin(String origin) {
        originInput.sendKeys(origin);
    }

    public void enterDestination(String destination) {
        destinationInput.sendKeys(destination);
    }

    public void enterDepartureDate(String departureDate) {
        departureDateInput.sendKeys(departureDate);
    }

    public void enterReturnDate(String returnDate) {
        returnDateInput.sendKeys(returnDate);
    }

    public void clickSearchButton() {
        searchButton.click();
    }
}


Base Class:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;

public class BaseTest {
    protected WebDriver driver;
    protected HomePage homePage;

    @BeforeSuite
    public void setup() {
        // Set the path to the chromedriver executable
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

        // Create a new instance of ChromeDriver
        driver = new ChromeDriver();
        driver.manage().window().maximize();

        // Initialize the home page
        homePage = new HomePage(driver);
    }

    @AfterSuite
    public void teardown() {
        // Close the browser
        driver.quit();
    }
}


Configuration XML file (testng.xml):
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="ExpediaTestSuite">
    <test name="ExpediaFlightBookingTest">
        <classes>
            <class name="com.example.ExpediaFlightBookingTest" />
        </classes>
    </test>
</suite>

Updated ExpediaFlightBookingTest class:
import org.openqa.selenium.By;
import org.testng.annotations.Test;

public class ExpediaFlightBookingTest extends BaseTest {

    @Test
    public void bookFlight() {
        // Launch the browser and navigate to Expedia
        driver.get("https://www.expedia.com");

        // Click on the "Flights" tab
        homePage.clickFlightsTab();

        // Fill the flight search form for round trip and click on the search button
        homePage.selectRoundTripOption();
        homePage.enterOrigin("New York");
        homePage.enterDestination("Los Angeles");
        homePage.enterDepartureDate("05/20/2023");
        homePage.enterReturnDate("05/27/2023");
        homePage.clickSearchButton();

        // Select round trip flights
        driver.findElement(By.id("filter-ROUND_TRIP")).click();

        // Switch to the other tab
        String mainWindowHandle = driver.getWindowHandle();
        for (String handle : driver.getWindowHandles()) {
            if (!handle.equals(mainWindowHandle)) {
                driver.switchTo().window(handle);
                break;
            }
        }

        // Click on the checkout button
        driver.findElement(By.id("checkout-button")).click();

        // Fill the passenger's information
        driver.findElement(By.id("passenger-first-name")).sendKeys("John");
        driver.findElement(By.id("passenger-last-name")).sendKeys("Doe");

        // Add more code to fill in other passenger information if needed
    }
}

我也是这样做的。 需要更换``` "path/to/chromedriver" 与系统上 chromedriver 可执行文件的实际路径。此外,确保您在 Maven 项目中为 TestNG 设置了必要的依赖项和配置


but this error showing  

失败:bookFlight java.lang.NullPointerException:无法调用“org.openqa.selenium.WebDriver.get(String)”因为“this.driver”为空

Its not running


if you know how to solve ,please say me thank you 

    

maven frameworks testng project
© www.soinside.com 2019 - 2024. All rights reserved.