how to use testng annotation @BeforeTest? @BeforeTest (TestNG)
@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run.
@AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run.
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite1"> <test name="testing Test_Base"> <classes> <class name="testWordpress.Test_Base" ></class> </classes> </test> <test name="testing post"> <classes> <class name="testWordpress.Test_post" ></class> </classes> </test> <test name="testing Tag"> <classes> <class name="testWordpress.Test_tag" ></class> <class name="testWordpress.Test_Category" ></class> </classes> </test> </suite>
package testWordpress; import org.testng.annotations.AfterSuite; import org.testng.annotations.BeforeSuite; public class Test_Base { @BeforeSuite public void LogintoWordPress(){ System.out.println("Login to wordpress"); System.out.println(""); } @AfterSuite public void LogoutfromWordPress(){ System.out.println(""); System.out.println("Logout from wordpress"); } }
package testWordpress; import org.testng.annotations.AfterClass; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class Test_Category { @BeforeClass public void NavigateToCategoryPage(){ System.out.println(""); System.out.println("Click on Category page of wordpress-------------"); System.out.println(""); } @AfterClass public void writeStatusToExcel(){ System.out.println(""); System.out.println("Write Pass/Fail/Skip status to Excel / HTML report"); } @BeforeMethod public void VerifyExecutionStatus(){ System.out.println(""); System.out.println("Verify testcase execution status from excel"); ; } @AfterMethod public void WriteStatusToExcel(){ System.out.println("Write Testcase Pass/Fail/Skip status to Excel / HTML report "); System.out.println(""); } @BeforeTest public void defineVariables(){ System.out.println("-----------------------"); System.out.println("@BeforeTest testing"); } @Test public void AddnewCategory(){ System.out.println(" Testing Add new Category"); } @Test public void EditCategory(){ System.out.println(" Testing Edit Category"); } @Test public void ViewCategory(){ System.out.println(" Testing View Category"); } @Test public void DeleteCategory(){ System.out.println(" Testing Delete Category"); } @Test public void QuickEditCategory(){ System.out.println(" Testing Quick Edit Category"); } }
package testWordpress; import org.testng.annotations.AfterClass; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; public class Test_post { @BeforeClass public void NavigateToPostPage(){ System.out.println(""); System.out.println("Click on post page of wordpress-------------"); System.out.println(""); } @AfterClass public void writeStatusToExcel(){ System.out.println(""); System.out.println("Write Pass/Fail/Skip status to Excel / HTML report"); } @BeforeMethod public void VerifyExecutionStatus(){ System.out.println(""); System.out.println("Verify testcase execution status from excel"); } @AfterMethod public void WriteStatusToExcel(){ System.out.println("Write Testcase Pass/Fail/Skip status to Excel / HTML report "); System.out.println(""); } @Test public void AddNewPost(){ System.out.println(" Testing Add New post"); } @Test(dependsOnMethods = { "AddNewPost" }) public void EditPost(){ System.out.println("Edit post Testing"); } @Test(dependsOnMethods = { "EditPost" }) public void ViewPost(){ System.out.println("View post testing"); } @Test(dependsOnMethods = { "ViewPost" }) public void QuickEditPost(){ System.out.println("Quick Edit post testing"); } @Test(dependsOnMethods = { "QuickEditPost" }) public void TrashPost(){ System.out.println("Trash post testing"); } }
package testWordpress; import org.testng.SkipException; import org.testng.annotations.AfterClass; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; public class Test_tag { @BeforeClass public void NavigateTotagPage(){ System.out.println(""); System.out.println("Click on tag page of wordpress-------------"); System.out.println(""); } @AfterClass public void writeStatusToExcel(){ System.out.println(""); System.out.println("Write Pass/Fail/Skip status to Excel / HTML report"); } @BeforeMethod public void VerifyExecutionStatus(){ System.out.println(""); System.out.println("Verify testcase execution status from excel"); } @AfterMethod public void WriteStatusToExcel(){ System.out.println("Write Testcase Pass/Fail/Skip status to Excel / HTML report "); System.out.println(""); } @Test public void AddnewTag(){ System.out.println(" Testing Add new Tag"); } @Test public void EditTag(){ System.out.println(" Testing Edit Tag"); } @Test public void ViewTag(){ System.out.println(" Testing View Tag"); throw new SkipException("Skipping - This is not ready for testing "); } @Test public void DeleteTag(){ System.out.println(" Testing Delete Tag"); } }
How to download and configure apache ant?
- Go to google and type download Apache ant
- Select link http://ant.apache.org/bindownload.cgi
- Download file named like apache-ant-1.8.4-bin.zip
- Extract the zip file into a directory structure of your choice. Set the “ANT_HOME” environment variable to this location and include the “ANT_HOME/bin” directory in your path.
- Make sure that also the JAVA_HOME environment variable is set to the JDK. This is required for running Ant.
- Check your installation by opening a command line and typing “ant -version” into the commend line. The system should find the command ant and show the version number of your installed ant.
What is apache ant?
Apache Ant is a software tool for automating software build processes. It is similar to Make but is implemented using the Java language, requires the Java platform, and is best suited to building Java projects.
Apache Ant (Another Neat Tool) is a general purpose build tool. A build tool can be used to automate certain repetitive tasks, e.g. compiling source code, running software tests and creating jar files and Javadoc.
Apache Ant is used primarily used for building and deploying Java projects.
A Java build process typically includes:
the compilation of the Java source code into Java bytecode
creation of the .jar file for the distribution of the code
creation of the Javadoc documentation
Apache Ant uses a xml file for its configuration. This file is usually called “build.xml”. Ant builds are based on three blocks: tasks, targets and extension points.
A task in Ant is a unit of work which should be performed. Tasks are small, atomic steps, for example compile source code or create Javadoc. Tasks can be grouped into targets.
A target is a group of tasks which can be directly invoked via Ant. Tasks can also specify their dependencies. If a target depends on other targets then Ant will automatically perform these task first and so on and so on.
For example if target A depends on target B, Ant will first perform target B and then target A.
In your build.xml you can also specify the default target. Apache Ant will execute this target, if no explicit target is specified.
what is the difference between assert and verify in selenium ?
When an “assert” fails, the test will be aborted.
Where if a “verify” fails, the test will continue executing and logging the failure.
So when Assertion fails all test steps after that line of code are skipped 😦
To resolve this we write this command in try catch block 🙂
Example 1 :
package testWordpress; import org.testng.Assert; import org.testng.annotations.Test; public class AssertionsTest { @Test public void testMultiply() { System.out.println("Before Error "); Assert.assertEquals(21, multiply(10, 5)); System.out.println("After Error "); } public int multiply(int x, int y) { return x / y; } }
System.out.println(“After Error “); will never executed. 😦
Please check eclipse console to conform.
We can resolve this problem by using Try-Catch block.
How?
Lets check out following code
package testWordpress; import org.testng.Assert; import org.testng.annotations.Test; public class AssertionsTest { @Test public void testMultiply() { System.out.println("Before Error "); try{ Assert.assertEquals(21, multiply(10, 5)); }catch(Throwable t){ // recovered // java code to fail the test System.out.println("After Error "); } } public int multiply(int x, int y) { return x / y; } }
how to use assertion in testng ? – Assert (TestNG)
Refer Assert (TestNG)
Sample Method Detail
assertTrue
assertFalse
assertEquals
Example 1 : what happen when assertion fail using assertEquals ?
package testWordpress; import org.testng.Assert; import org.testng.annotations.Test; public class AssertionsTest { @Test public void testMultiply() { Assert.assertEquals(50, multiply(10, 5)); } public int multiply(int x, int y) { return x / y; } }
Run above code using testNG and check TestNG result pane
Example 2 : what happen when assertion success using assertEquals ?
package testWordpress; import org.testng.Assert; import org.testng.annotations.Test; public class AssertionsTest { @Test public void testMultiply() { Assert.assertEquals(2, multiply(10, 5)); } public int multiply(int x, int y) { return x / y; } }
Example 3: what happen when assertion success using assertTrue?
package testWordpress; import org.testng.Assert; import org.testng.annotations.Test; public class AssertionsTest { package testWordpress; import org.testng.Assert; import org.testng.annotations.Test; public class AssertionsTest { @Test public void testMultiply() { Assert.assertTrue(multiply(10, 5)!=20, "Sorry"); } public int multiply(int x, int y) { return x / y; } }
Example 4: what happen when assertion fail using assertTrue?
package testWordpress; import org.testng.Assert; import org.testng.annotations.Test; public class AssertionsTest { @Test public void testMultiply() { Assert.assertTrue(multiply(10, 5)!=2, "Sorry"); } public int multiply(int x, int y) { return x / y; } }
Example 4: what happen when assertion success using assertFalse?
package testWordpress; import org.testng.Assert; import org.testng.annotations.Test; public class AssertionsTest { @Test public void testMultiply() { Assert.assertFalse(multiply(10, 5)>10, "Caught"); } public int multiply(int x, int y) { return x / y; } }
Lets add TestCase with assertion in testng.xml and verify testNG html report
<test name="test4"> <classes> <class name="testWordpress.AssertionsTest" ></class> </classes> </test>
how to parameterized test in TestNG using @dataprovider annotation ?
mapping of complex parameter types to a test method with @DataProvider annotation
See example:
public class ParameterizedLoginTest { @Test(dataProvider = "parameterIntTestProvider") public void parameterIntTest(String UserName, String Password) { System.out.println("UserName " + UserName); System.out.println("Password " + Password); } //This function will provide the parameter data // Selenium framework - read the data from xls file and put it in Object array // rows - number of time test has to be repeated // cols - number of parameters in test data @DataProvider(name = "Data-Provider-Function") public Object[][] parameterIntTestProvider() { Object[][] data = new Object[3][2]; data[0][0] = "UserName1"; data[0][1] = "UserName1@123"; data[1][0] = "UserName2"; data[1][1] = "UserName2@123"; data[2][0] = "UserName2"; data[2][1] = "UserName2@123"; return data; } }
Run this and Checkout TestNG result pane
Now Add new tag in testng.xml
<test name="test3"> <classes> <class name="testWordpress.ParameterizedLoginTest" ></class> </classes> </test>
And run TestNG.xml and see testng HTML report
You must be logged in to post a comment.