t.me/atinfo_chat Telegram группа по автоматизации тестирования

После отработки всех тестов Selenium не закрывает браузер

Теги: #<Tag:0x00007f0e69f229b0> #<Tag:0x00007f0e69f228e8> #<Tag:0x00007f0e69f22820> #<Tag:0x00007f0e69f22758> #<Tag:0x00007f0e69f22690> #<Tag:0x00007f0e69f225a0>

Проблема (Вопрос) заключается …
После отработки всех тестов Selenium не закрывает браузер
Я попытался сделать …
пробовал driver.close; driver.quit; прокидывал в try/finaly
У меня получилось …

У меня не получилось …

Код

import io.qameta.allure.Epic;
import io.qameta.allure.Owner;
import io.qameta.allure.Story;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

@Epic("ЭПИК")

public class Test1 extends Methods {

    @Owner("Alexeenko Maxim")
    @Story("СТОРИ")
    @Test(description = "Название метода")
    public void test() throws InterruptedException {
        BrowserSetting bs = new BrowserSetting();
        WebDriver driver = bs.BrowserSettings();

        WebElement clubLink = waitElementXpath((ChromeDriver) driver, "//span[text()=\"Клуб\"]");
        clubLink.click();

        Thread.sleep(3000);
        Assert.assertEquals(driver.getTitle(), "Клуб | Футбольный клуб «Динамо» Москва. Сила — в движении!");

        driver.close();

    }

    @Owner("Alexeenko Maxim")
    @Story("СТОРИ1")
    @Test(description = "Название метода2")
    public void test1() throws InterruptedException {
        BrowserSetting bs = new BrowserSetting();
        WebDriver driver = bs.BrowserSettings();

        WebElement clubLink = waitElementXpath((ChromeDriver) driver, "//span[text()=\"Клуб\"]");
        clubLink.click();

        Thread.sleep(3000);
        Assert.assertEquals(driver.getTitle(), "Клуб | фыФутбольный клуб «Динамо» Москва. Сила — в движении!");

        driver.close();

    }

    @Owner("Alexeenko Maxim")
    @Story("СТОРИ2")
    @Test(description = "Название метода3")
    public void test2() throws InterruptedException {
        BrowserSetting bs = new BrowserSetting();
        WebDriver driver = bs.BrowserSettings();

        WebElement clubLink = waitElementXpath((ChromeDriver) driver, "//span[text()=\"Клуб\"]");
        clubLink.click();

        Thread.sleep(3000);
        try {
            Assert.assertEquals(driver.getTitle(), "Клуб | Футбольный клуб «Динамо» Москва. Сила — в движении!");

        } finally {
            driver.close();
        }

    }

}

Логи и ошибка вот такая …
/home/max/.jdks/openjdk-16.0.1/bin/java -ea -javaagent:/home/max/.m2/repository/org/aspectj/aspectjweaver/1.8.10/aspectjweaver-1.8.10.jar -Didea.test.cyclic.buffer.size=1048576 -javaagent:/opt/idea-IC-211.6693.111/lib/idea_rt.jar=35777:/opt/idea-IC-211.6693.111/bin -Dfile.encoding=UTF-8 -classpath /opt/idea-IC-211.6693.111/lib/idea_rt.jar:/opt/idea-IC-211.6693.111/plugins/testng/lib/testng-rt.jar:/home/max/IdeaProjects/Dnm/target/test-classes:/home/max/IdeaProjects/Dnm/target/classes:/home/max/.m2/repository/io/qameta/allure/allure-testng/2.14.0/allure-testng-2.14.0.jar:/home/max/.m2/repository/io/qameta/allure/allure-java-commons/2.14.0/allure-java-commons-2.14.0.jar:/home/max/.m2/repository/io/qameta/allure/allure-model/2.14.0/allure-model-2.14.0.jar:/home/max/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.12.3/jackson-databind-2.12.3.jar:/home/max/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.12.3/jackson-annotations-2.12.3.jar:/home/max/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.12.3/jackson-core-2.12.3.jar:/home/max/.m2/repository/org/apache/tika/tika-core/1.26/tika-core-1.26.jar:/home/max/.m2/repository/javax/xml/bind/jaxb-api/2.3.1/jaxb-api-2.3.1.jar:/home/max/.m2/repository/javax/activation/javax.activation-api/1.2.0/javax.activation-api-1.2.0.jar:/home/max/.m2/repository/javax/activation/activation/1.1.1/activation-1.1.1.jar:/home/max/.m2/repository/org/jooq/joor-java-8/0.9.14/joor-java-8-0.9.14.jar:/home/max/.m2/repository/io/qameta/allure/allure-test-filter/2.14.0/allure-test-filter-2.14.0.jar:/home/max/.m2/repository/org/testng/testng/7.4.0/testng-7.4.0.jar:/home/max/.m2/repository/com/beust/jcommander/1.78/jcommander-1.78.jar:/home/max/.m2/repository/org/webjars/jquery/3.5.1/jquery-3.5.1.jar:/home/max/.m2/repository/org/slf4j/slf4j-nop/1.7.13/slf4j-nop-1.7.13.jar:/home/max/.m2/repository/org/slf4j/slf4j-api/1.7.13/slf4j-api-1.7.13.jar:/home/max/.m2/repository/com/google/guava/guava/21.0/guava-21.0.jar:/home/max/.m2/repository/org/seleniumhq/selenium/selenium-java/4.0.0-beta-3/selenium-java-4.0.0-beta-3.jar:/home/max/.m2/repository/org/seleniumhq/selenium/selenium-api/4.0.0-beta-3/selenium-api-4.0.0-beta-3.jar:/home/max/.m2/repository/org/seleniumhq/selenium/selenium-chrome-driver/4.0.0-beta-3/selenium-chrome-driver-4.0.0-beta-3.jar:/home/max/.m2/repository/com/google/auto/service/auto-service-annotations/1.0-rc7/auto-service-annotations-1.0-rc7.jar:/home/max/.m2/repository/com/google/auto/service/auto-service/1.0-rc7/auto-service-1.0-rc7.jar:/home/max/.m2/repository/com/google/auto/auto-common/0.10/auto-common-0.10.jar:/home/max/.m2/repository/org/seleniumhq/selenium/selenium-chromium-driver/4.0.0-beta-3/selenium-chromium-driver-4.0.0-beta-3.jar:/home/max/.m2/repository/org/seleniumhq/selenium/selenium-devtools/4.0.0-beta-3/selenium-devtools-4.0.0-beta-3.jar:/home/max/.m2/repository/org/seleniumhq/selenium/selenium-http/4.0.0-beta-3/selenium-http-4.0.0-beta-3.jar:/home/max/.m2/repository/org/seleniumhq/selenium/selenium-json/4.0.0-beta-3/selenium-json-4.0.0-beta-3.jar:/home/max/.m2/repository/org/seleniumhq/selenium/selenium-devtools-v85/4.0.0-beta-3/selenium-devtools-v85-4.0.0-beta-3.jar:/home/max/.m2/repository/org/seleniumhq/selenium/selenium-devtools-v88/4.0.0-beta-3/selenium-devtools-v88-4.0.0-beta-3.jar:/home/max/.m2/repository/org/seleniumhq/selenium/selenium-devtools-v89/4.0.0-beta-3/selenium-devtools-v89-4.0.0-beta-3.jar:/home/max/.m2/repository/org/seleniumhq/selenium/selenium-devtools-v90/4.0.0-beta-3/selenium-devtools-v90-4.0.0-beta-3.jar:/home/max/.m2/repository/org/seleniumhq/selenium/selenium-devtools-v91/4.0.0-beta-3/selenium-devtools-v91-4.0.0-beta-3.jar:/home/max/.m2/repository/org/seleniumhq/selenium/selenium-edge-driver/4.0.0-beta-3/selenium-edge-driver-4.0.0-beta-3.jar:/home/max/.m2/repository/org/seleniumhq/selenium/selenium-firefox-driver/4.0.0-beta-3/selenium-firefox-driver-4.0.0-beta-3.jar:/home/max/.m2/repository/org/seleniumhq/selenium/selenium-firefox-xpi-driver/4.0.0-beta-3/selenium-firefox-xpi-driver-4.0.0-beta-3.jar:/home/max/.m2/repository/org/seleniumhq/selenium/selenium-ie-driver/4.0.0-beta-3/selenium-ie-driver-4.0.0-beta-3.jar:/home/max/.m2/repository/org/seleniumhq/selenium/selenium-opera-driver/4.0.0-beta-3/selenium-opera-driver-4.0.0-beta-3.jar:/home/max/.m2/repository/org/seleniumhq/selenium/selenium-remote-driver/4.0.0-beta-3/selenium-remote-driver-4.0.0-beta-3.jar:/home/max/.m2/repository/io/netty/netty-buffer/4.1.58.Final/netty-buffer-4.1.58.Final.jar:/home/max/.m2/repository/io/netty/netty-codec-http/4.1.58.Final/netty-codec-http-4.1.58.Final.jar:/home/max/.m2/repository/io/netty/netty-codec/4.1.58.Final/netty-codec-4.1.58.Final.jar:/home/max/.m2/repository/io/netty/netty-handler/4.1.58.Final/netty-handler-4.1.58.Final.jar:/home/max/.m2/repository/io/netty/netty-common/4.1.58.Final/netty-common-4.1.58.Final.jar:/home/max/.m2/repository/io/netty/netty-transport-native-epoll/4.1.58.Final/netty-transport-native-epoll-4.1.58.Final.jar:/home/max/.m2/repository/io/netty/netty-transport-native-kqueue/4.1.58.Final/netty-transport-native-kqueue-4.1.58.Final.jar:/home/max/.m2/repository/io/netty/netty-transport-native-unix-common/4.1.58.Final/netty-transport-native-unix-common-4.1.58.Final.jar:/home/max/.m2/repository/io/netty/netty-transport/4.1.58.Final/netty-transport-4.1.58.Final.jar:/home/max/.m2/repository/io/netty/netty-resolver/4.1.58.Final/netty-resolver-4.1.58.Final.jar:/home/max/.m2/repository/io/opentelemetry/opentelemetry-api/1.0.0/opentelemetry-api-1.0.0.jar:/home/max/.m2/repository/io/opentelemetry/opentelemetry-context/1.0.0/opentelemetry-context-1.0.0.jar:/home/max/.m2/repository/io/opentelemetry/opentelemetry-exporter-logging/1.0.0/opentelemetry-exporter-logging-1.0.0.jar:/home/max/.m2/repository/io/opentelemetry/opentelemetry-sdk-metrics/1.0.0-alpha/opentelemetry-sdk-metrics-1.0.0-alpha.jar:/home/max/.m2/repository/io/opentelemetry/opentelemetry-sdk-common/1.0.0/opentelemetry-sdk-common-1.0.0.jar:/home/max/.m2/repository/io/opentelemetry/opentelemetry-sdk-extension-autoconfigure/1.0.0-alpha/opentelemetry-sdk-extension-autoconfigure-1.0.0-alpha.jar:/home/max/.m2/repository/io/opentelemetry/opentelemetry-sdk-trace/1.0.0/opentelemetry-sdk-trace-1.0.0.jar:/home/max/.m2/repository/io/opentelemetry/opentelemetry-api-metrics/1.0.0-alpha/opentelemetry-api-metrics-1.0.0-alpha.jar:/home/max/.m2/repository/io/opentelemetry/opentelemetry-sdk/1.0.0/opentelemetry-sdk-1.0.0.jar:/home/max/.m2/repository/io/opentelemetry/opentelemetry-semconv/1.0.0-alpha/opentelemetry-semconv-1.0.0-alpha.jar:/home/max/.m2/repository/net/bytebuddy/byte-buddy/1.10.19/byte-buddy-1.10.19.jar:/home/max/.m2/repository/org/apache/commons/commons-exec/1.3/commons-exec-1.3.jar:/home/max/.m2/repository/org/asynchttpclient/async-http-client/2.12.2/async-http-client-2.12.2.jar:/home/max/.m2/repository/org/asynchttpclient/async-http-client-netty-utils/2.12.2/async-http-client-netty-utils-2.12.2.jar:/home/max/.m2/repository/io/netty/netty-codec-socks/4.1.53.Final/netty-codec-socks-4.1.53.Final.jar:/home/max/.m2/repository/io/netty/netty-handler-proxy/4.1.53.Final/netty-handler-proxy-4.1.53.Final.jar:/home/max/.m2/repository/io/netty/netty-transport-native-epoll/4.1.53.Final/netty-transport-native-epoll-4.1.53.Final-linux-x86_64.jar:/home/max/.m2/repository/io/netty/netty-transport-native-kqueue/4.1.53.Final/netty-transport-native-kqueue-4.1.53.Final-osx-x86_64.jar:/home/max/.m2/repository/org/reactivestreams/reactive-streams/1.0.3/reactive-streams-1.0.3.jar:/home/max/.m2/repository/com/typesafe/netty/netty-reactive-streams/2.0.4/netty-reactive-streams-2.0.4.jar:/home/max/.m2/repository/com/sun/activation/jakarta.activation/1.2.2/jakarta.activation-1.2.2.jar:/home/max/.m2/repository/org/seleniumhq/selenium/selenium-safari-driver/4.0.0-beta-3/selenium-safari-driver-4.0.0-beta-3.jar:/home/max/.m2/repository/org/seleniumhq/selenium/selenium-support/4.0.0-beta-3/selenium-support-4.0.0-beta-3.jar com.intellij.rt.testng.RemoteTestNGStarter -usedefaultlisteners false -socket43635 @w@/tmp/idea_working_dirs_testng.tmp -temp /tmp/idea_testng.tmp
Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=on -Dswing.aatext=true
Starting ChromeDriver 90.0.4430.24 (4c6d850f087da467d926e8eddb76550aed655991-refs/branch-heads/4430@{#429}) on port 4103
Only local connections are allowed.
Please see ChromeDriver - WebDriver for Chrome - Security Considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
июн. 07, 2021 10:09:49 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
июн. 07, 2021 10:09:49 AM org.openqa.selenium.devtools.CdpVersionFinder findNearestMatch
INFO: Found exact CDP implementation for version 90
Starting ChromeDriver 90.0.4430.24 (4c6d850f087da467d926e8eddb76550aed655991-refs/branch-heads/4430@{#429}) on port 19688
Only local connections are allowed.
Please see ChromeDriver - WebDriver for Chrome - Security Considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
июн. 07, 2021 10:10:03 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
июн. 07, 2021 10:10:03 AM org.openqa.selenium.devtools.CdpVersionFinder findNearestMatch
INFO: Found exact CDP implementation for version 90
Starting ChromeDriver 90.0.4430.24 (4c6d850f087da467d926e8eddb76550aed655991-refs/branch-heads/4430@{#429}) on port 22949
Only local connections are allowed.
Please see ChromeDriver - WebDriver for Chrome - Security Considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
июн. 07, 2021 10:10:14 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
июн. 07, 2021 10:10:14 AM org.openqa.selenium.devtools.CdpVersionFinder findNearestMatch
INFO: Found exact CDP implementation for version 90

java.lang.AssertionError:
Expected :Клуб | фыФутбольный клуб «Динамо» Москва. Сила — в движении!
Actual :Клуб | Футбольный клуб «Динамо» Москва. Сила — в движении!

at org.testng.Assert.fail(Assert.java:99)
at org.testng.Assert.failNotEquals(Assert.java:1037)
at org.testng.Assert.assertEqualsImpl(Assert.java:140)
at org.testng.Assert.assertEquals(Assert.java:122)
at org.testng.Assert.assertEquals(Assert.java:629)
at org.testng.Assert.assertEquals(Assert.java:639)
at Test1.accountDelete1(Test1.java:42)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)
at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:598)
at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173)
at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:824)
at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.testng.TestRunner.privateRun(TestRunner.java:794)
at org.testng.TestRunner.run(TestRunner.java:596)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:377)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:371)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:332)
at org.testng.SuiteRunner.run(SuiteRunner.java:276)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1212)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1134)
at org.testng.TestNG.runSuites(TestNG.java:1063)
at org.testng.TestNG.run(TestNG.java:1031)
at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:109)

===============================================
Default Suite
Total tests run: 3, Passes: 2, Failures: 1, Skips: 0

Process finished with exit code 0

Версии ОС и софта следующие …

Попробуйте так

@AfterClass
public void tearDown()
{
    driver.close(); //Закрывает сессию WebDriver
}

А зачем вы в каждом методе создаёте новый экземпляр WebDriver? В этом есть какой-то сакральный смысл? В TestNg же есть аннотация @BeforeMethod, которая выполняется перед каждым тестовым методом, если вам это нужно делать. И @AfterMethod будет выполнять код после каждого тестового метода.

Сакрального смысла нет) Я просто новичок. Только начал познавать автотесты :slightly_smiling_face:
Если WebDriver вынести то его не видно внутри тестовых методах

public class Test1 extends Methods {

private WebDriver driver = null;
private BrowserSetting bs = null;

@BeforeClass
public void init(){
  bs = new BrowserSetting();
  driver = bs.BrowserSetting();
}

Спасибо тебе, добрый человек. Все работает как надо :+1: