我要验证: 1)如果从城市为空,则用户得到警告,程序退出 2)出发日期不能为空或少于返回日期
这是代码:
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class e2e {
public static WebDriver driver;
public static void main(String[] args) {
//Go to URL
System.setProperty("webdriver.chrome.driver", "C:\\Users\\chromedriver_win32\\chromedriver.exe");
driver= new ChromeDriver();
driver.get("https://www.spicejet.com/");
//Travel city pickers
//Question1:If From city is blank generate warning
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(By.id("ctl00_mainContent_ddl_originStation1_CTXTaction")).click();
WebElement fromCity=driver.findElement(By.xpath("//a[@value='ATQ']"));
//driver.findElement(By.xpath("//a[@value='ATQ']")).click();
driver.findElement(By.xpath("//a[@value='']")).click();
String fromCt=fromCity.getText();
if(fromCt.equals(" ")){
System.out.println("You did not enter tarvelling from city");
driver.quit();
}
else{
driver.findElement(By.xpath("(//a[@value='DEL'])[2]")).click();}
//Travel date pickers
JavascriptExecutor jsLeave= (JavascriptExecutor)driver;
jsLeave.executeScript("document.getElementById('ctl00_mainContent_view_date1').value='12-05-2019'");
JavascriptExecutor jsReturn= (JavascriptExecutor)driver;
jsReturn.executeScript("document.getElementById('ctl00_mainContent_view_date2').value='18-05-2019'");
}
}
问题:当我尝试使用此行时,该行应使城市为空,但没有收到警告,但没有此类元素:无法定位元素故障。driver.findElement(By.xpath("//a[@value='']")).click();
第二个问题是当前正在填充05/12/2019,但没有返回日期,该字段中没有任何内容。
如果您有时间,我可以在这里提供一些帮助。预先感谢。
答案 0 :(得分:0)
这是检查第一种情况的方法,而第二种情况则永远不会发生,因为返回日期字段将禁用您出发日期之前的所有日期。
driver.get("https://spicejet.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
// click on search
driver.findElement(By.id("ctl00_mainContent_btn_FindFlights")).click();
// check from city error message
if (driver.findElement(By.id("ctl00_mainContent_ddl_originStation1_CTXT")).getText().equals("")) {
if (driver.findElement(By.id("view-origin-station")).getText().equals("Select Departure City")) {
System.out.println("Pass - Select Departure City message displayed as user did not enter from city.");
}else {
System.out.println("Fail - Select Departure City message not displayed when user not enter from city.");
}
}
// enter from city
driver.findElement(By.id("ctl00_mainContent_ddl_originStation1_CTXT")).click();
driver.findElement(By.xpath("//a[contains(.,'ATQ')]")).click();
// enter to city
driver.findElement(By.id("ctl00_mainContent_ddl_destinationStation1_CTXT")).click();
driver.findElement(By.xpath("//a[contains(.,'GOI')]")).click();