As described in the question, when i initialize an instance of selenium web driver, my java program does not close after the main method has finished running. I am using the sample code from the official Selenium documentation:
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", pathToWebdriver);
WebDriver driver = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
try {
driver.get("https://google.com/ncr");
driver.findElement(By.name("q")).sendKeys("cheese" + Keys.ENTER);
WebElement firstResult = wait.until(presenceOfElementLocated(By.cssSelector("h3>div")));
System.out.println(firstResult.getAttribute("textContent"));
} finally {
driver.quit();
}
System.out.println(Thread.getAllStackTraces().keySet());
}
Output:
[Thread[ForkJoinPool.commonPool-worker-3,5,main], Thread[Monitor Ctrl-Break,5,main], Thread[AsyncHttpClient-3-1,5,main], Thread[Signal Dispatcher,9,system], Thread[Common-Cleaner,8,InnocuousThreadGroup], Thread[process reaper,10,system], Thread[Reference Handler,10,system], Thread[AsyncHttpClient-timer-1-1,5,main], Thread[Attach Listener,9,system], Thread[AsyncHttpClient-timer-4-1,5,main], Thread[Finalizer,8,system], Thread[main,5,main]]
PS: Its the same if im using chrome webdriver or driver.close()
Edit:
This problem seems to be selenium-4.0.0-alpha only
I have a couple issues when I run the test with edge web browser.
The first issue is that every time I run the test by run as > testng test , the automation will close all the existing open edge web browser and then it will open a brand new edge browser but it is blank (could not load the url)
The second issue is that the automation would only load the url if I manually close all of the existing edge browser.
Is there a way to fix these issues? please help me thank you in advance.
Here is my code
public class OpenThePageUsingEdge {
public String baseUrl = "https://catalog-qa.baylorgenetics.com/search";
String driverPath = "C:\\Eclipse\\MicrosoftWebDriver.exe";
public WebDriver driver ;
//initiate NameofInsured as the GenerateData class
//GenerateData NameOfAddrecipients;
#BeforeTest
public void setup(ITestContext ctx) {
TestRunner runner = (TestRunner) ctx;
runner.setOutputDirectory("J:\\zzQA Selenium Automation Suite\\Test Results");
}
#Test
public void openThePageusingEdge () throws InterruptedException {
System.out.println("launching Edge browser");
System.setProperty("webdriver.edge.driver", driverPath);
driver = new EdgeDriver();
driver.get(baseUrl);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
I am using cucumber with POM.I have created a file called "Driver factory" where i have initialised a String browserName as a parameter to public static void openBrowser(String browserName). (Because i want to test in multiple browser).I am trying to call method in hooks file df.openBrowser(browserName);
public static void openBrowser(String browserName) throws Exception{
if(browserName.equalsIgnoreCase("chrome")) {
System.out.println("***Test cases are executing in Chrome****");
System.setProperty("webdriver.webdriver.chrome.driver.driver","C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
I am getting error in Hooks.java file saying "browserName cannot be resolved to a variable".
How do I resolve this?
I just have learned about selenium.
My code:
public class FirstTest {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://mail.google.com/");
driver.manage().window().maximize();
driver.findElement(By.id("Email")).sendKeys(" YOUR USER NAME");
driver.findElement(By.id("Passwd")).sendKeys("YOUR PASSWORD");
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.findElement(By.id("signIn")).click();
driver.findElement(By.xpath("//div[#class='z0']/div")).click();
driver.findElement(By.xpath("//div[#class='gb_1 gb_3a gb_nc gb_e']/div/a")).click();
driver.findElement(By.xpath("//*[#id='gb_71']")).click();
driver.close();
}
}
Issue is Firefox didnt run. I use Firefox 52.0.1 and Selenium 3.8.1 Anyone can check why for me !
After click run as java application:
I'm using Firefox 45.8.0 version and I tried below code to open Firefox browser but i'm getting error that: "The path to the driver executable must be set by the webdriver.gecko.driver system property".
Please sagest me how to set the path.
Note:gecko driver will work for above firefox version 48.
package First;
import org.openqa.selenium.firefox.FirefoxDriver;
public class City {
public static void main(String[] args) {
// TODO Auto-generated method stub
FirefoxDriver c1=new FirefoxDriver();
c1.get("http://google.com");
}
}
Try this below code.
In your code you were not provide gecko driver path. You can download gecko driver from this link
public class City {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Drivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);
driver.get("http://google.com");
}
}