1

Issue: I'm trying to close my webdriver and and used both driver.close and driver.quit in my code and no luck. Is it required to have the @Before parameter in order to use the @After?

Code:

import org.junit.After;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

 public class Automate {
     private WebDriver driver;
     private String baseUrl;
     private boolean acceptNextAlert = true;
     private StringBuffer verificationErrors = new StringBuffer();
   @Test
   public void LaunchChrome_Method1() {`
   System.setProperty("webdriver.chrome.driver","C:\\Users\\HASANK\\Desktop\\tEST\\chromedriver.exe");

          ChromeOptions options = new ChromeOptions();
          options.addArguments("disable-infobars");

          WebDriver driver = new ChromeDriver(options);
          driver.get("http://www.google.com");
          driver.get("https://www.google.com/");
       driver.findElement(By.id("lst-ib")).clear();
       driver.findElement(By.id("lst-ib")).sendKeys("cnn");
       driver.findElement(By.id("lst-ib")).sendKeys(Keys.ENTER);
       driver.findElement(By.linkText("CNN - Breaking News, Latest News and Videos")).click();
       }
   @After
public void tearDown() throws Exception {
  driver.close();
  driver.quit();
}
GhostCat
  • 137,827
  • 25
  • 176
  • 248
BK Hasan
  • 121
  • 1
  • 1
  • 9

2 Answers2

0

Even i have faced the same issue earlier, try updating both the chrome and chromedriver and try one more time. If it is still not working try an alternate by creating a method

    public static void killDriverInstance() {
    try {
        String basePath = System.getProperty("user.dir");
        String finalPath = basePath + "\\src\\utils\\killDriver.bat";
        Process process = Runtime.getRuntime().exec(finalPath);
        process.waitFor();
    } catch (Exception ex) {
        System.out.println("Exception in killDriverInstance method:" + ex.toString());
    }
}

and in killdriver.bat file write this

REM Kill Chrome Driver 
taskkill /im chromedriver.exe /f

REM Kill Chrome browser
REM taskkill /im chrome.exe /f
Sandeep Raulo
  • 154
  • 1
  • 5
  • 21
0

Using this kind of method for this in C# language:

private void CloseChromeDriverProcesses()
        {
            var chromeDriverProcesses = Process.GetProcesses().
                Where(pr => pr.ProcessName == "chromedriver");

            if (chromeDriverProcesses.Count() == 0)
            {
                return;
            }

            foreach (var process in chromeDriverProcesses)
            {
                process.Kill();
            }
        }

I think that some minutes, and you can rewrite this, using Java language.

Ukrainis
  • 534
  • 2
  • 16