driver.close() method is not working in Selenium WebDriver on Firefox - selenium

I am writing a simple program in Eclipse using JUnit annotation.
diver.close() is not closing my Firefox browser after the tests. It works fine with Chrome. Code snippet is here.
public class FireFox1 {
WebDriver driver;
#Before
public void setUp() {
driver= new FirefoxDriver();
driver.get("http://book.theautomatedtester.co.uk/chapter4");
}
#After
public void tearDown() {
driver.close();
}
#Test
public void testExamples() {
WebElement element= driver.findElement(By.id("nextBid"));
element.sendKeys("100");
}
}

sometimes while on repeated usages,we'll facing problems with driver.close().
Anyways driver.quit() will rectify your problem.
Generally driver.close() closes the browser(the instance of driver is still intact) and driver.quit() is to kill the webdriver instance. As anyhow you are using here for only one page,then you can go with driver.quit().
Thank you.

Better use driver.quit() method. It closes the browser, but due to some unknown issues it throws NullPointerException. Just catch it..
try{
driver.quit();
}catch (Exception e){
System.out.println("Nothing to do with it");
}

Assuming you have started 5 browsers(classes) parallely using grid:
driver.close - Used for close the current browser( where execution going on)
driver.quit - Used for close all the browsers started by the current execution.
You can use any one of this..
May be browser compatiblity issue, try to downgrade the FF let we see...

Use latest GeckoDriver.exe (17) with Latest FireFox (54.0);
It works fine for me. I had the same problem before.
This problem that you are facing is completely a compatibility problem between driver & Browser version.
driver.close(); should have work without problem if you use above versions. Let me know if it works.

This is what the problem in Firefox driver.close() works in Firefox only with internet connection but in case of Chrome it works without internet connection.

Related

IE browser gets shrunk during execution

I am facing an weird issue. I have a selenium suite to execute on multiple browsers. It works fine of chrome and Firefox. But in case of IE, the web page under test gets shrunk (web page resize) making elements hidden. Hence facing NoSuchElementException.
I have already tried executing on full screen. No help.
Please help in solving this issue.
Thanks,
Praveen
Don't know the exact reason for that, however, suggested workaround would be to implement pre-execution method for your test which would take care of all your browser different configurations. For example, if you are using Java with Selenium and JUnit5, it might be:
#BeforeEach
void beforeTestExecution() {
DesiredCapabilities desiredCapabilities = DesiredCapabilities.internetExplorer();
desiredCapabilities.setCapability(
CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);
driver = new InternetExplorerDriver(desiredCapabilities);
// Navigate to URL
driver.get("http://www.yoursite.com");
// Maximize your website window before test.
driver.manage().window().maximize();
}
Or:
#BeforeEach
void configBrowser() {
DesiredCapabilities desiredCapabilities = DesiredCapabilities.internetExplorer();
desiredCapabilities.setCapability(
CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);
driver = new InternetExplorerDriver(desiredCapabilities);
// Start Internet Explorer maximized.
driver.manage().window().maximize();
}

Cross browser test

I have to do login to an application with two different browsers (IE & FF) and hence I tried to do cross browser test. When the URL gets passed to IE, I am getting link as "Continue to this website not recommended", whereas the same link will not be displayed in FF. In the below if I use this statement driver.findElement(By.id("overridelink")).click(); then it works fine in IE but failing in firefox since there wont be any link in FF.
please let me know the possible where one script should run in both the browsers. Below is the script which am trying
#Test
#Parameters("browser")
public void verifypagetitle(String browsername) {
if(browsername.equalsIgnoreCase("IE"))
{
System.setProperty("webdriver.ie.driver",
"D:\\2.53.1\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
}
else if (browsername.equalsIgnoreCase("firefox"))
{
System.setProperty("webdriver.firefox.bin",
"C:\\Program Files\\Mozilla Firefox\\firefox.exe");
driver = new FirefoxDriver();
}
driver.get("URL");
driver.manage().window().maximize();
driver.findElement(By.id("overridelink")).click();
driver.findElement(By.id("j_username")).sendKeys("userid");
driver.findElement(By.id("j_password")).sendKeys("pwd");
driver.findElement(By.id("j_password")).submit();
This is most likely caused by the IE security level. In my experience if I change it to Low, the warning is not present. You can try and see if this will solve the issue in your case.
Another solution is to add conditional logic to check the browser being used. Something like this:
Capabilities cap = ((RemoteWebDriver) browserDriver).getCapabilities();
String browsername = cap.getBrowserName();
// This block to find out if it is IE
if ("internet explorer".equalsIgnoreCase(browsername)) {
driver.findElement(By.id("overridelink")).click();
}

Why I cannot use Selenium Chrome Driver

I tried to use ChromeDrive but getting error or not excuted well.
My simple java code for testing was:
#Test
public void testGoogleSearch() {
System.setProperty("webdriver.chrome.driver", "lib\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com/xhtml");
Thread.sleep(5000); // Let the user actually see something!
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("ChromeDriver");
searchBox.submit();
Thread.sleep(5000); // Let the user actually see something!
driver.quit();
}
Following this tutorial:
https://sites.google.com/a/chromium.org/chromedriver/getting-started
SOLVED
I need to update the ChromeDrive and replace the previous one.
Under my project directory /lib
Download here: https://sites.google.com/a/chromium.org/chromedriver/downloads
yes, i suggest you can mention like this C:\\Users\\mona\\Downloads\\chromedriver_win32\\chromedriver.exe

Script runs fine in debug mode but not runs in run mode in IE Browser

i have tried following code both in Firefox and IE.
Firefox it is working fine but in IE it is not working . Im using IEv11 ,selenium v2.45 and also i tried with v2.46 and 2.44 .IE Driver Server v2.46. Please help me
public class IEBrowser {
WebDriver driver;
#Test
public void url(){
System.setProperty("webdriver.ie.driver", "E:\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
driver.get("http://www.toolsqa.com/automation-practice-form/");
driver.findElement(By.name("firstname")).sendKeys("hi");
}
}
Use this code before finding the elements:
try {
Thread.sleep(10000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
This is applicable to any appium project. *Works in Android Appium too.
It's working fine in my System for all browsers.This may be due to internet speed as well.Any how add a WebDriverWait and see
driver.get("http://www.toolsqa.com/automation-practice-form/");
WebDriverWait wait = new WebDriverWait(driver, 40);//Unit in Seconds
wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("firstname"))).sendKeys("hi");

Selenium Force Close Firefox

ProfilesIni profile = new ProfilesIni();
FirefoxProfile ff = profile.getProfile("ScreenCapture");
WebDriver driver = new FirefoxDriver(ff);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.get(url);
Thread.sleep(8000);
File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
driver.quit();
Shouldn't driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); force a close of the selenium generated Firefox browser after 15 seconds? The browser just sits and says its transferring data for an hour+. Basically just hangs saying its transferring...
I am capturing ajax heavy pages which is why Im asking everything to wait for 8 seconds after page loads. But that should have nothing to do with the driver forcing a close after 15 seconds.
Where am I going wrong?
Details: Centos x64 6.4 with Firefox 10.0.12 and latest Selenium as of 10 min ago.
Is there something I can do in Java to go around this?
Question: How can I force close the Selenium generated Firefox window after N seconds?
If you use Junit along with Java, then some thing like this :-
#After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
Note :-
To get a full skeleton of how it should be written just download the selenium IDE for FF and export some test case to Java /jUnit.
My linux knowledge is limited, but you can kill a process by running the linux command pkill.
driver.quit();
Thread.sleep(15000); //use a poll loop instead to check process running every 1 sec
Runtime rt = Runtime.getRuntime();
rt.exec("pkill firefox");
I think that the java process will need to have enough permissions to kill a process, but haven't tried it.
To follow up on Ardesco's comment, an example would look as follows:
ProfilesIni profile = new ProfilesIni();
FirefoxProfile ff = profile.getProfile("ScreenCapture");
WebDriver driver = new FirefoxDriver(ff);
driver.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS);
try {
driver.get(url);
} catch (TimeoutException e) {
System.out.println("15 seconds were over, force continue!");
} finally {
File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
driver.quit();
}
The try part will run the request but when the timeout time set with pageLoadTimeout has been exceeded an exception is thrown which we catch. The finally part will be run regardless if the requested page was loaded properly in less than 15 seconds or whether an exception was thrown.
Implicit waits will not force a close of a browser after 15 seconds.
Implicit waits are used when trying to find elements in the DOM, they are not used when trying to load a page. If you want Selenium to stop trying to load a page after 15 seconds you will need to set a pageLoadTimeout, it's used like this:
driver.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS);
The default page load timeout is 0 (which means wait indefinitely), hence the behaviour that you are seeing.
(There is obviously an assumption here that the driver binary you are using has implemented this method.)
The JavaDoc for timeouts in Selenium is available Here