我要测试expedia.com自动完成部分。我有四个Java页面。 DriverWapper,BasePage,HomePage和AutoPage。我分享了页面。当我测试代码时,它会在搜索框中写入纽约,但此后它不会写入相关的机场名称,并且测试失败。我该如何解决?
DriverWrapperPage
public class DriverWrapper {
private static WebDriver driver = null;
private static final String url = "https://www.expedia.com/";
@BeforeClass
public void before() {
//Set chromeDriver
System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
//Initialize webDriver interface
driver = new ChromeDriver( );
//Navigate to url
driver.navigate( ).to(url);
}
@AfterClass
public void tearDown(){
driver.quit();
}
public static WebDriver getDriver() {
return driver;
}
public static String getUrl() {
return url;
}
}
主页
public class HomePage extends BasePage {
//Locators
private By flightTabe = By.xpath(".//*[@id='tab-flight-tab-hp']");
private By flightOrigin = By.xpath(".//*[@id='flight-origin-hp-flight']");
private By results = By.className("results-item");
//Constructors
//Methods
void flighttab() {autoCompeteStatus(flightTabe);}
public void flightOrigin () {autoCompeteStatus(flightOrigin);}
public void result() {stat(results);}
}
BasePage
public class BasePage {
DriverWrapper driver = new DriverWrapper();
public void clickOn(By locator) {
DriverWrapper.getDriver( ).findElement(locator).click( );
}
public void setValue(String value, By locator) {
DriverWrapper.getDriver( ).findElement(locator).sendKeys(value);
}
public String getTextFromElement(By locator) {
return DriverWrapper.getDriver( ).findElement(locator).getText( );
}
public void autoCompeteStatus(By locator) {
driver.getDriver( ).findElement(locator).click( );
driver.getDriver( ).findElement(locator).sendKeys("NewYork");
List<WebElement> list = DriverWrapper.getDriver( ).findElements(locator);
for (WebElement ele : list) {
if (ele.getText( ).contains("New York, NY (JFK-John F. Kennedy Intl.)")) {
ele.click( );
break;
}
}
}
}
TestPage
@Test
public static void aout() throws InterruptedException {
HomePage home = new HomePage();
home.flighttab();
Thread.sleep(3000);
home.result();
Thread.sleep(3000);
home.result();
测试应该成功...但是失败