TestNG annotation Issue in Selenium JAVA

70 views
Skip to first unread message

Rahil Kumar

unread,
Apr 4, 2016, 3:47:28 AM4/4/16
to Selenium Users
I have written basic TESTNG java selenium script for learning. IN below code after execution of test1 method I am expecting closing of browser as @AfterTest will run. But after Test1 method Test2 method is running . Browser is closing after execution of both @test method. Can anyone help me how to close browser after each run of @test method


import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;

public class Sample {

    public String baseUrl = "https://www.facebook.com/";
    public WebDriver driver = new FirefoxDriver();
    @BeforeTest
    public void beforeTest() 
      {
      driver.get(baseUrl);      
      }
    @Test
    public void test1() 
    {
      driver.get(baseUrl);
      String expectedTitle = "Facebook - Log In or Sign Up";
      String actualTitle = driver.getTitle();
      Assert.assertEquals(actualTitle, expectedTitle);
    }
    @Test
    public void test2() 
    {
      driver.get(baseUrl);
      String expectedTitle = "Facebook - Log In or Sign Up";
      String actualTitle = driver.getTitle();
      Assert.assertEquals(actualTitle, expectedTitle);
    }
  @AfterTest
  public void afterTest()
  {
  driver.close();
  }

}

⇜Krishnan Mahadevan⇝

unread,
Apr 4, 2016, 4:05:04 AM4/4/16
to Selenium Users
Rahil,

There are some basic problems here. 

@BeforeTest/@AfterTest is designed to run only once per <test> [ a <test> can have one ore more classes, wherein each class can have one or more @Test annotated test methods ]. So you cannot use that annotation for your use case.

You can try using @BeforeMethod/@AfterMethod, which is guaranteed to run before executing a @Test annotated test method, but then again, your Test class has more than 1 @Test annotated test methods and your WebDriver reference is global. So when your @Test methods run in parallel, your @AfterMethod may end up triggering the quit() method on the wrong test method.

I would suggest that you please take a look at this blog post that I created sometime back and try leveraging something on the similar lines




Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/c3cf2397-ec3d-4d74-837d-cec16d345676%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

padhi.su...@gmail.com

unread,
Apr 4, 2016, 4:13:46 AM4/4/16
to Selenium Users

Hi,

First of all ,why have you written same logic in both the methods test1() and test2().
Always the code under @Test annotation would be executed and then @AfterTest.


Thanks

Reena Jain

unread,
Apr 4, 2016, 4:13:46 AM4/4/16
to seleniu...@googlegroups.com
thank you krishan

On Mon, Apr 4, 2016 at 1:35 PM, Reena Jain <reena.j...@gmail.com> wrote:
  "@AfterTest" will execute after each '@test' execution.u can make  "@AfterTest" as '@AfterSuite'
   

On Mon, Apr 4, 2016 at 1:17 PM, Rahil Kumar <info....@gmail.com> wrote:

--

Reena Jain

unread,
Apr 4, 2016, 4:13:46 AM4/4/16
to seleniu...@googlegroups.com
  "@AfterTest" will execute after each '@test' execution.u can make  "@AfterTest" as '@AfterSuite'
   
On Mon, Apr 4, 2016 at 1:17 PM, Rahil Kumar <info....@gmail.com> wrote:

--

Rahil Kumar

unread,
Apr 4, 2016, 6:09:53 AM4/4/16
to seleniu...@googlegroups.com
hi , I have replaced AfterTest and BeforeTest with AfterMethod and beforemethod respectively , In in this case only test1 () Passed , other test2() and aftertest() failed

⇜Krishnan Mahadevan⇝

unread,
Apr 4, 2016, 6:17:20 AM4/4/16
to Selenium Users
Rahil,

I believe I already explained as to when this can happen. I would suggest that you please go through the blog link that I shared.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

On Mon, Apr 4, 2016 at 3:39 PM, Rahil Kumar <info....@gmail.com> wrote:
hi , I have replaced AfterTest and BeforeTest with AfterMethod and beforemethod respectively , In in this case only test1 () Passed , other test2() and aftertest() failed

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages