我正在处理一个 Cucumber 场景,该场景使用 Selenium 来导航酒店预订网站、登录,然后继续预订房间。该代码使用两个页面工厂类:
BookRoomLoginPage_PF
- 管理登录过程。BookRoomBookingPage_PF
- 管理实际的房间预订。创建
BookRoomLoginPage_PF
实例后,用户名和密码输入功能正常。然而,登录后,当尝试使用 BookRoomBookingPage_PF
的实例预订酒店时,我发现该对象为空。包含步骤定义的类名为 BookRoom.java
。
BookRoomBookingPage_PF.java
package pagefactory;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class BookRoomLoginPage_PF {
private WebDriver driver2;
@FindBy(id="username")
private WebElement txt_username;
@FindBy(id="password")
private WebElement txt_password;
@FindBy(id="doLogin")
private WebElement loginButton;
public BookRoomLoginPage_PF(WebDriver driver1) {
this.driver2 = driver1;
PageFactory.initElements(driver1, this);
}
public void gotWebSite() {
driver2.navigate().to("https://automationintesting.online/#/admin");
}
public void enterUserNameAndPassword(String username, String password) {
txt_username.sendKeys(username);
txt_password.sendKeys(password);
loginButton.click();
}
}
BookRoomBookingPage_PF.java
package pagefactory;
import org.openqa.selenium.*;
import org.openqa.selenium.support.*;
import org.openqa.selenium.support.ui.*;
public class BookRoomBookingPage_PF {
private WebDriver driver;
@FindBy(xpath="//a[@id='frontPageLink']")
private WebElement frontPageLink;
@FindBy(id="roomName")
WebElement txt_roomNumber;
@FindBy(id="type")
WebElement roomTypeDropDown;
@FindBy(id="accessible")
WebElement accessibleTrueFalse;
@FindBy(id="roomPrice")
WebElement roomPriceText;
@FindBy(id="wifiCheckbox")
WebElement wifiCheckBox;
@FindBy(id="safeCheckbox")
WebElement safeCheckbox;
@FindBy(id="createRoom")
WebElement createRoomButton;
public BookRoomBookingPage_PF(WebDriver driver1) {
this.driver = driver1;
PageFactory.initElements(driver1, this);
}
public BookRoomBookingPage_PF() {}
public void validateUserReachedBooking() {
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement Category_Body = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("frontPageLink")));
String frontPageSTring = frontPageLink.getText();
String expectedFrontPageText= "Front Page";
Assert.assertEquals(frontPageSTring, expectedFrontPageText);
}
public void gotWebSite() {
driver.navigate().to("https://automationintesting.online/#/admin");
}
public void enterRoomNumber(String numberForRoom) {
txt_roomNumber.sendKeys(numberForRoom);
}
public void selectRoomType() {
Select selectRoomType = new Select(roomTypeDropDown);
selectRoomType.selectByVisibleText("Family");
}
public void setRoomAccessibility() {
Select selectRoomAccessibility = new Select(accessibleTrueFalse);
selectRoomAccessibility.selectByVisibleText("True");
}
public void enterRoomPrice(String numberForPrice) {
txt_roomNumber.sendKeys(numberForPrice);
}
public void checkWifiBox() {
wifiCheckBox.click();
}
public void checkSafeBox() {
safeCheckbox.click();
}
public void createRoom() {
createRoomButton.click();
}
}
BookRoom.java
package StepDefinitions;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.*;
import org.openqa.selenium.support.ui.*;
import io.cucumber.java.en.*;
import pagefactory.*;
public class BookRoom {
public WebDriver driver;
public WebDriver driver1 = null;
private BookRoomLoginPage_PF bookRoomLoginPage_PF;
BookRoomBookingPage_PF bookRoomBookingPage_PF;
private ChromeOptions options = new ChromeOptions();
@Given("I am on the booking page")
public void i_am_on_the_booking_page() {
options = new ChromeOptions();
options.addArguments("--remote-allow-origins=*");
System.setProperty("webdriver.chrome.driver", "C:/path-to-driver/chromedriver.exe");
driver = new ChromeDriver(options);
bookRoomLoginPage_PF= new BookRoomLoginPage_PF(driver);
bookRoomLoginPage_PF.gotWebSite();
bookRoomLoginPage_PF.enterUserNameAndPassword("admin", "password");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
}
@When("I select the setting")
public void i_select_the_setting() {
driver.findElement(By.id("wifiCheckbox")).click();
}
@And("I select the room type")
public void i_select_the_room_type() {
bookRoomBookingPage_PF.selectRoomType();
}
// Rest of the step definitions...
}
BookRoom.feature
Feature: Verify you can book a room from the non admin website
I want to use this template for my feature file
Scenario Outline: Verify you can book a room
Given I am on the booking page
When I select the setting
And I select the room type
And I set Accessible to true
And I enter <roomnumber> and <price>
And I hit create
Then booking is listed
Examples:
| roomnumber | price |
| 108 | 326 |
任何人都可以帮助确定为什么
BookRoomBookingPage_PF
实例保持为空并且没有按预期执行吗?
我认为线路故障是
driver.findElement(By.id("wifiCheckbox")).click();
在您登录和新页面准备就绪之间,可能需要一些时间(加载 css、在后台运行任何 js 等)。请记住,selenium 可能比真实浏览器上的真实场景慢。所以我会添加一些检查来查看该元素是否可用,然后可能等待 1-2 秒,再次检查并继续或失败。
每次更改页面时,这是一个很好的做法,可以减少不稳定的情况。 另外,我想说,考虑添加这些检查作为步骤,以明确您需要它们并推动其他开发人员遵循这种方法。