[英]Selenium Java- text field validation and date validation for empty values
我要驗證: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'");
}
}
問題:當我嘗試使用此行時,該行應使city變空,但沒有收到警告,但沒有此類元素:無法找到元素故障。 driver.findElement(By.xpath("//a[@value='']")).click();
第二個問題當前正在填充2019年5月12日,但沒有返回日期,該字段中沒有任何內容。
如果您有時間,我可以在這里提供一些幫助。 提前致謝。
這是檢查第一種情況的方法,而第二種情況永遠不會發生,因為返回日期字段將禁用您出發日期之前的所有日期。
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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.