I want to know how run multiple files without running the before class() again and again.
E.g.: Consider i am writing test case for GMail
login.java
: Which contains complete testing of the login pageinbox.java
: Which contains complete testing of the inboxspam.java
: Which contains complete testing of the spam folder pagelabel.java
: Which contains complete testing of create/edit/deleting of labels
Now every file contains a before class which has
- calling the browser
- input username and password
- clicking the submit button and after class which logs out
Using TestNG when I run, every time it will login, test for the particular file and logs out
It needs to run like this: It should login, then test the inbox, spam and label then logs out.
Is it the right way to execute? Or is there a solution for this?
4 Answers 4
You may try this-
public class GmailContentTesting
{
public WebDriver driver;
public String baseUrl ="http://www.gmail.com";
@BeforeClass
public void setup()
{
/*System.setProperty("webdriver.chrome.driver", "Browsers/chromedriver.exe");
driver = new ChromeDriver();*/
driver =new FirefoxDriver();
driver.get(baseUrl);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@BeforeTest
public void gmailLogin()
{
WebElement EmailTextbox= driver.findElement(By.xpath("//input[contains(@id,'Email')]"));
EmailTextbox.clear();
EmailTextbox.sendKeys("your email");
WebElement nextButton =driver.findElement(By.xpath("//input[contains(@id,'next')]"));
nextButton.click();
WebElement PasswdTextbox =driver.findElement(By.xpath("//input[contains(@id,'Passwd')]"));
PasswdTextbox.clear();
PasswdTextbox.sendKeys("Your Passwd");
WebElement SigninButton =driver.findElement(By.xpath("//input[contains(@id,'signIn')]"));
SigninButton.click();
}
@Test(priority=1)
public void inboxTesting()
{
Write your code for complete testing of the inbox
}
@Test(priority=2)
public void spamTesting()
{
Write your code for complete testing of the spam folder page
}
@Test(priority=3)
public void lableTesting()
{
Write your code for complete testing of create/edit/deleting of labels
}
@AfterTest
public void Logout()
{
Write your code for Gmail Logout.
}
-
Could you please explain how your code prevents the BeforeTest and AfterTest routines from running before and after each Test routine?Kate Paulk– Kate Paulk2016年03月28日 11:24:00 +00:00Commented Mar 28, 2016 at 11:24
You can try using @BeforeSuite
You have to group your test classes into a suite in the testng.xml and annotate the setup method in one of your test classes.
This link provides a nice tutorial: http://examples.javacodegeeks.com/enterprise-java/testng/testng-beforesuite-example/
You can do it as:
- Have a static driver inside the test class; static WebDriver driver = null;
- Create a single
@BeforeClass
, which will initialize the driver to your desired browser, navigate to gmail and login. - Have
@Test
annotations for all the 4 cases. - Remove/comment out all other tags from those individual classes. Just have them as methods. Then call those methods from inside your
@Test
methods. - Finally have a
@AfterClass
to logout.
You can use something like this in your script class ;
public class gmailTest extends BaseTest
{
@Test
public void loginLogoutTest()
{
inboxTest();
spamTest();
}
public void inboxTest()
{
log.info("Executing inboxTest");
//Your code here
log.info("loginToApplication has been Successfully Excecuted");
}
public void spamTest()
{
log.info("Executing spamTest");
//Your code here
log.info("spamTest has been Successfully Excecuted");
}
}
- Now call it where ever required
- Here we have only one @test so your gmail login will login one time from @beforetest and logout once from @aftertest.
Hope it help and revert if still more doubts.
-
i am using it in my script and working as expected so can try ,it will work and will reduce the code as we can reuse the codes hereRANA DINESH– RANA DINESH2018年08月10日 06:18:44 +00:00Commented Aug 10, 2018 at 6:18
Explore related questions
See similar questions with these tags.