Question

I was writing a test in Selenium WebDriver. One of the function I created always evaluates to false.

---Test Case Class---

package com;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.Select;


public class EfroTechCareers {
    private WebDriver driver;

    public EfroTechCareers(WebDriver driver)
    {
        this.driver = driver;
        if(!"Jobs and Career at Efrotech | Efrotech Services".equals(driver.getTitle()))
        {
            throw new IllegalStateException("This is not the Careers Page");
        }
    }

    By applyingFor = By.id("ddlJobs");
    By candidateName = By.id("txtName");
    By candidateFatherName = By.id("txtFName");
    By candidateGender = By.id("ddlGender");
    By candidateAddress = By.id("txtAddress");
    By candidateCity = By.id("ddlResumeCity");
    By candidateEmail = By.id("txtEmail");
    By candidateHomePhone = By.id("txtPhoneHome");
    By candidateOfficePhone = By.id("txtPhoneOff");
    By candidateCellPhone = By.id("txtPhoneMob");
    By candidateCNIC = By.id("txtCNIC");
    By candidateExperience = By.id("ddlExperience");
    By candidateCurrentSalary = By.id("txtCurrentSalary");
    By candidateExpectedSalary = By.id("txtExpectedSalary");
    By btnFormReset = By.id("btnReset");

    public EfroTechCareers formFillCareers() 
    {
        driver.switchTo().frame(driver.findElement(By.cssSelector("iframe[height='520']")));
        new Select(driver.findElement(applyingFor)).selectByVisibleText("QA Engineer");
        driver.findElement(candidateName).sendKeys("Candidate Name");
        driver.findElement(candidateFatherName).sendKeys("Cand. Father's Name");
        new Select(driver.findElement(candidateGender)).selectByVisibleText("Male");
        driver.findElement(candidateAddress).sendKeys("XYZ Lane 123 Road");
        new Select(driver.findElement(candidateCity)).selectByVisibleText("Karachi");
        driver.findElement(candidateEmail).sendKeys("name@domainname.com");
        driver.findElement(candidateHomePhone).sendKeys("12345678");
        driver.findElement(candidateOfficePhone).sendKeys("87654321");
        driver.findElement(candidateCellPhone).sendKeys("03331234567");
        driver.findElement(candidateCNIC).sendKeys("12345-6789458-9");
        new Select(driver.findElement(candidateExperience)).selectByVisibleText("5 Years");
        driver.findElement(candidateCurrentSalary).sendKeys("123456");
        driver.findElement(candidateExpectedSalary).sendKeys("654123");
        driver.findElement(btnFormReset).click();
                return this;
    }

    public boolean verifyFormReset()
    {

        if (
                ("-Select-".equals(new Select(driver.findElement(applyingFor)).getFirstSelectedOption().getText()) &&    
                "".equals(driver.findElement(candidateName).getText())  &&
                "".equals(driver.findElement(candidateFatherName).getText()) && 
                "-Select-".equals(new Select(driver.findElement(candidateGender)).getFirstSelectedOption().getText()) && 
                "".equals(driver.findElement(candidateAddress).getText()) &&
                "-Select-".equals(new Select(driver.findElement(candidateCity)).getFirstSelectedOption().getText()) && 
                "".equals(driver.findElement(candidateEmail).getText())  && 
                "".equals(driver.findElement(candidateHomePhone).getText()) && 
                "".equals(driver.findElement(candidateOfficePhone).getText()) && 
                "".equals(driver.findElement(candidateCellPhone).getText())  &&
                "".equals(driver.findElement(candidateCNIC).getText())  &&
                "-Select-".equals(new Select(driver.findElement(candidateExperience)).getFirstSelectedOption().getText()))
                        )
                    return true;
                                else 
                                    return false;
    }
}

=====================

---Here's the main class--

package com;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.support.PageFactory;

public class EfroTechMain {

    /**
     * @param args
     *  
     */
    public static WebDriver driver;
    public static String baseUrl;

    public static void main(String[] args)  {
        // TODO Auto-generated method stub
        baseUrl = "http://www.efrotech.com";
        driver = new HtmlUnitDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get(baseUrl +"/careers");


        EfroTechCareers eTC = PageFactory.initElements(driver, EfroTechCareers.class);

        eTC.formFillCareers();
        if (eTC.verifyFormReset() == true)
        {
            System.out.println("All fields are reset to defaults");
        } else
            System.out.println("Some fields are not reset to defaults");



    }

}

=====================

The method verifyFormReset() is always evaluates to false, and sometimes evaluates to true if only single statement is checked within if() block.

The same method evaluates to correct boolean once created in a single main() class.

Was it helpful?

Solution 2

The problem has been solved. The issue was the HTMLUnitDriver driver that is doing fast execution and the value that I have to compare was not matching with the expected value resulting in method always evaluates to false. Using the FirefoxDriver does the trick.

Thank yall for responding.

OTHER TIPS

Try creating a method, and print out the result to see the false one(s).

You can use a method like this:

public Boolean verifyElementEmpty(By by, String s) {
    Boolean check = driver.findElement(by).getText().equals(s);
    System.out.println("Element: " + by.toString() + " Empty: " + check);
    return check;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top