equals 在 for 循环中不起作用
equals is not working in inside for loop
这是我的一个客户端应用程序的副本。我想测试负面情况,比如我给的值不应该出现在下拉列表中。我将采用 j 值来断言功能。如果 j 值不为 1,则我给的值不在下拉列表中。
对于下面的程序,我期望 j 值为 1,但它只显示 0。为什么 iqual 忽略大小写在 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
===============================================
好的,我通过 运行 你写的测试找到了你的答案,这是因为你的变量 fbmonth 不像 Jan 那样 return 月, Feb 个人,事实上 return fbmonth is equal to "Month\nJan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug\nSep\nOct\nNov\nDec"
您需要 break/split 这个并迭代以获得您想要的。
更新使用它来迭代:
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");
}
}
}
这是我的一个客户端应用程序的副本。我想测试负面情况,比如我给的值不应该出现在下拉列表中。我将采用 j 值来断言功能。如果 j 值不为 1,则我给的值不在下拉列表中。
对于下面的程序,我期望 j 值为 1,但它只显示 0。为什么 iqual 忽略大小写在 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
===============================================
fbmonth is equal to "Month\nJan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug\nSep\nOct\nNov\nDec"
您需要 break/split 这个并迭代以获得您想要的。 更新使用它来迭代:
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");
}
}
}