Search code examples
javaselenium-webdriverassertassertion

equals is not working in inside for loop


this is replica of one of my client application. i want to test negative scenario like the value i give should not available in the dropdown list. i am going to take the j value for assert functionality. if j value is not 1, the value i give is not in the drop down list.

for below program, i am expecting j value to 1 but its showing 0 only. why iqual ignore case is not working inside for loop but it is working outside forloop. i am even listed all the values inside the forloop, it has "May" value.

is there any simple script to check the value i give is not in the dropdown list??

package Facebook;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;

public class Login2 {

    @Test
    public void Login() throws InterruptedException {
        System.setProperty("webdriver.chrome.driver",
                "D:\\Besent Technology\\Drivers\\chromedriver_win32_2.34\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.facebook.com/");

        List<WebElement> monthlist = driver.findElements(By.id("month"));

        String month = "May";
        String mon = "May";
        int i = 0;
        int j = 0;

        if (mon.equals(month)) {
            System.out.println(mon + " project should be selected");
            i++;
        }

        for (WebElement ele : monthlist) {
            String fbmonth = ele.getText().trim();
            System.out.println(ele.getText());
            if (fbmonth.equals(month)) {
                System.out.println(fbmonth + " project is displaying");
                j++;
            } else {
                continue;
            }

        }
        System.out.println("print i value: " + i);
        System.out.println("print j value: " + j);

    }
}

output

        [RemoteTestNG] detected TestNG version 6.14.2
        Starting ChromeDriver 2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1) on port 3885
        Only local connections are allowed.
        Aug 13, 2018 7:18:16 PM org.openqa.selenium.remote.ProtocolHandshake createSession
        INFO: Detected dialect: OSS
        May project should be selected
        Month
        Jan
        Feb
        Mar
        Apr
        May
        Jun
        Jul
        Aug
        Sept
        Oct
        Nov
        Dec
        print i value: 1
        print j value: 0
        PASSED: Login

        ===============================================
            Default test
            Tests run: 1, Failures: 0, Skips: 0
        ===============================================


        ===============================================
        Default suite
        Total tests run: 1, Failures: 0, Skips: 0
        ===============================================

Solution

  • enter image description hereOk, i found an answer for you by running the test you wrote and it is due to the fact that your variable fbmonth does not return month as Jan, Feb individual, infact it return the fbmonth is equal to "Month\nJan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug\nSep\nOct\nNov\nDec"

    You need to break/split this and iterate over to get what you want. Update use this to iterate:

    for (WebElement ele : monthlist) {
                System.out.println(ele.getText());
                String[] fbmonth = ele.getText().trim().split("\n");
                for(j=0;j<fbmonth.length;j++) {
                    System.out.println("fbmonth" +j +"="+ fbmonth[j] );
                    if (fbmonth[j].equals(month)) {
                        System.out.println(fbmonth[j] + " project is displaying");
    
                    }
                }
    
    
    
            }