Java program is not terminating when using Selenium Webdriver - java

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

Related

java.lang.NullPointerException when using webdriver manager selenium java

While using selenium with java, WebdriverManager is not running and the below code is giving null pointer exception. I have returned the driver at end of class.
I have one ask whether should I keep the Webdriver driver as static or not.
import io.github.bonigarcia.wdm.WebDriverManager;
public class Browserselector {
public WebDriver driver;
public static Properties prop;
public WebDriver initializeDriver() throws IOException {
{
String browserName = "firefox";
System.out.println(browserName);
if (browserName.contains("Chrome")) {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
} else if (browserName.contains("IE")) {
WebDriverManager.iedriver().setup();
driver = new InternetExplorerDriver();
} else if (browserName.contains("FireFox")) {
WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();
} else if (browserName.contains("EDGE")) {
WebDriverManager.edgedriver().setup();
driver = new EdgeDriver();
}
}
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("google.com");
return driver;
}
}
Thanks for your help in advance.
you are trying to start "firefox" - but the if condition checks for "Firefox", if you want to use it like that change the following condition
browserName.contains("FireFox")
into
browserName.equalsIgnoreCase("FireFox")
I recommend you to change the nested if with a "switch" it's more readable and easy to follow/understand
Also, don't use a URL without specifying the protocol
driver.get("https://www.google.com");

What is my program in Selenium not stopping executing?

I built my Selenium test cases in Maven, and the following in my main method:
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "webdriver/chromedriver");
WebDriver driver=new ChromeDriver();
driver.get("https://localhost:4502");
driver.quit();
}
My chromedriver was downloaded and moved to a folder called "webdriver" in the project. Once the program is lauched, the chrome browser is opened and then closed.
However, even after the chrome browser is closed, and "driver.quit()" is executed, why is the not exit and terminates its execution?
public static void main(String[] args) {
String folder_path = System.getProperty("user.dir");
System.out.println(folder_path);
System.setProperty("webdriver.chrome.driver",
folder_path+"\\driver\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://www.google.com");
driver.quit();
}
In your example above you should get compile time exception instead of running session.
I still don't know why it is happening. But, I can use System.exit(1) to exit the program.

Web don't run immediately when using WebDriver driver.get(""")

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:

Run exe file using selenium webdriver

How can I run exe file using selenium webdriver. If i can run the exe file then i can automate the windows window using auto it tool and can run those exe using java selenium. it would help in browsing the file in Selenium
Yes, you can do that.
Runtime.getRuntime().exec("path to the autoIt exe file");
Ex:
Runtime.getRuntime().exec("E:\\Softwares\\Testing\\FileIUploadAutoit.exe");
Here is the small example of uploading a file to website using Selenium WebDriver java using TestNG
public class autoitclass {
public WebDriver driver;
#BeforeTest
public void websitemain()
{
System.setProperty("webdriver.gecko.driver", "E:\\Softwares\\Testing\\geckodriver.exe");
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
String URL = "http://www.megafileupload.com/";
driver.get(URL);
}
#Test
public void uploadFile() throws Throwable{
driver.findElement(By.xpath(".//a[contains(#class,'slider-btn')]")).click();
driver.findElement(By.xpath(".//*[#id='initialUploadSection']")).click();
Runtime.getRuntime().exec("E:\\Softwares\\Testing\\FileIUploadAutoit.exe");
}
#AfterTest
public void quit(){
driver.quit();
}
You cannot test standalone apps(desktop) using selenium(Some me please correct me if incorrect),exception being apps developed using Electron. When you compile and build an electron project you get an Exe which selenium can interact and test.
The following example demo how to interact :
public void TestSampleGooglePlayElectronApp()
{
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.BinaryLocation = #"C:\MySampleElectronApp\MyApp.exe";
DesiredCapabilities capability = new DesiredCapabilities();
capability.SetCapability(CapabilityType.BrowserName, "Chrome");
capability.SetCapability("chromeOptions", chromeOptions);
IWebDriver driver = new ChromeDriver(chromeOptions);
driver.FindElement(By.XPath("//paper-button[contains(text(),'Sign in')]")).Click();
}

Not able to run selenium script using phantomjs driver

I am new to phantomjs driver, I need to run my script in background using phantomjs headless driver.
Here is my code i am getting null-pointer exception.
currently am using selenium 2.32,testNG,phantomjs jar 1.0.3
public class PhantomjsDemo {
public WebDriver driver;
#BeforeMethod
public void setup(){
DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true);
caps.setCapability("takesScreenshot", true);
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,"C:\\phantomjs-1.9.2-windows\\phantomjs.exe");
WebDriver driver = new PhantomJSDriver(caps);
driver.get("www.google.com");
}
#Test
public void google(){
driver.findElement(By.xpath("//*[#id='gbqfba']")).getText();
driver.findElement(By.xpath("//*[#id='gbqfba']")).getSize().getHeight();
driver.findElement(By.xpath("//*[#id='gbqfba']")).getSize().getWidth();
driver.findElement(By.xpath("//*[#id='gbqfba']")).click();
}
#AfterMethod
public void close(){
driver.quit();
}
}
You are not initializing your Webdriver member variable in the setup() method, but a method variable:
WebDriver driver = new PhantomJSDriver(caps);
Change it to
this.driver = new PhantomJSDriver(caps);
and the NPE should go away.

Categories

Resources