我在比较两个字符串时遇到问题。两个字符串都相同,但条件仍然失败。下面是代码和屏幕截图。我得到的错误。
以下是代码,
public void validatePageHeader(String strExpectedPageHeader) {
try {
Thread.sleep(2000);
String strActualPageHeader = weHomePageHeader.getText();
System.out.println("strExpectedPageHeader :"+strExpectedPageHeader);
System.out.println("strActualPageHeader :"+strActualPageHeader);
if(weHomePageHeader.getText().equalsIgnoreCase(strExpectedPageHeader)){
System.out.println("Matched");
}
else{
System.out.println("Not Matched");
}
Assert.assertEquals(strActualPageHeader, strExpectedPageHeader );
} catch (Exception e) {
System.out.println("Exception in validating page header "+ e.getMessage());
e.printStackTrace();
}
}
以下是控制台中显示的错误。
[TestNG] Running:
C:\Users\Sanju\AppData\Local\Temp\testng-eclipse-1613164258\testng- customsuite.xml
strExpectedPageHeader :Guru99 Bank
strActualPageHeader :Guru99 Bank
Not Matched
[Utils] Attempting to create F:\SeleniumPractice\Guru99Bank\test- output\Default suite\Default test.xml
[Utils] Directory F:\SeleniumPractice\Guru99Bank\test-output\Default suite exists: true
PASSED: launchApp
FAILED: testLoginPageHeader("Guru99 Bank Home Page", "Guru99 Bank", "mngr56592", "umYzehE")
java.lang.AssertionError: expected [Guru99 Bank] but found [Guru99 Bank]
at org.testng.Assert.fail(Assert.java:94)
at org.testng.Assert.failNotEquals(Assert.java:513)
at org.testng.Assert.assertEqualsImpl(Assert.java:135)
at org.testng.Assert.assertEquals(Assert.java:116)
at org.testng.Assert.assertEquals(Assert.java:190)
at org.testng.Assert.assertEquals(Assert.java:200)
at com.pageobjects.LoginPage.validatePageHeader(LoginPage.java:63)
at com.driver.Login.testLoginPageHeader(Login.java:59)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:100)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:646)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:811)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1137)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:753)
at org.testng.TestRunner.run(TestRunner.java:607)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:368)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:363)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:321)
at org.testng.SuiteRunner.run(SuiteRunner.java:270)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1284)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1209)
at org.testng.TestNG.runSuites(TestNG.java:1124)
at org.testng.TestNG.run(TestNG.java:1096)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)
===============================================
Default test
Tests run: 2, Failures: 1, Skips: 0
===============================================
答案 0 :(得分:1)
您可以尝试使用.trim功能,或者在您正在进行测试的地方发送HTML文件。您可以尝试:
String strActualPageHeader = weHomePageHeader.getText().trim();
System.out.println("strExpectedPageHeader :"+strExpectedPageHeader);
System.out.println("strActualPageHeader :"+strActualPageHeader);
if(weHomePageHeader.getText().trim().equalsIgnoreCase(strExpectedPageHeader)){
System.out.println("Matched");
}
else{
System.out.println("Not Matched");
}