gpt4 book ai didi

java - Selenium Java - 文本字段验证和空值的日期验证

转载 作者:行者123 更新时间:2023-12-02 05:46:08 24 4
gpt4 key购买 nike

我想验证: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'");
}
}

问题:当我尝试使用这条线时,它应该使 from city 为空,但我没有收到警告,但没有这样的元素:无法定位元素失败。driver.findElement(By.xpath("//a[@value='']")).click();
第二个问题是当前正在填充 05/12/2019,但没有返回日期,该字段中没有任何内容。

如果您有时间,我可以在这里提供一些帮助。提前致谢。

最佳答案

以下是检查第一种情况的方法,而第二种情况永远不会发生,因为返回日期字段将禁用出发日期之前的所有日期。

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

关于java - Selenium Java - 文本字段验证和空值的日期验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56094456/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com