equals在for循环内部不起作用

时间:2018-08-13 13:59:11

标签: java selenium-webdriver assert assertion

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

对于以下程序,我期望j值为1,但仅显示0。为什么相等忽略大小写不在for循环内部工作,但在forloop外部工作。我什至列出了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
        ===============================================

1 个答案:

答案 0 :(得分:1)

enter image description here好吧,我通过运行编写的测试为您找到了答案,这是由于以下事实:您的变量fbmonth不像Jan,Feb个人那样返回month,实际上它返回了{{1 }}

您需要对此进行分解/拆分,然后进行迭代以获得所需的内容。  更新使用它来迭代:

fbmonth is equal to "Month\nJan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug\nSep\nOct\nNov\nDec"