Table of Contents:



Software Quality Assurance (QA) TrainingTESTNG FOR SELENIUM AUTOMATIONTable of Contents: TOC \o "1-3" \h \z \u TestNG IntroductionBenefits of TestNGInstall/Configure TestNG with EclipseTestNG AnnotationsCreate first TestNG script Batch Test in TestNGSKIP test in TestNGData-Driven / Parameterizing Tests TestNG Assertions / Reporting Errors TestNG “Error / Exception” HandlingRevision History:DescriptionVersionDateInitial version created1.04/13/16TestNG for Selenium TestNG Introduction:TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionality that make it more powerful and easier to use.It is an open source automated testing framework; where NG of TestNG means Next Generation. TestNG is similar to JUnit but it is much more powerful than JUnit but still it’s inspired by JUnit. It is designed to be better than JUnit, especially when testing integrated classes. TestNG eliminates most of the limitations of the older framework and gives the developer/automation engineer the ability to write more flexible and powerful tests with help of easy annotations, grouping, sequencing & parametrizing. Benefits of TestNG:There are number of benefits but from Selenium perspective, major advantages of TestNG are:It gives the ability to produce HTML Reports of executionAnnotations made testers life easy Test cases can be Grouped & Prioritized more easily Parallel testing is possible with the help of Selenium GridGenerates LogsData Driven / Parameterization test is possible (@DataProvider)Framework Architecture in Selenium:Selenium Script 1, XLS File (Test Data) Selenium Script 2, ---- nUnit/Junit/TestNG -----> HTML Test ReportsSelenium Script N LoggingIn above diagram, Junit or TestNG is the controller for everything. You can use Selenium with TestNG or Junit but most of the companies prefer TestNG since we get very good reports in TestNG and it’s easier to execute parallel test cases with TestNG and Grid. Install/Configure TestNG with Eclipse:Full setup need: Java, Eclipse, and TestNG.JAVA setup on Windows/Mac: Install Java/JDK SE Development Kit 8u191Accept license agreementDownload: Windows x64207.22 MB jdk-8u191-windows-x64.exeCopy the downloaded .zip file to relevant location (ex: C:/Selenium) and unzip it.Now setup environment variable with java/jdk paths: Setup the following environment variables in System and User variables> User variables:JAVA_HOMEC:\Program Files\Java\jdk1.8.0_191System variablesPathC:\Program Files\Java\jdk1.8.0_191\binRun the following commands on command prompt verify whether java/jdk setup done correctly: (you should not see any errors)java -versionjavac -versionEclipse setup on Windows OS:Install Eclipse: IDE for Java EE Developers 212 MB: Eclipse is an editor to develop java programs. Eclipse is the replacement for notepad or textpad.Check your laptop version (ex: Win7/8/10 32-bit or 64-bit by going to start—computer—right click properties)32-bit: : eclipse folder (right click on .zip folder, extract here), open the extracted folder, go to eclipse folder, and double click eclipse.exe or right click on eclipse.exe-> create shortcut-> move it to task bar on the bottom of the desktop.Now double click on eclipse from task bar.Note/Error: If you see error message "Java was started but returned exit code=13" then uninstall 64 bit eclipse and install 32 bit ver of eclipse OR following this video to fix this issue: Windows 10, though it is 64 bit, please install 32 bit if you have an issue with 64 bitEclipse setup on Mac OS:Go to this link Eclipse IDE for Java EE DevelopersFollow below video to setup Eclipse on Mac OS. new workspace and java project for TestNG in Eclipse:Open Eclipse and create new workspace TestNGWS (ex: C:\QA\TestNGWS)File->New-> Project -> Java -> Java ProjectProject Name = TestNGForSelenium, Click Next, Select Libraries tabClick FinishInstall TestNG in Eclipse:NEW: (just use this)Open Eclipse-> Help -> Eclipse Marketplace -> Search for TestNG (in Find textbox enter TestNG and hit enter key)… you should see like below with icon Install: click on Install iconOLD:Open Eclipse-> Help -> Install New Software -> Enter TestNG URL (see below) ex: OR OR in “Work with:” , click Add and enter Name as , Location as click OK, and select the checkbox TestNG -> Next -> Next -> I Accept checkbox -> Finish.For more details, please refer: eclipse 3.4 or later ver, and above enter: eclipse 3.3, and above enter: to verify whether TestNG installed successfully in eclipse?Eclipse -> Window -> Show View -> Other..-> Java -> You must see TestNG (that means TestNG installed successfully in eclipse as a plug-in)TestNG will control the test cases. First TestNG script / TestNG AnnotationsCreate a new java project “TestNGForSelenium” in eclipse. There is NO public static void… in TestNG instead we’ll use annotations:Create a new class “SampleTest” (right click src -> New -> Class) without public static void…….Now, create annotation functions within the above class:public class SampleTest {// create annotation functions@Testpublic void testApp(){ /* we will get an error. Position the cursor on red error icon, you will see an option "Add TestNG library" and select and double click. You will see TestNG library under "JRE System Library" folder on the left. In TestNG library, you will see testng.jar is presented.Again, move the mouse to @Test, click "Import 'test'(org.testng.annotations) or press cntrl+shift+O(not zero). You will see "import org.testng.annotations.Test;" in the top of the script. Error will be gone now. “Test” is in-built class in TestNG */System.out.println("Testing app");}@Test // testApp is our test case.public void testLogin(){ System.out.println("Login Test");}}// Your final TestNG script looks like below:import org.testng.annotations.Test;public class SampleTest {// create annotation functions@Test public void testApp(){ System.out.println("Testing app");}@Test // testApp is our test case.public void testLogin(){ System.out.println("Login Test");}}//Run the script now (Right click the script (ex: SampleTest.java) -> Run As -> TestNG) and check the results. Both the test methods will be executed. And also, click “Results of running class SampleTest” tab, and click the Open TestNG Report button on right. You should see results in a better format.Note: You can get the results in html format on your local system by refreshing your java project (Right click TestNGForSelenium -> Refresh -> You will see test-output -> index.html -> right click -> Properties -> look for the location (ex: C:\QA\TestNGForSelenium\SeleniumTestNG\test-output\index.html). Go to that location and open index.html in a browser. This feature is not available in Junit.Good and useful features of TestNG:Configuration information (Annotations) for a TestNG class: @BeforeSuite: The annotated method will be run before all tests in this suite have run. @AfterSuite: The annotated method will be run after all tests in this suite have run. @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. @BeforeGroups: The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked. (not part of testng class)@AfterGroups: The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked. (not part of testng class)@BeforeClass: The annotated method will be run before the first test method in the current class is invoked. @AfterClass: The annotated method will be run after all the test methods in the current class have been run. @BeforeMethod: The annotated method will be run before each test method. @AfterMethod: The annotated method will be run after each test method.@DataProvider: For data-driven test@TestCreate TestNG script by following below steps:Create a TestNG Class (right click on “src”-> New -> Other -> TestNG -> TestNG Class -> Next (You can see 9 different annotations under “Annotations” table: These are the annotation functions in TestNG).Select Source folder as "/TestNGForSelenium/src" (click Browser button to do it), Package name as "test" and Class name as "YahooTest", select Annotations: @BeforeMethod and @BeforeTest and click Finish.Now, you should see package "test" generated with YahooTest.javaDouble click YahooTest.java and create script like below:(Example1)package test;import org.testng.annotations.Test;import org.testng.annotations.AfterTest;import org.testng.annotations.BeforeMethod;import org.testng.annotations.BeforeTest;public class YahooTest { @Test public void testReciveMail() {// Receiving Email code here...System.out.println ("Testing Receiving email");} @Test public void testSendMail() {// Sending Email code here... System.out.println ("Testing Sending email"); } @BeforeMethod public void beforeMethod() { System.out.println ("Execute @Test"); } @BeforeTest public void beforeTest() {//Login code… System.out.println ("----------------------"); System.out.println ("Login to YahooEmail"); System.out.println ("----------------------"); } @AfterTest public void afterTest() {//Logout code… System.out.println ("----------------------"); System.out.println ("Logout from YahooEmail"); System.out.println ("----------------------"); }}// Right click Yahootest.java and run as TestNG class// Observer the output results now...Open Console, you see the following:----------------------Login to YahooEmail - @BeforeTest----------------------Execute @Test - @BeforeMethodTesting Receiving email - @TestExecute @Test - @BeforeMethodTesting Sending email - @Test----------------------Logout from YahooEmail - @AfterTest----------------------PASSED: testReciveMailPASSED: testSendMail(Example2)Create another TestNG class with Source folder as "/TestNGForSelenium/src", Package name as "test1" and Class name as "YahooTest1", with @BeforeSuite, @AfterSuite, @BeforeTest, @AfterTest, @BeforeMethod and @AfterMethod and click Finish.package test1;import org.testng.annotations.Test;import org.testng.annotations.BeforeMethod;import org.testng.annotations.AfterMethod;import org.testng.annotations.BeforeTest;import org.testng.annotations.AfterTest;import org.testng.annotations.BeforeSuite;import org.testng.annotations.AfterSuite;public class YahooTest1 {@Test public void testReciveMail() {// selenium code here...System.out.println ("Test - Testing Receiving email");} @Test public void testSendMail() { // selenium code here... System.out.println ("Test - Testing Sending email"); }@BeforeSuite // once before executing all tests in all java filespublic void initializeTest() {System.out.println ("BeforeSuite - Test Execution started");}@AfterSuitepublic void shutDownSelenium() {// Stop/shutdown selenium server System.out.println ("AfterSuite - Test Execution stopped");}@BeforeTest // before executing all test casespublic void openBrowser () {System.out.println ("BeforeTest – Open Browser");System.out.println ("__________________________");}@AfterTest // after executing all test casespublic void closeBrowser() {System.out.println("AfterTest – Close Browser");}@BeforeMethod // before executing every testpublic void Login() {System.out.println ("__________________________");System.out.println ("BeforeMethod- Login");}@AfterMethodpublic void Logout() {System.out.println ("AfterMethod - Logout");System.out.println ("__________________________");}}// Right click Yahootest.java and run as TestNG class// Observer the output results now...Open Console, you see the following:BeforeSuite - Test Execution startedBeforeTest – Open Browser____________________________________________________BeforeMethod- LoginTest - Testing Receiving emailAfterMethod - Logout____________________________________________________BeforeMethod- LoginTest - Testing Sending emailAfterMethod - Logout__________________________AfterTest – Close BrowserPASSED: testReciveMailPASSED: testSendMail=============================================== Default test Tests run: 2, Failures: 0, Skips: 0===============================================AfterSuite - Test Execution stopped===============================================Default suiteTotal tests run: 2, Failures: 0, Skips: 0===============================================Batch Test in TestNG ****Execute multiple tests in a batch in a serial/sequential orderThis can be done using the file called testng.xml1. Create another testng class: YahooNewsTest under test1 package with below code: (right click test1->new->other-> testng->testngclass)package test1;import org.testng.annotations.BeforeTest;import org.testng.annotations.Test;public class YahooNewsTest {@BeforeTest // **public void xyz(){System.out.println ("Before Executing Yahoo News Test");}@Test // **public void testNews(){System.out.println("Executing Yahoo News Test");}}2. Copy testng.xml file (can find out in G Drive: SeleniumFiles folder) and past it under the project folder (ex: TestNGForSelenium) - not under the src folder.3. testng.xml has some configurations which will tell testng and eclipse that these are the following test cases to be run / executed.4. Right click testng.xml-> Open With -> Other -> DTD Editor and update the xml file like below with above 3 tests or double click testing.xml and click source tab.<!DOCTYPE suite SYSTEM ""><suite name="Yahoo"><test name="Yahoo Mail Test1"><classes><class name="test1.YahooTest1"></class></classes></test><test name="Yahoo Mail Test"><classes><class name="test.YahooTest"></class></classes></test><test name="Yahoo News Test"><classes><class name="test1.YahooNewsTest"></class></classes></test></suite><!-- neodl6.grp.bf1. Sat Nov 19 18:13:53 PST 2016 --><!-- neodl1.grp.bf1. Mon Mar 5 20:00:08 UTC 2018 -->5. Right click testng.xml -> Run As -> TestNG Suite// All tests will be executed and you will see total 3 pass and 0 failed and skipped. 3 pass means, 3 methods. There are 2 methods in YahooTest1.java and 1 method in YahooNewsTest.javaSKIP test in TestNGHow to SKIP the test(s) from batch execution? Assume, we don’t want to execute YahooNewsTest. Option1, delete it from xml file, else follow below approach (preferred):Open YahooNewsTest.java and update the script like below:Under @Test / System.out.println….Add below lines of code:throw new SkipException(“Skipping the test because this feature broken in this release”);Position the cursor or Right click on SkipException and select Import or cntr+shift+O (not zero)…. to add Import statement within the script.Your YahooNewsTest.java looks like this (after adding SkipExecption):package test1;import org.testng.SkipException;import org.testng.annotations.BeforeTest;import org.testng.annotations.Test;public class YahooNewsTest {@BeforeTest // **public void xyz(){System.out.println ("Before Executing Yahoo News Test");}@Test // **public void testNews(){System.out.println("Executing Yahoo News Test");throw new SkipException("Skipping the test because this is not part of regression test for this release"); }}Now, right click on testng.xml: -> right click -> Run As -> TestNGSuite. Check the results now… you will see one test skipped and YahooNewTest marked as yellow.Data-Driven / Parameterizing Tests You can run single test with different sets of input data (login with valid username/pwd, invalid username/pwd etc).Test Case: Create multiple new user accounts in Yahoo Mail.Create a new test: package: test -> right click -> New -> Other -> TestngClass -> YahooRegistrationTest.java (make sure Source folder is: \TestNGForSelenium\src and Package name is: test) and select TestNG annotation @DataProviderpackage test;import org.testng.annotations.DataProvider;import org.testng.annotations.Test;public class YahooRegistrationTest {@Test (dataProvider="registerData")public void testRegister (String username, String password, String email, String city) {// selenium web driver code// launch browser – web deriver driver = new chromedriver();// enter url – driver.get ();// click register link// first name fname// last name lname// username Uname// password Pwd// email// city// click register button – register.click();System.out.println (username +"--"+password +" -- "+email +"-- "+city +"--");}@DataProviderpublic Object [][] registerData(){//path of the excel sheet C:\Selenium\1218\TestNG\TestData.xlsx// rows - number of times test has to be repeated (ex: 3 rows)// col - parameters in dataObject [][] data =new Object [3][4]; // 3 rows and 4 columns// first rowdata[0][0]="user1"; data[0][1]="password1";data[0][2]="email1";data[0][3]="city1";// second rowdata[1][0]="user2";data[1][1]="password2";data[1][2]="email2";data[1][3]="city2";// third rowdata[2][0]="user3";data[2][1]="password3";data[2][2]="email3";data[2][3]="city3";return data;}}// Run As TestNG Test. Check the results… you should see that test executed 3 times with 3 different input values.TestNG Assertions / Reporting Errors Asserts helps us to verify the conditions of the test and decide whether test has failed or passed. A test is considered successful ONLY if it is completed without throwing any exception.Make a copy of YahooNewsTest.java to YahooNewsTest2.java in test1 packageUpdate the script like below:package test1;import org.testng.annotations.Test;import org.testng.annotations.BeforeMethod;import org.testng.Assert;import org.testng.annotations.AfterMethod;import org.testng.annotations.BeforeTest;import org.testng.annotations.AfterTest;import org.testng.annotations.BeforeSuite;import org.testng.annotations.AfterSuite;public class YahooNewsTest2 {@BeforeTest // **public void xyz(){System.out.println ("Before Executing Yahoo News Test");}@Test // **public void testNews(){System.out.println("Executing Yahoo News Test");// selenium code// expected, actual results// text is present// link is presentAssert.assertEquals("Selenium", "Selenium");// This will show pass if title of the page is Selenium – Google Search//Assert.assertEquals("Good", "Good1");// This will show fail since both expected and actual are differentAssert.assertTrue(6>1, "Error Message");// Condition satisfied so it will passAssert.assertFalse(10>9, "Error Message");// Condition not satisfied so it will fail with message.}}// Run the test nowAnother example (with Selenium WebDriver):@Testpublic void testCaseVerifyHomePage() {driver= new FirefoxDriver(); // FF browser will be launcheddriver.navigate().to(""); // will be openeddriver.sendkeys(“QA”);driver.GoogleSerach.click();Assert.assertEquals("QA – Google Search", driver.getTitle());}// Here we are verifying if the page title is equal to 'Google' or not. If the page title is not matching with the text / title that we provided, it will fail the test case.TestNG “Error / Exception” HandlingAssume, while running multiple scripts in a batch and if one of the script has an issue (line number 10 has an issue (total lines are 19)), results will show 1 failure and also, other lines in the script will not execute and it will move to next script and execute.This is not a good practice. It should work like, though there is an error within the script, it should skip that particular line(s) of code and execute rest all within the same script (if there is NO dependency) and then execute next script and also, all will show pass in the results and next lines of code (after the error line) also will execute. In this chapter, you will learn how try & catch works. Here is an example. Save YahooNewsTest2.java as YahooNewsTest3.java and modify the script accordingly like below:package test1;import org.testng.annotations.Test;import org.testng.annotations.BeforeMethod;import org.testng.Assert;import org.testng.annotations.AfterMethod;import org.testng.annotations.BeforeTest;import org.testng.annotations.AfterTest;import org.testng.annotations.BeforeSuite;import org.testng.annotations.AfterSuite;import org.testng.*;public class YahooNewsTest3 {@BeforeTest // **public void xyz(){System.out.println ("Before Executing Yahoo News Test");}@Test // **public void testNews(){System.out.println("Executing Yahoo News Test");// selenium code// expected, actual results// text is present// link is presentAssert.assertEquals("Good", "Good");// This will show pass since both expected and actual are same//Assert.assertEquals("Good", "Good1");// This will show pass since both expected and actual are sameAssert.assertTrue(6>1, "Error Message");// Condition satisfied so it will pass System.out.println ("Before Assertion Error");try{Assert.assertTrue(6>11, "Error Message");// Condition not satisfied so it will fail - this is an error// click Today’s deals}catch(Throwable t) { // assersion is an error (not an exception), Throwable can handle both error and exception.///System.out.println("Caught the error");}Assert.assertFalse(8>9, "Error Message");// Condition satisfied so it will passSystem.out.println ("After Assersion Error");}}Example: If you are verifying all the links there in and if one or more of the links have an issue, it will just skip the link(s) and continue with next link verification. This can be done by including try/catch functions within the code.Note: Go thru Automation End to End Process and Selenium Architecture before starting Selenium WebDriver. ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download