samedi 21 septembre 2019

Selenium only executes if != null part of loop, and does not run the "else if null " part of the loop

I am automating a sign up process on a webpage. On the page, when a user supplies govt id number, the page auto-populates the address fields. If the id does not match, the address fields are left blank.

I have a if-else statement where (condition 1: ID matches and address fields are auto-populated)..(If statement skips address fields, only fills out remaining 3 fields) (condition 2: ID does not match, all fields are blank)..(else statement fills out all 8 fields including address)

When I make condition 1 true, code runs If statement and works correctly. When I make condition 2 true, code again run what the If statement should do rather than the else statement

I am using Selenium Java for the automation.

I have tried putting individual if-else statements for each field object, I have tried alternating between if != null, if !field.equals(null). I have also tried doing field.gettext(), assigning it to a variable and checking variable.length != 0.

if (driver.findElement(By.name("city")) != null) {

    object.setField6;
    object.setField7;
    object.setField8;
    object.setField9;

} else {

    object.setField2;
    object.setField3;
    object.setField4;
    object.setField5;
    object.setField6;
    object.setField7;
    object.setField8;
    object.setField9;
}

I picked a random address field for my if statement.

if (cityField != null) {

     Only fill the three remaining fields

} else {

     Fill all 8 fields (i.e. including address fields)

}

When I run this, and put a valid id number, the if statement works correctly. But when I put an invalid id, which leaves the "cityField" null, the if statement executes instead of the "else" statement and only populates 3 fields instead of all 8.

This is all I am doing inside the object methods for each field:

method() {
  city.click();
  city.sendKeys(cityName); 
}

Aucun commentaire:

Enregistrer un commentaire