Re: Webdriver is not closing the Browser

1,134 views
Skip to first unread message

darrell

unread,
Nov 25, 2012, 3:14:27 PM11/25/12
to webd...@googlegroups.com
Do you work for Google? Are you testing Gmail? If the answer to this is no then I'd recommend you quit using WebDriver with Gmail. The Gmail application is relatively complex compared to most things people test with WebDriver. If you need an email account and a way to check it as part of testing your application then Gmail is one of the last things you should use. Setup up a simple SMTP email server. If you are behind a firewall you don't even need security. You can then use something like javamail to access the email server and check for mail.

In general, avoid complex applications like Gmail when there are more simple applications available and more importantly, avoid GUI when there are API calls you can do inside. Just google "setting up SMTP mail server for testing" and you'll probably find something.

Darrell

On Friday, 23 November 2012 02:17:39 UTC-5, Ram krishna wrote:
Hi Guys,

I am trying to close the firefox browser after the gmail completely sign out for that i am checking whether signin button is displayed or not
If signin button displayed then driver should close the browser

Please find below the code which i have written in after the test block

@After
    public void tearDown() throws Exception {
        System.out.println("Entered in to the After the Test Block...");
        while (driver.findElement(By.xpath("//input[@id='signIn']")).isDisplayed())
        {
            System.out.println("Login form is displayed and closing the browser");
            driver.quit();
        }
        System.out.println(" Close the Browser Manually and clean the %Temp% folder ");
       
    }

Any help in debugging why it is not closing the browser and how to close the browser ? would be grateful


Regards
Ramakrishna
9704620674

A B

unread,
Nov 26, 2012, 5:47:14 AM11/26/12
to webd...@googlegroups.com
Try following code -
   driver.close();
   driver.quit();

Cheers,
A B
Blog - www.automation.lifegoeasy.com

Ram krishna

unread,
Nov 26, 2012, 7:56:51 AM11/26/12
to webd...@googlegroups.com
Hi Darrel,

Thank you so much for your reply

I am not working for google. Just testing the Gmail using Webdriver [for exploring webdriver and its methods etc.]

Actually my question is that when i use the same code with in the @Test block it is working fine. Why it is not working if i use it in @After block ?

code used is :


System.out.println("Entered in to the After the Test Block...");
        while (driver.findElement(By.xpath("//input[@id='signIn']")).isDisplayed())
        {
            System.out.println("Login form is displayed and closing the browser");
            driver.quit();
        }
        System.out.println(" Close the Browser Manually and clean the %Temp% folder ");

I would be grateful to you if at all you can help me to know the reasons behind this... Thank you in advance

Note: Thank you for your suggestion that letting me know that GMAIL is complex application to learn webdriver.

Regards,
Ramakrishna
9704620674

Ram krishna

unread,
Nov 26, 2012, 7:58:49 AM11/26/12
to webd...@googlegroups.com
Hi A B,

Thank you for your reply.I have tried using quit  and close methods in @After block it was not working...

darrell

unread,
Nov 27, 2012, 5:30:46 PM11/27/12
to webd...@googlegroups.com
The reason I ask if you are working at Google is because it sometimes requires inside knowledge to understand why something isn't working. If you were at Google I'd recommend getting in touch with the Gmail team and see if they have experienced similar issues. Without that inside knowledge it can get very difficult to understand what conflicts might be happening.

Selenium is javascript running on the browser. If the site you are testing is javascript intensive (like Gmail or Facebook) then it can be quite challenging trying to learn Selenium. It could be a situation of doing something which works 99% of the time but isn't 100% correct will definitely fail on a site like Gmail.

It could be a timing issue as well. The timing of you code will change as you move things around. Taking something which works in the Test annotation and moving it to the After annotation will definitely change the timing.

However, looking at the code you just posted you code has no delays and will try to close the browser again and again. This could be a source of the problem. Adding a simple Thread.sleep(1000) after the driver.quit() might solve the problem. If it does work then you have a piece of information you didn't before. I will occasionally add sleep statements to code to see if a change in timing helps or makes things worse. If they are the same then I suspect it is not a timing issue.

Essentially, change one thing, run the test. Change it back, change a second thing, run the test. Keep changing one thing at a time add see if there is something which consistently makes it better or worse. From that develop a theory then try to prove the theory right or wrong. Things to change aren't just code. Changing web browser, changing from FirefoxDriver to Firefox using RemoteWebDriver, etc.. The change from Test to After is significant. What if you only have one test? What if you put everything in After and have an empty Test?

Personally, I have never tested Gmail. So I've never had to deduce why something would not work. I find testing the applications I am paid to test usually helps me at learning a test tool.

If you want to post a complete code snippet which works and a second one which does not. We might be able to see something which helps determine the problem.

Darrell

Ram krishna

unread,
Nov 28, 2012, 5:38:35 AM11/28/12
to webd...@googlegroups.com
Hi Darrell,

First of all Thank you so much for taking time to write lengthy email. I have gone through your email. I understand how to debug/learn the things. 

Just for having a glance at my code i am pasting it below here:

package testing;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class GmailLogin {
private WebDriver driver;
private WebDriverWait wait;
@Before
public void setUp() throws Exception {
System.out.println("Entered the Before Class...!!!!");
driver = new FirefoxDriver();
driver.get("http://www.gmail.com");
wait =new WebDriverWait(driver,20);
driver.manage().window().maximize();
}
@Test
public void testGmailLogin()throws Exception{
      System.out.println("Entered in to the Main Testing Block");
      driver.findElement(By.id("Email")).clear();
      driver.findElement(By.id("Email")).sendKeys("tramak...@prokarmasoftech.com");
      driver.findElement(By.id("Passwd")).clear();
      driver.findElement(By.id("Passwd")).sendKeys("windows@123");
      driver.findElement(By.id("signIn")).click();
      System.out.println("Waiting for page to load fully...");
      wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//span[contains(text(),'tramak...@prokarmasoftech.com')]")));
      driver.findElement(By.xpath("//span[contains(text(),'tramak...@prokarmasoftech.com')]")).click();
      int Checkboxescount = driver.findElements(By.cssSelector("div.T-Jo-auh")).size();
      System.out.println("No.of Checkboxes in the mail:"+ --Checkboxescount);
        
      
      boolean countflag = driver.findElement(By.xpath("//span[@id=':pn']/parent::*/parent::*/parent::*/parent::*/div/div[1]/span/div[1]/span/b[3]")).isDisplayed();
     
      // prints the no.of unread messages if exists in inbox
      if(countflag==true){
      System.out.print("No.of Unread Messages in Gmail:");
      System.out.println(driver.findElement(By.xpath("//span[@id=':pn']/parent::*/parent::*/parent::*/parent::*/div/div[1]/span/div[1]/span/b[3]")).getText());
      }else{
     System.out.println("No Unread Messages in your INBOX");
         }
      
      if(driver.findElement(By.id("gb_71")).isDisplayed())
      {
     driver.findElement(By.id("gb_71")).click();
      }else { System.out.println("sign out button is not displayed to click");}
      
      wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@id='signIn']")));
      
      boolean Flag = driver.findElement(By.xpath("//input[@id='signIn']")).isDisplayed();
      //System.out.println(Flag);
      while(Flag)
{
System.out.println("Login form is displayed and browser closed");
driver.quit();
Flag = false;
}
}
@After
public void tearDown() throws Exception {
//System.out.println("Entered After the Test Block...!!!!");
//System.out.println(" Close the Browser Manually and clean the %Temp% folder ");
   
}

}

Regards
Ramakrishna

Smita Sinha

unread,
Dec 4, 2012, 2:06:56 AM12/4/12
to webd...@googlegroups.com
Try 
@AfterSuite(alwaysRun = true)
 public void setupAfterSuite() {
 driver.close();
 }

This is working for me.

Smita

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/cV_Le_AgFy0J.

To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.

Ram krishna

unread,
Dec 4, 2012, 8:18:02 AM12/4/12
to webd...@googlegroups.com
Hi Smita Sinha,

Thank you for your reply. I got the solution and working fine my code

Ramakrishna
      driver.findElement(By.id("Email")).sendKeys("tramakrishna...@prokarmasoftech.com");
      driver.findElement(By.id("Passwd")).clear();
      driver.findElement(By.id("Passwd")).sendKeys("windows@123");
      driver.findElement(By.id("signIn")).click();
      System.out.println("Waiting for page to load fully...");
      wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//span[contains(text(),'tramak...@prokarmasoftech.com')]")));
      driver.findElement(By.xpath("//span[contains(text(),'tramakris...@prokarmasoftech.com')]")).click();
Reply all
Reply to author
Forward
0 new messages