警告:`WebDriverException` 由 `findElement` 方法在 Selenium 中抛出。

huangapple 未分类评论58阅读模式
英文:

warning webdriverexception thrown by findelement selenium

问题

当我运行所有测试时,其中一些测试会崩溃,并且在控制台中收到通知:“WARNING: WebDriverException thrown by findElement(By.xpath: some locator)”和“Session ID is null. Using WebDriver after calling quit()?”。我已经得出结论,失败的测试是调用使用WebDriverWait的方法所导致的。但是,我感到困惑,因为当我单独运行每个测试时,每个测试都会通过,甚至是那些在一起运行时失败的测试。有人知道我如何解决这个问题吗?谢谢。

代码:

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);

            // 获取 chromeDriver 路径
            String driverPath = properties.getProperty("DRIVER_PATH_WIN_CHROME");
            // 获取报告路径
            String reportPath = properties.getProperty("FOLDER_REPORT_PATH");

            System.setProperty(properties.getProperty("DRIVER_NAME_CHROME"),
                    workingDir +
                    driverPath);
            System.out.println(workingDir + driverPath);

            // 创建报告
            report = new ExtentReports(workingDir + reportPath, false);
            // 被测试网站的 URL
            report.addSystemInfo("URL", "https://demoqa.com/");

            createDriver();

            getDriver().manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

            // 告诉浏览器打开哪个页面
            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();
    }
}
英文:

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();
}

huangapple
  • 本文由 发表于 2020年5月3日 22:08:58
  • 转载请务必保留本文链接:https://java.coder-hub.com/61575833.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定