第二个网页的类的对象没有被调用(黄瓜)

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

我有一个黄瓜场景,应该使用硒去酒店预订网站,登录该网站然后预订酒店。我正在使用两个页面工厂类来执行此操作。一个类称为 BookRoomLoginPage_PF,它应该处理登录页面上的登录,另一个类称为 BookRoomBookingPage_PF,它处理实际预订房间。当创建 BookRoomLoginPage_PF 的实例来输入用户名和密码时,它工作正常,但是一旦我实际进入酒店预订页面,我尝试使用 BookRoomBookingPage_PF 的实例来实际处理酒店预订,该对象为空。具有实际步骤定义的类称为 BookRoom.java。

这是我的 BookRoomBookingPage_PF 的代码


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 的代码


package pagefactory;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import junit.framework.Assert;


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;
    
    //
    
    //Select selectRoomAccessibility = new Select(accessibleTrueFalse);
        
    public BookRoomBookingPage_PF(WebDriver driver1) {
            
            this.driver = driver1;
            
            
            
            
        }
    
    public BookRoomBookingPage_PF() {
        
        
        
        
        
        
    }
    
    @SuppressWarnings("deprecation")
    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



package StepDefinitions;

import java.util.List;
import java.util.concurrent.TimeUnit;

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 org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import pagefactory.BookRoomBookingPage_PF;
import pagefactory.BookRoomLoginPage_PF;
import pagefactory.HotelRoom;

public class BookRoom {
 public WebDriver driver;
    public WebDriver driver1 = null;
    private BookRoomLoginPage_PF bookRoomLoginPage_PF;
     BookRoomBookingPage_PF bookRoomBookingPage_PF;
     private     ChromeOptions options = new ChromeOptions();
    
     
    

    

    // scenario 2

    @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:/Users/Mjrlo/eclipse-workspace/CucumberJava/src/test/resources/drivers/chromedriver.exe");
         
         driver = new ChromeDriver(options);
        // originalWindow = driver.getWindowHandle();
         bookRoomLoginPage_PF= new BookRoomLoginPage_PF(driver);
         
         bookRoomLoginPage_PF.gotWebSite();
         bookRoomLoginPage_PF.enterUserNameAndPassword("admin", "password");
         
         
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        // driver.navigate().to("https://automationintesting.online/#/admin");
        

         
    }

    @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();
        

    }

    @And("I set Accessible to true")
    public void i_set_accessible_to_true() {

        bookRoomBookingPage_PF.setRoomAccessibility();
    

    }

    @And("I enter (.*) and (.*)$")
    public void i_enter_and(String roomNumber, String price) {

        bookRoomBookingPage_PF.enterRoomNumber(roomNumber);

        bookRoomBookingPage_PF.enterRoomPrice(price);
        
    
    }

    @And("I hit create")
    public void i_hit_create() {
        
    
        bookRoomBookingPage_PF.createRoom();
    }
    
    @Then("booking is listed")
    public void booking_is_listed() {
        //checks if booking is in the list 
        
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    //  WebElement roomTable = driver.findElement(By.className("row.detail"));
    //  Select selectRoomNumber = new Select(roomTable);
        
        List<WebElement>dynamicDiv=driver.findElements(By.xpath("//div//div//div//div//div//div[@class='row']"));
        
        

        
        
        for(int i =1;i<dynamicDiv.size();i++) {
            String room= "room" + Integer.toString(i);
            driver.findElement(By.id(room)).click();
            System.out.println(room);
        }
    
        //Assert.assertTrue(!driver.findElement(By.xpath("//div[@id='alert alert-danger']")).isDisplayed());
        
        
        
    
    }
    
    

}

这是我的功能文件 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 的第二个对象为空且未执行。

java selenium-webdriver cucumber page-factory
1个回答
0
投票

我认为线路故障是

driver.findElement(By.id("wifiCheckbox")).click();

在您登录和新页面准备就绪之间,可能需要一些时间(加载 css、在后台运行任何 js 等)。请记住,selenium 可能比真实浏览器上的真实场景慢。所以我会添加一些检查来查看该元素是否可用,然后可能等待 1-2 秒,再次检查并继续或失败。

每次更改页面时,这是一个很好的做法,可以减少不稳定的情况。 另外,我想说,考虑添加这些检查作为步骤,以明确您需要它们并推动其他开发人员遵循这种方法。

© www.soinside.com 2019 - 2024. All rights reserved.