java - equals 在 for 循环内部不起作用

标签 java selenium-webdriver assert assertion

这是我的一个客户端应用程序的副本。我想测试负面场景,例如我给出的值不应在下拉列表中可用。我将采用断言功能的 j 值。如果j值不为1,则我给出的值不在下拉列表中。

对于下面的程序,我期望 j 值为 1,但它仅显示 0。为什么 iqual 忽略大小写在 for 循环内部不起作用,但在 for 循环外部起作用。我什至列出了 forloop 内的所有值,它具有“May”值。

是否有任何简单的脚本来检查我给出的值不在下拉列表中?

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

    }
}

输出

        [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
        ===============================================

最佳答案

enter image description here好的,我通过运行您编写的测试找到了答案,这是由于您的变量 fbmonth 不返回月份为 Jan、Feb 个人,事实上它返回 fbmonth 等于“Month\nJan”\n二月\n三月\n四月\n五月\n六月\n七月\n八月\n九月\n十月\n十一月\n十二月"

你需要打破/分割它并迭代以获得你想要的。 更新使用它来迭代:

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

                }
            }



        }

关于java - equals 在 for 循环内部不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51824152/

相关文章:

java - 数字文字中是否有下划线结尾?

java - 在单独的线程中停止可运行的

java - 在 android 中接收彩信并将其显示在我的应用程序中作为 ListView

ruby-on-rails - Ocra 生成的 exe 不接受参数

c - 子进程会在 abort() 上发送 SIGCHLD 吗?

c++ - 异常与错误代码与断言

java - 在 Hibernate 中更新一对多字段

testing - 如何重新运行失败的 selenium-rc 测试脚本?

python - 发送 Ctrl+f python selenium chrome

express - Mocha 测试正在通过,但断言或期望失败