When I run all the tests, some of them crash and I have a notification in the console "WARNING: WebDriverException thrown by findElement(By.xpath: some locator)" and "Session ID is null. Using WebDriver after calling quit()?". I have concluded that the failing tests are those that invoke the methods in which it was used WebDriverWait. But, I'm confused because when I run each test individually, each one passes, even those that failed when I ran them together. Does anyone know how I could solve this problem? Thank you.
Code:
package methods;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import java.io.InputStream;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
public class PropertyFile extends SetUp {
public static Properties properties;
public static InputStream inputStream;
public ExtentReports report;
public ExtentTest test;
@BeforeMethod
public void readPropertiesFile() {
try {
String workingDir = System.getProperty("user.dir");
System.out.println(workingDir);
properties = new Properties();
inputStream = readFile("application.properties");
properties.load(inputStream);
//uzima vrednost do chromeDrivera
String driverPath = properties.getProperty("DRIVER_PATH_WIN_CHROME");
//uzima vrednost do report-a
String reportPath = properties.getProperty("FOLDER_REPORT_PATH");
System.setProperty(properties.getProperty("DRIVER_NAME_CHROME"),
workingDir +
driverPath);
System.out.println(workingDir + driverPath);
// kreiranje reporta
report = new ExtentReports(workingDir + reportPath, false);
//URL do sajta koji testiramo
report.addSystemInfo("URL", "https://demoqa.com/");
createDriver();
getDriver().manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
// govorimo pretrazivacu koju stranicu da otvori
getDriver().get(properties.getProperty("URL"));
} catch (Exception e) {
System.out.println("Failed to instantiate and load properties and chrome
driver!");
}
}
@Test
public void check(){
test = report.startTest("exampe");
slider.moveSliderTo(100);
test.log(LogStatus.INFO, "Slider was moved");
MainMethods.waitElementToBeVisible(By.xpath("//span[@style = 'left:
20%;']"));
test.log(LogStatus.INFO, "Position of slider is on 20%");
}
@AfterMethod
public void tearDown(ITestResult testResult) throws Exception {
if (testResult.getStatus() == ITestResult.FAILURE) {
String path = methods.Screenshot.takeScreenshot(getDriver(),
methods.Screenshot.generateFileName(testResult));
String imgPath = test.addScreenCapture(path);
test.log(LogStatus.FAIL, "Test case which FAILED is " +
testResult.getName(),
imgPath);
test.log(LogStatus.FAIL, "Method which FAILED is " +
testResult.getThrowable());
} else if (testResult.getStatus() == ITestResult.SUCCESS) {
test.log(LogStatus.PASS, "Test case which PASSED IS " +
testResult.getName());
} else if (testResult.getStatus() == ITestResult.SKIP) {
test.log(LogStatus.PASS, "Test case which SKIPPED IS " +
testResult.getName());
}
getDriver().manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
quitDriver();
report.endTest(test);
report.flush();
}