Hey there, I'm new to Selenium, scripting in Java and still learning the ropes by piecing together code from various tutorials and recorders. Currently, I'm working on a script to verify the presence of a specific 'element' (I would also like to do the reverse). While the script passes when it successfully locates the 'element', if I intentionally change the element details to ensure failure (since it doesn't actually exist), TestNG still marks the test as passing but flags it as a configuration failure?
I assume that I may be overlooking something to address the failed aspect of the test, but I'm not quite sure how to approach it. Every time I try to run into this issue.
package Links;
import org.testng.annotations.*;
import static org.testng.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class TestArea {
private WebDriver driver;
private StringBuffer verificationErrors = new StringBuffer();
@BeforeClass(alwaysRun = true)
public void setUp() throws Exception {
System.setProperty("webdriver.gecko.driver", "C:\\Automation\\SeleniumFiles\\Browser Drivers\\geckodriver.exe");
driver = new FirefoxDriver();
}
@Test
public void Example() throws Exception {
driver.get(
"http://MyWebsite");
try {
assertTrue(isElementPresent(
By.xpath("The Element I want look for ")));
} catch (Error e) {
verificationErrors.append(e.toString());
}
}
// -------------------------------------------------------------------------------------
@AfterClass(alwaysRun = true)
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
}
An example of a "passed" test with a failed configuration.
FAILED CONFIGURATION: @AfterClass tearDown java.lang.AssertionError: java.lang.AssertionError: expected [true] but found [false] at org.testng.Assert.fail(Assert.java:96) at Links.TestArea.tearDown(TestArea.java:39) 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:124) at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:59) at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:455) at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:222) at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:142) at org.testng.internal.TestMethodWorker.invokeAfterClassMethods(TestMethodWorker.java:214) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111) at org.testng.TestRunner.privateRun(TestRunner.java:648) at org.testng.TestRunner.run(TestRunner.java:505) at org.testng.SuiteRunner.runTest(SuiteRunner.java:455) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415) at org.testng.SuiteRunner.run(SuiteRunner.java:364) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208) at org.testng.TestNG.runSuitesLocally(TestNG.java:1137) at org.testng.TestNG.runSuites(TestNG.java:1049) at org.testng.TestNG.run(TestNG.java:1017) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
PASSED: Example
=============================================== Default test Tests run: 1, Failures: 0, Skips: 0
Configuration Failures: 1, Skips: 0
=============================================== Default suite Total tests run: 1, Failures: 0, Skips: 0 Configuration Failures: 1, Skips:
0
The configuration issue doesn't arise when the test successfully locates the element.
Thank you very much in advance