简体   繁体   中英

Selenium Java- text field validation and date validation for empty values

I want to validate: 1) If from city is blank then user get's a warning and program quits 2)Depart date is not empty or less than return date

This is the code:

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'");
    }
}

Problems: When I try to use this line , which should make from city empty I do not get the warning but no such element: Unable to locate element failure. driver.findElement(By.xpath("//a[@value='']")).click();
2nd problem is currently 05/12/2019 is getting populated but not return date, nothing goes into that field.

I can use some help here if you have time. Thanks in advance.

Here is the way how you can check the first scenario, and your 2nd scenario never happens as the return date field will be disable all the dates prior to your departure date.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM